使用 Python 将 HTML 转换为 PDF
我正在尝试在 Django 中将 HTML 转换为 PDF 文档,但没有成功。
我尝试使用 wkhtmltopdf 0.9.9,但是 Apache 抛出 wkhtmltopdf 无法连接到服务器的错误。当我直接使用 wkhtmltopdf 时,它运行得非常好,并将 HTML 转换为 PDF 文档。
我也尝试过使用 unoconv,但是渲染的 PDF 文件没有应用任何 CSS。我也尝试过使用xhtml2pdf。我再次面临同样的问题;渲染的 PDF 文件未应用任何 CSS 样式。我花了今天和昨晚的大部分时间来尝试解决这个问题,但仍然距离解决问题还很远。
如果您需要更多信息,请告诉我
I am trying to convert HTML into a PDF document in Django and haven't been successful.
I have tried using wkhtmltopdf 0.9.9, however Apache throws an error that wkhtmltopdf cannot connect to server. When I use wkhtmltopdf directly, it runs perfectly fine and converts the HTML into a PDF document.
I have also tried using unoconv, however the rendered PDF file doesn't have any CSS applied to it. I have also tried using xhtml2pdf. Again I am facing same issue; the rendered PDF file doesn't have any CSS styling applied. I have spent the better part of today and last night trying to solve this issue and I'm still no closer to solving the problem.
Let me know if you need any more information
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为 Django 配置 Pisa 不应该太难 。
网上确实有几个例子可以向您展示如何做到这一点,并且
解释如何链接到模板中的外部资源:
在您的情况下,您应该尝试中提到的链接回调函数第一篇博文:
对于较新的 Django 版本,您可能应该使用
STATIC_ROOT
而不是MEDIA_ROOT
然后在您的渲染方法:
Configuring Pisa for Django shouldn't be too hard.
There are really several examples on the net that show you how to do it and
explain how to link to external resources in your templates:
In your case you should try the link-callback-function mentioned in the first blog post:
For newer Django-Version you probably should use
STATIC_ROOT
instead ofMEDIA_ROOT
Then use
fetch resources
accordingly in your render-method:我建议你使用 pisa、pypdf 和 html5lib 组合,它对我有用。
I suggest you to use pisa, pypdf and html5lib combination, it worked for me.
一个可能但不太优雅的解决方案是运行一个小脚本,通过无头浏览器组件(Linux 上的 webkit/xvfb)渲染 html,然后将其另存为 pdf。
A possible, but not so elegant solution, is to run a small scripts which renders the html via a headless browser component (webkit/xvfb on Linux) and then saves it as a pdf.
您可以使用 pyhtml2pdf 模块
Source 代码将 HTML 页面转换为 pdf
You can convert a HTML page to pdf by using the pyhtml2pdf module
Source for the code