把 html 文件转成 pdf

2019-01-04 10:23:41 +08:00
 Ewig
import pdfkit

pdfkit.from_url('http://www.sec.gov/Archives/edgar/data/1754581/000119312518361084/d620103df1.htm','/home/shenjianlin/out.pdf')

这个链接是打开的是 html,现在我通过程序转成 pdf 但是现在转换过程中报错了,大家有没有更好的方法,谢谢


Traceback (most recent call last):
File "timeout.py", line 3, in <module>
pdfkit.from_url('http://www.sec.gov/Archives/edgar/data/1754581/000119312518361084/d620103df1.htm','/home/shenjianlin/out.pdf')
File "/usr/lib/python3.4/site-packages/pdfkit/api.py", line 26, in from_url
return r.to_pdf(output_path)
File "/usr/lib/python3.4/site-packages/pdfkit/pdfkit.py", line 153, in to_pdf
'https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server' % stderr)
OSError: wkhtmltopdf: cannot connect to X server

You will need to run wkhtmltopdf within a "virtual" X server.
Go to the link below for more information
https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server
4137 次点击
所在节点    Python
15 条回复
Kamitora
2019-01-04 10:50:09 +08:00
chrome 打开,右键,打印,导出 pdf
sethverlo
2019-01-04 10:53:09 +08:00
wkhtmltopdf, pandoc, 还有个什么忘了,自己搜下吧,但是坑挺多的,不建议生产环境用,自己玩玩儿还是可以的
sethverlo
2019-01-04 10:53:44 +08:00
哦对了你这个 X server 指的是 x11
vissssa
2019-01-04 10:56:26 +08:00
binux
2019-01-04 10:56:35 +08:00
You will need to run wkhtmltopdf within a "virtual" X server.
Go to the link below for more information
https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server
kingwl
2019-01-04 10:57:21 +08:00
519718366
2019-01-04 10:57:44 +08:00
chrome 插件:Full Page Screen Capture
Aprilming
2019-01-04 11:19:56 +08:00
你这个更新到最新的 wkhtmltopdf,好像就可以了,这个我之前用过,需要 X server,但是服务器没有 X server,需要安装其他包才能解决,上次看的时候最新的 wkhtmltopdf 貌似自带了。
yidinghe
2019-01-04 11:22:01 +08:00
用 Chrome 的 headless 模式直接转换,无需 xserver
Aprilming
2019-01-04 11:23:05 +08:00
这玩意在生产环境中,还是比较坑比的,自己玩玩就算了,主要是在服务器上转 pdf 的速度比较慢,还经常报错,还有文件名中文的问题,反正之前用的时候,坑的一批,最后为了解决慢的问题,还重构了代码,ps:在自己电脑转的时候不算慢,但是到了服务器上真的慢。主要还是 X server 的问题
devforma
2019-01-04 11:47:20 +08:00
https://yq.aliyun.com/articles/601753
可以用阿里云函数计算搭一个服务
nogoodren
2019-01-04 11:48:45 +08:00
试一试虚拟打印机
l0o0
2019-01-04 13:38:39 +08:00
如果用 python 的话,推荐你试试这个 https://github.com/Kozea/WeasyPrint,我感觉比较不错,打印出来的效果比 wkhtmltopdf 好挺多的。而且开发还挺活跃,有问题直接提 issue
xjqaxl
2019-01-04 14:26:52 +08:00
phantomjs
Ewig
2019-01-04 16:49:54 +08:00
已经解决,原因是版本太低,在外国论坛找了一个高版本的已经解决,谢谢诸位!

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

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

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

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

© 2021 V2EX