简体   繁体   English

使用Python将HTML转换为PDF

[英]Convert HTML into PDF using Python

I am trying to convert HTML into a PDF document in Django and haven't been successful. 我试图将HTML转换为Django中的PDF文档并且没有成功。

I have tried using wkhtmltopdf 0.9.9, however Apache throws an error that wkhtmltopdf cannot connect to server. 我尝试过使用wkhtmltopdf 0.9.9,但是Apache抛出了wkhtmltopdf无法连接到服务器的错误。 When I use wkhtmltopdf directly, it runs perfectly fine and converts the HTML into a PDF document. 当我直接使用wkhtmltopdf时,它运行得很好并将HTML转换为PDF文档。

I have also tried using unoconv, however the rendered PDF file doesn't have any CSS applied to it. 我也尝试使用unoconv,但渲染的PDF文件没有应用任何CSS。 I have also tried using xhtml2pdf. 我也尝试过使用xhtml2pdf。 Again I am facing same issue; 我再次面临同样的问题; the rendered PDF file doesn't have any CSS styling applied. 渲染的PDF文件没有应用任何CSS样式。 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 如果您需要更多信息,请与我们联系

Configuring Pisa for Django shouldn't be too hard . 为Django配置Pisa应该不会太难

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: 在你的情况下,你应该尝试在第一篇博文中提到的链接回调函数:

def fetch_resources(uri, rel):
    """
    Callback to allow pisa/reportlab to retrieve Images,Stylesheets, etc.
    `uri` is the href attribute from the html link element.
    `rel` gives a relative path, but it's not used here.

    """
    path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ""))
    return path

For newer Django-Version you probably should use STATIC_ROOT instead of MEDIA_ROOT 对于较新的Django-Version,您可能应该使用STATIC_ROOT而不是MEDIA_ROOT

Then use fetch resources accordingly in your render-method: 然后在render-method中相应地使用fetch resources

pdf = pisa.pisaDocument(StringIO.StringIO(
        html.encode("UTF-8")), 
        result, 
        link_callback=fetch_resources,
        encoding="utf-8")

我建议你使用pisa,pypdf和html5lib组合,它对我有用。

一个可能但不那么优雅的解决方案是运行一个小脚本,通过无头浏览器组件(Linux上的webkit / xvfb)呈现html,然后将其保存为pdf。

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

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