从php执行Wkhtmltopdf时无法解析主机


Wkhtmltopdf fails to resolve host when executed from php

我正在使用wkhtmltopdf从php生成pdf,但我有一个特殊的问题。当模板不使用任何外部源时,一切都很好。但当它通过http/file协议导入任何内容时,我会得到:

The exit status code '1' says something went wrong:
stderr: ""
stdout: "Loading pages (1/6)
[> ] 0%
[======> ] 10%
[=======> ] 13%
[============================================================] 100%
Counting pages (2/6) 
[============================================================] Object 1 of 1
Resolving links (4/6) 
[============================================================] Object 1 of 1
Loading headers and footers (5/6) 
Printing pages (6/6)
[> ] Preparing
[============================================================] Page 1 of 1
Done 
Exit with code 1 due to network error: HostNotFoundError 

例外告诉我:command: "D:'Programs'wkhtmlpdf'bin'wkhtmltopdf.exe" --lowquality "C:'Windows'TEMP'knp_snappy54d3831ed861e8.25642951.html" "C:'Windows'Temp'D60C.tmp".

在这种情况下,是@import url(http://fonts.googleapis.com/css?family=Bree+Serif);导致了这个错误,但如果我想让它在php中工作,我就不能使用任何链接。当从命令行运行时,一切都很好。我已经检查了php是否从"SYSTEM"用户那里执行它,所以我为自己生成了一个以SYSTEM运行的cmd窗口,并且创建了pdf,没有任何问题。Allow_url_fopen已启用,openssl也已启用。

我使用的是KnpSnappyBundle,它使用KnpSnappy库,它使用wkhtmltopdf。我已经阅读了SO上所有与wkhtmltopdf相关的问题,但找不到答案。

PHP 5.5.5,apache 2.4.4

谢谢

更新:symfony控制台组件似乎用proc_open做了一些魔术。

$this->process = proc_open($commandline, $descriptors, $this->processPipes->pipes, $this->cwd, $this->env, $this->options);

参数var_dump为

string(227) "cmd /V:ON /E:ON /C "("D:'Programs'wkhtmlpdf'bin'wkhtmltopdf.exe" --lowquality "C:'Windows'TEMP'knp_snappy54d3949bd6e035.20793656.html" "C:'Windows'Temp'2627.tmp") 1>"C:'Windows'Temp'sf_26C4.tmp" 2>"C:'Windows'Temp'sf_26C5.tmp""" 
array(3) {
    [0]=> array(2) {
        [0]=> string(4) "pipe" 
        [1]=> string(1) "r" 
    } 
    [1]=> array(3) {
        [0]=> string(4) "file" 
        [1]=> string(3) "NUL" 
        [2]=> string(1) "w" 
    } 
    [2]=> array(3) {
        [0]=> string(4) "file" 
        [1]=> string(3) "NUL" 
        [2]=> string(1) "w" 
    } 
} array(0) {
} string(24) "D:'Web'e-diagnostyka'web" array(0) {
} array(3) {
    ["suppress_errors"]=> bool(true) 
    ["binary_pipes"]=> bool(true) 
    ["bypass_shell"]=> bool(true) 
}

我也遇到了类似的问题。它是在我运行一个composer更新之后开始发生的。

问题出现在KnpLabs/snappy存储库的最新版本中。

恢复到这个存储库的早期版本(0.3.1)为我修复了这个问题。

有关导致问题的更改的更详细信息,请参阅:KnpLabs/snappy version changes

希望能有所帮助。:)