我正在尝试使用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%
。看来该命令返回得太早,不允许程序完成并实际创建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
关于php - PHP执行linux命令是否终止太早?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33868195/