How do I get WKHTMLTOPDF to execute via PHP?-Collection of common programming errors
Ok, this has been asked to some degree before but there are no solutions or accepted answers and I’d like to try and be more comprehensive in my question so:
I’m trying to WKHTMLTOPDF up and running via PHP on a shared server (in this case it’s MediaTemple (gs)). According to the host there is no reason this won’t work and in fact it is working via SSH. So…
I’ve tried a variety of things, the most basic does nothing, just silently fails:
exec("/path/to/binary/wkhtmltopdf http://www.google.com pdf1.pdf");
– Source: Question on Stack Overflow
The full PHP bindings along with the following give me errors, which despite my best Googling I can’t figure out:
Call:
$html = file_get_contents("http://www.google.com");
$pdf = new WKPDF();
$pdf->set_html($html);
$pdf->render();
$pdf->output(WKPDF::$PDF_EMBEDDED,'sample.pdf');
– Source: WKHTMLTOPDF on Google Code
Error:
Fatal error: Uncaught exception 'Exception' with message 'WKPDF didn't return
any data.
Loading pages (1/6) [> ] 0% [======> ] 10% terminate called
after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc
' in /path/to/wkhtmltopdf.php:206 Stack trace: #0 /path/to/index.php(8): WKPDF->render() #1 {main} thrown in /path/to/wkhtmltopdf.php on line 206
And once I got this (below is an extract as I can't reproduce it now):
Qt Concurrent has caught an exception thrown from a worker thread. This is not
supported, exceptions thrown in worker threads must be caught before
control returns to Qt Concurrent.
I’ve also tried a few other options but with the same results; no PDF. So what do I do now, how do I figure out what’s wrong? My PHP level is on the basic side but I’ll do my best. Many thanks in advance.
-
You can also try my project here. It provides a clean OO interface to the command line utility:
http://mikehaertl.github.io/phpwkhtmltopdf/
Usage is very simple:
Originally posted 2013-11-27 05:08:43.