proc_open 在 Apache2 和 CLI 中给出不同的输出

proc_open gives different output in Apache2 vs CLI

我正在使用 wkhtmltopdf 将我们网站上的 HTML 文档转换为 PDF 文件。我在 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);
}

注意:出于测试目的,我做了 $htmlData = file_get_contents('http://google.com');

当我在网络浏览器中浏览到该页面并单击 "download PDF" 时,我得到以下输出:

(Download original PDF file)

为了找出问题所在,我进入了命令行 运行:

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

效果很好,所以我想知道 PHP 是否有问题。我输入 php -a 并将上面的代码粘贴到命令行中,然后 运行 它运行得很好。

PDF 应如下所示:

(Download original PDF file)

为什么 运行 来自 Apache 的相同代码(通过我的网络浏览器)给出的 PDF 与直接在命令行上 运行 不同?这些奇怪的字符是从哪里来的?我怎么调试这个?

感谢,我解决了问题。我从我的命令行 运行 export 和 PHP 中的 proc_open 比较输出。

在我的命令行上,我看到 LANG=en_US.UTF8 但从 PHP 显示 LANG="C"

解决方案是在 proc_open

中的环境中设置 LANG
$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
]);