phpwkhtmltopdf - 无法创建PDF - 没有错误消息失败

时间:2015-05-28 09:23:36

标签: php wkhtmltopdf

尝试使用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

enter image description here

去c:\ windows \ temp并且可以看到文件tmp4047.tmp.pdf - 但是已损坏并且不会加载

从命令行运行wkhtmltopdf没有问题 - PDF创建正常

 wkhtmltopdf http://google.com google.pdf

编辑 - 使用snappy代替 - 工作正常,是否有人在AWS弹性beanstalk上工作?任何教程? 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用户将引号括在“程序文件”

1 个答案:

答案 0 :(得分:2)

我遇到了在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不被识别为内部或外部命令,可运行的程序或批处理文件。向Mikehaertl致信,他的有用文档和wkhtmltopdf令人惊叹的工作。干杯!