【发布时间】:2015-12-19 00:51:30
【问题描述】:
尝试使用 PHP 和包 phpwkhtmltopdf 创建 PDF
require '../vendor/autoload.php';
use mikehaertl\wkhtmlto\Pdf;
// You can pass a filename, a HTML string or an URL to the constructor
$pdf = new Pdf('http://www.google.co.uk');
// On some systems you may have to set the binary path.
//$pdf->binary = 'C:\pdf';
$pdf->send('google.pdf');
if (!$pdf->send()) {
throw new Exception('Could not create PDF: '.$pdf->getError());
}
但是得到错误
Fatal error: Uncaught exception 'Exception' with message 'Could not create PDF: Failed without error message: wkhtmltopdf "http://google.com" "C:\Windows\Temp\tmp4047.tmp.pdf"' in C:\wamp\www\site\ajax\createpdf.php on line 24
转到 c:\windows\temp 并可以看到文件 tmp4047.tmp.pdf - 但已损坏且无法加载
从命令行运行 wkhtmltopdf 没有任何问题 - PDF 创建正常
wkhtmltopdf http://google.com google.pdf
编辑 - 使用 snappy 代替 - 工作正常,有没有人在 AWS 弹性豆茎上工作?任何教程?周全
//snappy
use Knp\Snappy\Pdf;
$snappy = new Pdf('C://"Program Files"/wkhtmltopdf/bin/wkhtmltopdf.exe');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $snappy->getOutput('http://www.google.co.uk');
注意 - windows 用户在“程序文件”周围加上引号
【问题讨论】:
-
可能是权限问题。尝试捕获该异常并打印堆栈跟踪,然后使用跟踪查看 wkhtmltopdf 的源并查看它崩溃的确切位置。
-
嗨 - 你是对的,但是更快地尝试了不同的包
标签: php wkhtmltopdf