PHP 执行 linux 命令终止得太早?

PHP executing linux commands terminate too soon?

我正在尝试使用 wkhtmltopdf 将 html 文档转换为 pdf。我将在 linux 上使用的命令:wkhtmltopdf 15.52579.html 15.52579.pdf.

这首先输出类似 Loading pages (1/6) [> ] 0% [===== 的内容,加载到 100%,然后显示:

Loading pages (1/6) Counting pages (2/6) Resolving links (4/6) Loading headers and footers (5/6) Printing pages (6/6) Done

我要php执行这个命令。我尝试使用 php 的 exec("wkhtmltopdf 15.52579.html 15.52579.pdf")shell_exec("wkhtmltopdf 15.52579.html 15.52579.pdf"),无论是否通过添加 2>&1 捕获 stderr。我也试过 proc_open 函数。

每次,我的结果都是Loading pages (1/6) [> ] 0% [======> ] 10%。看起来命令 returns 太快了,不允许程序完成并实际创建 pdf。

php 属于 运行 的用户具有执行该程序的正确权限。脚本由网页执行,应该在几秒钟内完成。我错过了什么?

另一种方法是使用一个名为 snappy 的库 (php5),它包装了 wkhtmltopdf 并提供了一个漂亮干净的面向对象的界面以及适当的错误报告。

$snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
$html = file_get_contents('http://www.yourdomain.tld/path/15.52579.html');
$file = '/your/local/path/15.52579.pdf';
$snappy->generateFromHtml($html, $file);

访问 github 下载和更多文档:https://github.com/knplabs/snappy