我一直在努力奋斗,感到无助.Prestashop使用tcpdf从使用Smarty填充的HTML模板生成发票和交付单.我们正在努力更新发票设计,发现缺少CSS支持的tcpdf.经过一些研究,我们决定将wkhtmltopdf作为将HTML/CSS模板转换为PDF的正确工具.
问题
该商店具有将多个发票导出为单个PDF的功能.使用TCPDF我能够通过在生成文件之前具有奇数页数的每张发票之后插入空白页来使批处理文件准备好进行双面打印.但是现在我们切换到了wkhtmltopdf我无法达到同样的效果.
关键问题是,虽然wkhtmltopdf允许使用多个HTML模板,但似乎没有可靠的方法来确定在生成文件之前它们将具有的页数.页眉和页脚模板可以接收发票最终的页数,但它们与主要内容分开,因此我无法相应地插入分页符.
我也尝试计算height of the content/ PDF page height但是一旦我开始导出多个模板就会出现各种问题(使用单个模板可以正常工作).这种方法不是很好,因为在内容中插入空白页本身也会导致页脚出现在新页面上,这不是我想要的.
我最好的尝试
我发现的唯一方法可能让我解决这些问题是非常低效的.使用的一个单独的实例每个模板添加到批我可以一次预先生成它包装的wkhtmltopdf,得到了临时文件名,确定它有多少页中使用pdfinfo,并相应地添加一个空白HTML模板到主实例.这是一个函数的草稿,用于获取添加的最后一个模板的页面数(从扩展包装器的类,基于我在SO上找到的其他一些pdfinfo 问题):
/**
* Return the number of pages in the last invoice template added
* $complete === true => return the length of the entire document
*/
public function getNumPages($complete = false)
{
if (!$complete) {
// Generate …Run Code Online (Sandbox Code Playgroud) 我是Symfony2的新手,在尝试运行这样的异步命令时遇到了阻塞:
class MyCommand extends ContainerAwareCommand{
protected function configure()
{
$this
->setName('my:command')
->setDescription('My command')
->addArgument(
'country',
InputArgument::REQUIRED,
'Which country?'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$country = $input->getArgument('country');
// Obtain the doctrine manager
$dm = $this->getContainer()->get('doctrine_mongodb.odm.document_manager');
$users = $dm->getRepository('MyBundle:User')
->findBy( array('country'=>$country));
}}
Run Code Online (Sandbox Code Playgroud)
当我从命令行调用它时,这非常有效:
php app/console my:command uk
Run Code Online (Sandbox Code Playgroud)
但是当我称之为Symfony2进程时,它不起作用:
$process = new Process("php ../app/console my:command $country");
$process->start();
Run Code Online (Sandbox Code Playgroud)
我收到一个数据库错误:" [MongoWriteConcernException] 127.0.0.1:27017:not master "
我认为这意味着该过程没有得到我的数据库配置...
我只想运行异步进程,还有其他方法吗?
也许一种方法来调用不需要答案的应用程序命令继续?
也许我需要使用注射?
PS:我目前的命令只是一个测试,最后应该是一个"昂贵"的操作...
存在哪种设计模式来实现一些PHP进程的执行以及在一个PHP进程中收集结果?
背景:
PHP中有很多大树(> 10000个条目),必须对它进行递归检查.我想减少执行时间.
我有一个websocket应用程序,我正在构建一个游戏,基于Ratchet,它使用React事件循环.在这个脚本的开头,我已经想出了如何实现一个periodictimer,每秒向游戏发送一个脉冲,然后执行ticks和combat rounds.这非常有效.
但是,我最近意识到我还需要添加"滞后"客户端的功能,或者暂停在函数中执行.例如,如果玩家被击晕,或者我希望NPC在回复触发器之前等待1.5秒,以获得更"逼真"的会话感觉.
这个功能是内置在反应库中的,还是我必须通过其他方式实现的?经过一些研究,看起来我可能正在寻找pthreads,看看这个问题/答案:如何在PHP应用程序中使用多线程
为了更清楚我想要实现的目标,请以此代码为例:
function onSay($string)
{
global $world;
$trigger_words = array(
'hi',
'hello',
'greetings'
);
$triggered = false;
foreach($trigger_words as $trigger_word)
{
if(stristr($string, $trigger_word))
{
$triggered = true;
}
}
if($triggered)
{
foreach($world->players as $player)
{
if($player->pData->in_room === $this->mobile->in_room)
{
sleep(1);
$this->toChar($player, $this->mobile->short . " says '`kOh, hello!``'");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
显然,这不起作用,因为sleep(1)函数将暂停整个服务器进程.
任何见解将不胜感激.谢谢!
更新:我的服务器脚本:
require 'vendor/autoload.php';
require 'src/autoload.php';
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use React\Socket\Server as Reactor;
use React\EventLoop\Factory as LoopFactory;; …Run Code Online (Sandbox Code Playgroud) 我使用phpmyadmin进行MySQL管理.当我做一个耗时几分钟的昂贵查询时,phpmyadmin似乎阻止了其他标签中正在进行的所有其他活动.我仍然可以使用mysql控制台进行查询,但我不能在任何选项卡中使用phpmyadmin,只有在其他选项卡中的大查询完成时才会加载和完成.我能以某种方式改变它吗?
我实际上是在尝试监视一个PHP变量(可能是一个单独的线程但在PHP中不可能),并且只要变量的值发生变化就会触发PHP函数.
例如:让变量$ _GLOBALS ['foo'] = 1;
如果在代码中的任何一点,$ _GLOBALS ['foo']的值变为其他东西,我想立即触发PHP函数.
变量可以是循环内部或函数中的任何位置.
为什么我想要这个:我有一个变量存储作为文本出现的最后一个错误.如果变量的值从""更改为其他值,我想触发错误.我的LOGIC可能看起来有点奇怪,但这就是我想做的事情.
Thanx提前.
编辑:我试过:如何在PHP中使用监视器?和一个如何使用多线程的PHP应用程序,但似乎并没有解决问题.
守则(认为这可以解决你对我的问题的一些疑虑):
public function addtag($tagarray,$uid,$tagtype="1")
{
$dbobj=new dboperations();
$uiobj=new uifriend();
$tagid=$uiobj->randomstring(30,DB_SOCIAL,SOCIAL_TAG,tag_tagid);
$c=0;
foreach($tagarray as $tags)
{
$c++;
$tagname=$tags["tagname"];
$taguid=$tags["tagid"];
$dbobj->dbinsert("INSERT INTO ".SOCIAL_TAG." (".tag_tagid.",".tag_fuid.",".tag_tuid.",".tag_tagname.",".tag_tagtype.") VALUES
('$tagid','$uid','$taguid','$tagname','$tagtype')",DB_SOCIAL);
}
if($c==0)
{
$lasterror="No tags were added";return "";
}
else
{
return $tagid;
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,如果我调用错误处理函数而不是监视变量,在我的情况下它是不可取的,因为错误处理函数可以执行任何操作,如给出警报和重定向到页面或任何类似的操作.
我问这个问题的原因,我想如果脚本没有到达行返回""; 这会影响项目的工作流程.这就是我担心的事情.
我正在讨论的变量是$ lasterror,我有很多这样的函数,其中使用$ lasterror.
我正在寻找一种在 PHP 上执行多线程的方法,并遇到了 pthreads PHP API,我认为这很容易实现(但是我必须找出如何安装支持 ZTS 的 PHP 版本对 Debian) .
问题是,当我查看 pthreads php.net 文档时,我发现了这个:
提示 考虑改用并行。
我不知道的。
我的目标是获取项目列表,并为每个项目打开一个 websocket,它永远监听某些更新。所以线程应该永远存在,如果被杀死,或者停止,或者这样,它应该重新启动(但是我认为我可以在外部处理这个)。
我不确定哪一种最适合这种情况。有什么推荐吗?
我遇到了一个问题,我的数据库调用显着减慢了页面加载速度。我正在从选举数据中填充多个图表,我的表包含大约 100 万行,我必须在方法内的每个方法中多次查询这些数据getCharts()。
我正在使用它来将返回数据传递给 JavaScript。
当您单击数据点时,这些图表会重新填充。因此,如果您单击一个点 ie ('democrat),它将重新加载页面并再次调用这些方法。
我要问的是是否有可能在原生 PHP 中做这样的事情。服务器在 linode 上运行 PHP 5.2。
foreach(function in getChartsMethod){
Start a child thread to process the function.
}
join the threads.
reload the page.
public function getCharts(){
$this->get_cast_chart();
$this->get_party_chart();
$this->get_gender_chart();
$this->get_age_chart();
$this->get_race_chart();
$this->get_ballot_chart();
$this->get_county_chart();
$this->get_precinct_chart();
$this->get_congressional_district_chart();
$this->get_senate_district_chart();
$this->get_house_district_chart();
return view('elections.index');
}
Run Code Online (Sandbox Code Playgroud)
样品方法
public function get_party_chart(){
$query = DB::table($this->tableName)
->select('party', DB::raw('count(*) as numVotes'))
->groupBy('party')
->orderBy('numVotes', 'ASC');
if ($this->filterParameters != null){
$query = $query->where(key($this->filterParameters), $this->operator, current($this->filterParameters));
}
$chartData = …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我必须在注册时将短信发送给用户.但是在数据库中插入记录时我想在浏览器中点击此URL.
任何人都可以建议如何在backgound运行此URL
http://www.myurl.com/smpp/sendsms?username=XX&password=XX&to=XX&from=XX&text=Test
我正在尝试使用API发送短信.它每秒发送几乎一条SMS但我想在一秒内使用PHP中的多线程/ pthreads发送多条SMS.怎么可能或者我怎样才能至少从我的端到异步发送多个SMS请求到API服务器.
//Threads Class
class MThread extends Thread {
public $data;
public $result;
public function __construct($data){
$this->data = $data;
}
public function run() {
foreach($this->data as $dt_res){
// Send the POST request with cURL
$ch = curl_init("http://www.example.com");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dt_res['to']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$this->result = $http_code;
/**/
}
}
}
// $_POST['data'] has multi arrays
$request = new MThread($_POST['data']);
if ($request->start()) {
$request->join();
print_r($request->result);
}
Run Code Online (Sandbox Code Playgroud)
任何想法将不胜感激.
我正在使用PHP从多个比特币交易所获取价格数据和交易量数据,但是当您加载页面时,它需要将近20秒。如何改善载入时间?我认为这与卷曲有关。
<?php
function getData($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$rawData = curl_exec($curl);
curl_close($curl);
return json_decode($rawData, true);
}
//BTC Volume LocalBitcoins
$BTCVolumeLocal = getData('https://localbitcoins.com/bitcoinaverage/ticker-all-currencies/');
$LocalVolume = $BTCVolumeLocal["USD"]["volume_btc"];
//BTC Volume BTCE
$BTCVolumeBTCE = getData('https://btc-e.com/api/3/ticker/btc_usd');
$BTCEVolume = $BTCVolumeBTCE["btc_usd"]["vol_cur"];
//BTC Volume Bitstamp
$BTCVolumeStamp = getData('https://www.bitstamp.net/api/ticker/');
$StampVolume = $BTCVolumeStamp["volume"];
//BTC Volume Bitfinex
$BTCVolumeFinex = getData('https://api.bitfinex.com/v1/pubticker/btcusd');
$FinexVolume = $BTCVolumeFinex["volume"];
//BTC Volume OKCoin
$BTCVolumeOK = getData('https://www.okcoin.com/api/ticker.do?ok=1');
$OKCoinVolume = $BTCVolumeOK["ticker"]["vol"];
//BTC Volume LakeBTC
$BTCVolumeLake = getData('https://www.lakebtc.com/api_v1/ticker');
$LakeVolume = $BTCVolumeLake["USD"]["volume"];
//Totals the Volumes
$TotalVolume …Run Code Online (Sandbox Code Playgroud) php ×10
asynchronous ×4
pthreads ×2
api ×1
bitcoin ×1
event-loop ×1
laravel ×1
mongodb ×1
mysql ×1
pdf ×1
phpmyadmin ×1
process ×1
reactphp ×1
symfony ×1
url ×1
wkhtmltopdf ×1