0

I'm converting HTML documents to PDF by invocating wkhtmltopdf with proc_open().

Here is an example script:

$wkhtmltopdf = '/bin/wkhtmltopdf';

$html = '<html><body><h1>Hello world!</h1></body></html>';

$process = proc_open($wkhtmltopdf . ' - -', [
    0 => ['pipe', 'r'],
    1 => ['pipe', 'w'],
    2 => ['pipe', 'w']
], $pipes);

fwrite($pipes[0], $html);
fclose($pipes[0]);

$pdf = stream_get_contents($pipes[1]);
fclose($pipes[1]);

$err = stream_get_contents($pipes[2]);
fclose($pipes[2]);

$status = proc_close($process);

if ($status != 0) {
    die('Status ' . $status . ': ' . var_export($err, true));
}

echo substr($pdf, 0, 4);

If I run the script from the PHP CLI, the script works fine, and outputs the PDF header as expected:

%PDF

If however, I run it from Apache's PHP module, I always get the same error:

Status 134: ''

Googling status 134 gives something like "assertion failed", which does not help me much. stderr being empty, it's not helpful either.

Why doesn't this code run under Apache's PHP module?

I'm using wkhtmltopdf 0.12.3 under Fedora 25, with PHP 7.1.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • Does it really work from the CLI, or just from CLI with X11 running? See also http://stackoverflow.com/questions/38719715/the-exit-status-code-134-says-something-went-wrong // Exit code 134 usually corresponds to SIGABRT, btw. -- Also the `error.log` might be more relevant, if stderr yields nothing. – mario Jan 08 '17 at 20:48
  • Hmm you're right, I'm using the CLI in a graphical environment. If I try via SSH, I now get the following error: *Error 134: 'QXcbConnection: Could not connect to display*. Is there a way to make it work in this case? – BenMorel Jan 08 '17 at 21:07
  • Like my grandmother used to say: compile from source or GTFO. Your wkhtmltopdf binary is unsuitable for server use without workarounds. – mario Jan 08 '17 at 21:30
  • On the server where it's working on PHP-FPM, the version is `wkhtmltopdf 0.12.2.1 (with patched qt)`. Do you think the `patched qt` thing is what makes it work without X11 running? – BenMorel Jan 08 '17 at 21:48
  • 1
    OK, I finally downloaded the archive from the [official wkhtmltopdf download page](http://wkhtmltopdf.org/downloads.html), and this archive contains a compiled binary version that works out of the box. I uninstalled the version that comes with Fedora 25 and replaced it with this one, and it works like a charm. Thanks for your help! Note that the solutions in the question you linked to involve working around with `xvfb`, but none of the proposed solutions seemed directly applicable to Fedora (no package with this name exists). – BenMorel Jan 08 '17 at 23:31
  • Stupid close... -.-' – Carlos Jan 13 '20 at 23:31

0 Answers0