我正在使用 wkhtmltopdf 从 php 生成 pdf,但我有一个特殊的问题。当模板不使用任何外部源时,一切都很好。但是当它通过 http/file 协议(protocol)导入任何内容时,我得到:
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
异常告诉我:
命令:“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,它使用 Knp snappy 库,该库使用 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
希望有帮助。 :)
关于php - 从 php 执行时 Wkhtmltopdf 无法解析主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28347218/