wkhtmltopdf via proc_open() does not work in Apache's PHP module

341 Views Asked by At

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.

0

There are 0 best solutions below