phpwkhtmltopdf: Could not create PDF: Failed without error message

It appears that no matter what I do, I cannot create a pdf at all, and there is no error or warning to give any hint as to what’s wrong.

$pdf = new Pdf;
$pdf->addPage('http://www.google.com');
if (!$pdf->saveAs('temp.pdf')) {
    throw new \Exception('Could not create PDF: '.$pdf->getError());
}

This produces the following: Could not create PDF: Failed without error message: wkhtmltopdf ‘http://www.google.com’ ‘/private/var/tmp/tmp_wkhtmlto_pdf_kThRYi.pdf’

I can run this no problem from the command line so I’m not sure what I’m doing wrong. I don’t know why it’s creating a random pdf within /private/var/.

I am using Symfony, and it is using version “^2.0”

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 26 (8 by maintainers)

Most upvoted comments

I was facing the same problem running on Windows and Apache with same error message, I fixed the issue just a few minutes 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 … Thanks to Mikehaertl for the appreciated solution and amazing work with wkhtmltopdf. Cheers my friend.

That random file is OK, it’s deleted after the PDF was downloaded or saved. Can you check if there’s maybe some output on stdout: echo $pdf->getCommand()->getOutput()? If you’re on Windows, you can also try to set the useExec feature (see here)