【问题标题】:Convert html to pdf using Python/Flask使用 Python/Flask 将 html 转换为 pdf
【发布时间】:2015-03-25 18:51:38
【问题描述】:

我想使用 Python + Flask 从 html 生成 pdf 文件。为此,我使用 xhtml2pdf。这是我的代码:

def main():
    pdf = StringIO()
    pdf = create_pdf(render_template('cvTemplate.html', user=user))
    pdf_out = pdf.getvalue()
    response = make_response(pdf_out)
    return response

def create_pdf(pdf_data):
    pdf = StringIO()
    pisa.CreatePDF(StringIO(pdf_data.encode('utf-8')), pdf)
    return pdf

在这个代码文件中是动态生成的。但! xhtml2pdf 不支持 CSS 中的许多样式,因为正确标记页面存在这个大问题。我找到了另一种乐器(wkhtmltopdf)。但是当我写了这样的东西时:

pdf = StringIO()
data = render_template('cvTemplate1.html', user=user)
WKhtmlToPdf(data.encode('utf-8'), pdf)
return pdf

引发错误:

AttributeError: 'cStringIO.StringO' object has no attribute 'rfind'

我的问题是如何在 Flask 中使用 wkhtmltopdf(动态生成文件)将 html 转换为 pdf?

提前感谢您的回答。

【问题讨论】:

    标签: python pdf flask wkhtmltopdf xhtml2pdf


    【解决方案1】:

    页面需要渲染,可以使用pdfkit:

    https://pypi.python.org/pypi/pdfkit

    https://github.com/JazzCore/python-pdfkit

    文档中的示例。

    import pdfkit
    
    pdfkit.from_url('http://google.com', 'out.pdf')
    pdfkit.from_file('test.html', 'out.pdf')
    pdfkit.from_string('Hello!', 'out.pdf')  # Is your requirement?
    

    【讨论】:

    • 好的。此代码工作正常(不包括安装 wkhtmltopdf 的问题):rendered_template = render_template('template.html', user=user) render_template = render_template.encode('utf-8') pdf = pdfkit.from_string(rendered_template, False, css= './static/styles.css') return make_response(pdf) 谢谢!
    • 但需要安装wkhtmlpdf
    • 它在本地工作。但无法安装在 cpanel(实时服务器)中。如何在 cpanel 中安装 wkhtmlpdf
    【解决方案2】:

    您是否尝试过使用WeasyPrintFlask-WeasyPrint?他们的网站上有很好的例子,所以我不在这里复制它们。

    【讨论】:

    • 安装说明看起来很吓人,因为依赖包,但 pip install weasyprint 处理了一切。
    • WeasyPrint 无法处理 JavaScript。
    • WeasyPrint 支持 CSS2.1 但未提及 js 支持。所以如果需要js支持,截至目前,我想说pypuppeteer可能会有所帮助。
    【解决方案3】:

    Conversion in 3 Steps from Webpage/HTML to PDF

    第一步:下载库pdfkit

    $ pip install pdfkit
    

    第二步:下载wkhtmltopdf

    对于 Ubuntu/Debian:

    sudo apt-get install wkhtmltopdf
    

    对于 Windows:

    (a)下载链接:WKHTMLTOPDF

    (b)设置:环境变量中的PATH变量设置二进制文件夹。

    第 3 步:要下载的 Python 代码:

    (i) 已保存的 HTML 页面

    import pdfkit
    pdfkit.from_file('test.html', 'out.pdf')
    

    (ii) 通过网站 URL 转换

    import pdfkit
    pdfkit.from_url('https://www.google.co.in/','shaurya.pdf')
    

    (iii) 以 PDF 格式存储文本

    import pdfkit
    pdfkit.from_string('Shaurya Stackoverflow','SOF.pdf')
    

    【讨论】:

    • 除了安装和添加环境变量,我还得重启电脑才能正常工作。
    【解决方案4】:

    不确定这是否会帮助任何人,但我的问题是将 Bootstrap5 元素捕获为 pdf。 pdfkit 没有这样做,这是使用 html2image 和 PIL 在 Windows 上解决的方法

    from html2image import Html2Image
    from PIL import Image
    
    try:
       hti.screenshot(html_file=C:\yourfilepath\file.html, save_as="test.png")
    
    finally:
       image1 = Image.open(r'C:\yourfilepath\test.png')
       im1 = image1.convert('RGB')
       im1.save(r'C:\yourfilepath\newpdf.pdf')
    

    【讨论】:

      猜你喜欢
      • 2011-11-26
      • 2010-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-14
      • 2017-05-06
      • 2012-05-01
      • 1970-01-01
      相关资源
      最近更新 更多