duanli8391 2015-05-28 09:23
浏览 682

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

Trying to create PDFs using PHP and the package phpwkhtmltopdf

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());
}

However get the error

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

Went to c:\windows\temp and can see file tmp4047.tmp.pdf - but is corrupt and wont load

Have run wkhtmltopdf from command line with no issues - PDF is created ok

 wkhtmltopdf http://google.com google.pdf

EDIT - used snappy instead - works fine, has anyone got this working on AWS elastic beanstalk? any tutorials? 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');

NOTE - windows users put quotes round "Program Files"

  • 写回答

1条回答 默认 最新

  • dousi8931 2015-12-18 16:51
    关注

    I was facing the same problem running on Windows and Apache with same error message, I fixed the issue just a few moments ago, by using 'bypass_shell' => 'true'; on the commandOptions and also specifying the binary path pointing to your installed wkhtmltopdf folder, normally in Program Files.

    Working code:

    $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());
              }
    

    If you set the useExec command parameter to true, then in your binary path you should add double quotes to "Program Files" or you will get the error C:\Program is not recognized as an internal or external command, operable program or batch file .. Credits to Mikehaertl for his helpful documentation and amazing work with wkhtmltopdf. Cheers!

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line