尝试使用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 -运行良好,有人在亚马逊网络服务弹性豆茎上使用过吗?有什么教程吗?TQ
//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用户将引号放在"Program Files“周围
发布于 2015-12-19 00:51:31
我在Windows和Apache上遇到了同样的问题,出现了同样的错误信息,我刚才修复了这个问题,在commandOptions上使用了'bypass_shell' => 'true';,并指定了指向您安装的wkhtmltopdf文件夹的二进制路径,通常在Program Files中。
工作代码:
$pdf = new Pdf([
'commandOptions' => [
'useExec' => false,
'escapeArgs' => false,
'procOptions' => array(
// This will bypass the cmd.exe which seems to be recommended on Windows
'bypass_shell' => true,
// Also worth a try if you get unexplainable errors
'suppress_errors' => true,
),
],
]);
$globalOptions = array(
'no-outline', // Make Chrome not complain
// Default page options
'page-size' => 'Letter'
);
$pdf->setOptions($globalOptions);
$pdf->addPage('http://www.google.com');
$pdf->binary = 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe';
if (!$pdf->send()) {
throw new Exception('Could not create PDF: '.$pdf->getError());
}如果将useExec命令参数设置为true,则应在二进制路径中为"Program Files“添加双引号,否则将出现错误C:\Program is not recognized as a internal or external command、operable program or batch File.感谢Mikehaertl的有用文档和wkhtmltopdf的出色工作。干杯!
https://stackoverflow.com/questions/30502397
复制相似问题