【问题标题】:phpwkhtmltopdf - Could not create PDF - Failed without error messagephpwkhtmltopdf - 无法创建 PDF - 失败且没有错误消息
【发布时间】: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


【解决方案1】:

我在 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 an internal or external command, operable program或批处理文件 .. 感谢 Mikehaertl 的有用文档和 wkhtmltopdf 的出色工作。干杯!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2022-01-02
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    相关资源
    最近更新 更多