使用 Python 将 HTML 转换为 PDF

发布于 2024-12-07 10:18:17 字数 323 浏览 2 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

归属感 2024-12-14 10:18:17

为 Django 配置 Pisa 不应该太难

网上确实有几个例子可以向您展示如何做到这一点,并且
解释如何链接到模板中的外部资源:

在您的情况下,您应该尝试中提到的链接回调函数第一篇博文:

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

对于较新的 Django 版本,您可能应该使用 STATIC_ROOT 而不是 MEDIA_ROOT

然后在您的渲染方法:

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

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:

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

Then use fetch resources accordingly in your render-method:

pdf = pisa.pisaDocument(StringIO.StringIO(
        html.encode("UTF-8")), 
        result, 
        link_callback=fetch_resources,
        encoding="utf-8")
寂寞美少年 2024-12-14 10:18:17

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

I suggest you to use pisa, pypdf and html5lib combination, it worked for me.

踏月而来 2024-12-14 10:18:17

一个可能但不太优雅的解决方案是运行一个小脚本,通过无头浏览器组件(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.

别低头,皇冠会掉 2024-12-14 10:18:17

您可以使用 pyhtml2pdf 模块

#if your using website URL
from pyhtml2pdf import converter
url = 'https://.....'
converter.convert(url, 'sample.pdf')

# if have the html file saved 
import os
from pyhtml2pdf import converter
path = os.path.abspath('abcd.html')
converter.convert(f'file:///{path}', 'sample.pdf') 

Source 代码将 HTML 页面转换为 pdf

You can convert a HTML page to pdf by using the pyhtml2pdf module

#if your using website URL
from pyhtml2pdf import converter
url = 'https://.....'
converter.convert(url, 'sample.pdf')

# if have the html file saved 
import os
from pyhtml2pdf import converter
path = os.path.abspath('abcd.html')
converter.convert(f'file:///{path}', 'sample.pdf') 

Source for the code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文