How do I get WKHTMLTOPDF to execute via PHP?
之前已经在某种程度上要求过此问题,但是没有解决方案或未接受的答案,我想尝试对问题进行更全面的介绍,以便:
我正在尝试在共享服务器上通过PHP启动并运行WKHTMLTOPDF(在本例中为MediaTemple(gs))。据主持人说,没有理由不起作用,实际上它是通过SSH起作用的。所以...
我尝试了各种方法,最基本的方法什么也没有做,只是默默地失败了:
1 |
-来源:关于堆栈溢出的问题
完整的PHP绑定以及以下内容给了我错误,尽管我做到了最好的谷歌搜索,但我仍然无法弄清:
呼叫:
1 2 3 4 5 | $html = file_get_contents("http://www.google.com"); $pdf = new WKPDF(); $pdf->set_html($html); $pdf->render(); $pdf->output(WKPDF::$PDF_EMBEDDED,'sample.pdf'); |
-资料来源:Google Code上的WKHTMLTOPDF
错误:
1 2 3 | Fatal error: Uncaught exception 'Exception' with message 'WKPDF didn't return any data. [cc lang="php"]Loading pages (1/6) [> ] 0% [======> ] 10% terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc |
'
在/path/to/wkhtmltopdf.php:206中堆栈跟踪:#0 /path/to/index.php(8):
WKPDF-> render()#1 {main}在第206行的/path/to/wkhtmltopdf.php中抛出
code> pre>
一旦知道了(以下是摘录,因为我现在无法重现):
1 2 3 | Qt Concurrent has caught an exception thrown from a worker thread. This is not supported, exceptions thrown in worker threads must be caught before control returns to Qt Concurrent. |
我还尝试了其他一些选择,但结果相同。没有PDF。那么,我现在该怎么办,我该如何找出问题所在?我的PHP级别是基本方面,但我会尽力而为。
您也可以在这里尝试我的项目。它为命令行实用程序提供了一个干净的OO接口:
https://github.com/mikehaertl/phpwkhtmltopdf
用法很简单:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?php use mikehaertl\\wkhtmlto\\Pdf; $pdf = new Pdf; // Add a HTML file, a HTML string or a page from a URL $pdf->addPage('/home/joe/page.html'); $pdf->addPage('<html>....</html>'); $pdf->addPage('http://google.com'); // Add a cover (same sources as above are possible) $pdf->addCover('mycover.html'); // Add a Table of contents $pdf->addToc(); // Save the PDF $pdf->saveAs('/tmp/new.pdf'); // ... or send to client for inline display $pdf->send(); |
看看snappy,这是一个PHP5库,可以从url或html页面生成缩略图,快照或PDF。它是wkhtmltopdf / wkhtmltoimage的包装。
您可能想要设置适当的标头,并将其设置为application / pdf,并将其设置为内联,以便人们可以在浏览器中看到它(几乎每个最新的浏览器都支持)并保存(如果需要)。这是我正在使用的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | exec("/usr/local/bin/wkhtmltopdf ../uploads/test.pdf"); $file ="../uploads/test.pdf"; $pdf = file_get_contents("../uploads/test.pdf"); header('Content-Type: application/pdf'); header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1 header('Pragma: public'); header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); header('Content-Length: '.strlen($pdf)); header('Content-Disposition: inline; filename="'.basename($file).'";'); ob_clean(); flush(); echo $pdf; ?> |
对于Linux:
1 |
对于Windows:
1 2 3 4 5 |
要从php(在Linux上)创建pdf,必须使用包装器。
1 2 3 | $cmd = '/usr/bin/xvfb-run --server-args="-screen 0, 1920x1080x24" /usr/bin/wkhtmltopdf http://google.com /tmp/google.pdf'; exec($cmd); |
这对我来说是一种预生成PDF报告,然后使用唯一的会话ID从请求页面(同一会话)中获取它的方法。
1 2 | $cmd ="/usr/local/bin/wkhtmltopdf 'http://127.0.0.1/myApp/pdfReport.php?key=something' tmp_reports/report_".$_POST['sessionid'].".pdf"; exec($cmd); |
运行时会发生什么:
1 2 3 |
我的猜测是,由于您没有为该文件指定任何文件夹,它将尝试将其写入当前工作目录。 (很可能是这种情况)Web用户可能对该文件夹没有写权限。因此,您应该能够通过提供Web用户可写文件夹的完整路径来解决此问题。
即。
1 |
除此之外,我不确定wkhtmltopdf是否可以正常运行,可能是因为它需要运行的X服务器(在未运行X的服务器上,可以通过安装Xvfb并使用xvfb-run运行wkhtmltopdf来解决此问题)。