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
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"
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!