我试图使用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正在运行的用户拥有正确的执行程序的权限。脚本由网页执行,应该在几秒钟内完成。我遗漏了什么?
发布于 2015-12-28 11:51:09
另一种方法是使用一个名为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
https://stackoverflow.com/questions/33868195
复制相似问题