简体   繁体   English

从PHP执行时,Wkhtmltopdf无法解析主机

[英]Wkhtmltopdf fails to resolve host when executed from php

I'm generating pdf from php using wkhtmltopdf, but I have peculiar problem. 我正在使用wkhtmltopdf从php生成pdf,但是我有一个特殊的问题。 When the template doesn't use any external sources everything is fine. 当模板不使用任何外部资源时,一切都很好。 But when it imports anything over http/file protocol I get: 但是,当它通过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 

Exception tells me that: command: "D:\\Programs\\wkhtmlpdf\\bin\\wkhtmltopdf.exe" --lowquality "C:\\Windows\\TEMP\\knp_snappy54d3831ed861e8.25642951.html" "C:\\Windows\\Temp\\D60C.tmp". 异常告诉我: command: "D:\\Programs\\wkhtmlpdf\\bin\\wkhtmltopdf.exe" --lowquality "C:\\Windows\\TEMP\\knp_snappy54d3831ed861e8.25642951.html" "C:\\Windows\\Temp\\D60C.tmp".

In this case it was @import url(http://fonts.googleapis.com/css?family=Bree+Serif); 在这种情况下,它是@import url(http://fonts.googleapis.com/css?family=Bree+Serif); which caused this error, but I simply can't use any link if I want it to work from php. 导致此错误的原因,但是如果我希望它可以从php运行,我根本无法使用任何链接。 When run from command line, everything is ok. 从命令行运行时,一切正常。 I've checked that php executes it from "SYSTEM" user, so I spawned myself a cmd window running as SYSTEM, and the pdf was created without any problems. 我检查了php是从“ SYSTEM”用户执行的,所以我产生了一个以SYSTEM身份运行的cmd窗口,并且创建了pdf时没有任何问题。 Allow_url_fopen is enabled, openssl as well. Allow_url_fopen已启用,openssl也已启用。

I'm using KnpSnappyBundle which uses Knp snappy library, which uses wkhtmltopdf. 我正在使用KnpSnappyBundle,它使用Knp snappy库,该库使用wkhtmltopdf。 I've read all wkhtmltopdf related problems on SO, but couldn't find an answer. 我已经阅读了所有与wkhtmltopdf相关的问题,但是找不到答案。

PHP 5.5.5, apache 2.4.4 PHP 5.5.5,Apache 2.4.4

Thank you 谢谢

Update: It seems symfony console component does some magic with proc_open. 更新:似乎symfony控制台组件使用proc_open做一些魔术。

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

And the parameters var_dump is 并且参数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) 
}

I had a similar problem. 我有一个类似的问题。 It started occurring after I ran a composer update. 我运行作曲家更新后,它开始发生。

The problem is in the latest release of the KnpLabs/snappy repository. 问题出在最新版本的KnpLabs / snappy存储库中。

Reverting back to an earlier release (0.3.1) of this repository fixed the problem for me. 恢复到该存储库的早期版本(0.3.1)对我来说解决了这个问题。

For a more detailed look at the changes made that are causing the problem, see: KnpLabs/snappy version changes 有关导致问题的更改的详细信息,请参阅: KnpLabs / snappy版本更改

Hope it helped. 希望能有所帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM