PHP 使用 wkhtmltopdf 的问题

2019-09-07 14:01:03 +08:00
 kkshell

问题麻烦跳转看下 https://segmentfault.com/q/1010000020318067

搞了一早上没得头绪,求支招

5105 次点击
所在节点    PHP
19 条回复
kkshell
2019-09-07 14:08:58 +08:00
youyou 会的大佬不
AlloVince
2019-09-07 14:46:25 +08:00
和 php 无关,wkhtmltopdf 需要启动图形界面,你可以借助 xvfb 封装一个无界面的命令行

具体可以参考我以前写过的一个 wkhtmltopdf 微服务 dockerfile

https://gist.github.com/AlloVince/947a2d08bc934a592b6eb58cc6410fde
kkshell
2019-09-07 15:01:23 +08:00
@AlloVince 好的谢谢,我参考一下
manhere
2019-09-07 15:04:21 +08:00
可以 composer require "knplabs/knp-snappy"
kkshell
2019-09-07 15:05:23 +08:00
@manhere 有什么作用
manhere
2019-09-07 15:06:30 +08:00
@kkshell https://github.com/KnpLabs/snappy 一直使用,没有问题
gamesme
2019-09-07 15:25:51 +08:00
虽然我是个小白 但是有看过一个 laravel 项目使用 wkhtmltopdf 项目叫 Bookstackapp 里面也是用楼上提到的 knpsnappy 包来调用 wkhtmltopdf
具体代码我就看不懂了 不知道能不能帮到你
gamesme
2019-09-07 15:26:56 +08:00
好像不是用 exec 是用 proc_open 执行
kkshell
2019-09-07 15:44:23 +08:00
@manhere
$snappy = new Pdf('/usr/bin/wkhtmltopdf');
$snappy->generateFromHtml($content, '/tmp/bill-123.pdf');

问题依旧如下:The exit status code '1' says something went wrong:
stderr: "wkhtmltopdf: cannot connect to X server
"
stdout: ""
command: /usr/bin/wkhtmltopdf --lowquality '/tmp/knp_snappy5d735f969db923.75691036.html' '/tmp/bill-123.pdf'.
kkshell
2019-09-07 15:44:48 +08:00
@gamesme 嗯,但是还是有这个问题 wkhtmltopdf: cannot connect to X server
kkshell
2019-09-07 15:45:07 +08:00
@AlloVince 网站无法访问
sleepm
2019-09-07 16:10:25 +08:00
你把它整复杂了
exec 直接调用 wkhtmltopdf 完了,要啥 xserver
kkshell
2019-09-07 16:17:19 +08:00
@sleepm 直接调用就是报这个错 string(41) "wkhtmltopdf: cannot connect to X server"
gamesme
2019-09-07 16:25:13 +08:00
目前为止我觉得二楼的回答是最靠谱的
For CentOS PHP environment the WkHTMLtoPDF tool not need xvfb-run to exec the command, But for Ubuntu PHP environment need xvfb-run to exec the command! I had revised my code as below and the issues was resolved

这是楼主您在 segmentfault 贴的解决办法链接,错误出现的原因应该是 Ubuntu 需要图形界面来运行 wkhtmltopdf
wangyongbo
2019-09-07 16:31:04 +08:00
你可以尝试升级到这个版本。wkhtmltopdf 0.12.4 (with patched qt)

我在 python 里面用的这个版本。 运行正常。
zjsxwc
2019-09-07 16:33:11 +08:00
基于 Qt Webkit 的工具你想不依赖 x server 就能运行?
183shl
2019-09-07 22:52:33 +08:00
借帖子问一下,我用 python 写了一个 markdown 转 pdf,表格渲染不出来怎么解决呢
linxiaojialin
2019-09-08 01:31:45 +08:00
Laravel:

# config/snappy.php
return [
'pdf' => array(
'enabled' => true,
'binary' => '/usr/local/bin/wkhtmltopdf',
'timeout' => false,
'options' => [],
'env' => [],
),
'image' => array(
'enabled' => true,
'binary' => '/usr/local/bin/wkhtmltoimage',
'timeout' => false,
'options' => [],
'env' => [],
),
];

# Controller@method:
$snappy = resolve('snappy.image.wrapper');
return $snappy->getOutput($url);
MeteorCat
2019-09-08 23:46:48 +08:00
这个玩意需要 x 桌面环境,也就是图形化处理;这个没办法,我记得蚂蚁笔记服务器部署也有这个,一安装这个划拉一堆 X 桌面环境

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/598836

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX