Proc_open gives different results in Apache2 vs CLI

I use wkhtmltopdfto convert HTML documents to PDF files on our website. I use the following code in my PHP class:

<?php

$pdfConv = proc_open('wkhtmltopdf -q -s letter --no-background --print-media-type --title Test - -', [
    0 => array('pipe', 'r'),
    1 => array('pipe', 'w'),
    2 => array('pipe', 'w')
], $pipes, '/tmp', NULL, [
    'bypass_shell' => true
]);

if(is_resource($pdfConv)){
    // Send STDIN
    fwrite($pipes[0], $htmlData);
    fclose($pipes[0]);

    // Receive STDOUT
    $pdfFile = stream_get_contents($pipes[1]);
    fclose($pipes[1]);

    // Set headers and send file to browser to be downloaded

    // Close process
    proc_close($pdfConv);
}

NOTE. For testing purposes, I did $htmlData = file_get_contents('http://google.com');.

When I looked at the page in my web browser and clicked "download PDF", I got the following output:

Messed up PDF File

( Download Source PDF )

Trying to understand what was wrong, I took on the command line and ran:

wkhtmltopdf -q -s letter --no-background --print-media-type --title Test http://google.com /tmp/google.pdf

This worked fine, so I thought something was wrong with PHP. I typed php -aand pasted the above code into the command line and ran it, and it worked perfectly.

Here's what the PDF looks like:

Good pdf file

( Download Source PDF )

Apache ( -) PDF , ? ? ?

+4
1

@Havenard, . export proc_open PHP .

LANG=en_US.UTF8, PHP LANG="C".

LANG proc_open

$pdfConv = proc_open('wkhtmltopdf -q -s letter --no-background --print-media-type --title Test - -', [
    0 => array('pipe', 'r'),
    1 => array('pipe', 'w'),
    2 => array('pipe', 'w')
], $pipes, '/tmp', [
    'LANG' => 'en_US.UTF8'
], [
    'bypass_shell' => true
]);
+1

All Articles