简体   繁体   English

使用 Python/Flask 将 html 转换为 pdf

[英]Convert html to pdf using Python/Flask

I want to generate pdf file from html using Python + Flask. To do this, I use xhtml2pdf.我想使用 Python + Flask 从 html 生成 pdf 文件。为此,我使用 xhtml2pdf。 Here is my code:这是我的代码:

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

In this code file is generating on the fly.在此代码文件中是动态生成的。 BUT, xhtml2pdf doesn't support many styles in CSS. because of this big problem to mark page correctly.但是,xhtml2pdf 不支持 CSS 中的许多 styles。因为正确标记页面这个大问题。 I found another instrument(wkhtmltopdf): But when I wrote something like:我找到了另一种工具(wkhtmltopdf):但是当我写下类似的东西时:

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

Was raised error:引发错误:

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

And my question is how to convert html to pdf using wkhtmltopdf (with generating file on the fly) in Flask?我的问题是如何在 Flask 中使用 wkhtmltopdf(动态生成文件)将 html 转换为 pdf?

Thanks in advance for your answers.预先感谢您的回答。

The page need render, You can use pdfkit: 页面需要渲染,您可以使用pdfkit:

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

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

Example in document. 文档中的示例。

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?

Have you tried with Flask-WeasyPrint , which uses WeasyPrint ? 您是否尝试过使用WeasyPrint的 Flask- WeasyPrint There are good examples in their web sites so I don't replicate them here. 他们的网站上有很好的例子,所以我不在这里复制它们。

Conversion in 3 Steps from Webpage/HTML to PDF 从网页/ HTML到PDF的3个步骤转换

Step1: Download library pdfkit 第1下载库pdfkit

$ pip install pdfkit

Step2: Download wkhtmltopdf 第2下载wkhtmltopdf

For Ubuntu/Debian: 对于Ubuntu / Debian:

sudo apt-get install wkhtmltopdf

For Windows: 对于Windows:

(a)Download link: WKHTMLTOPDF (a)下载链接: WKHTMLTOPDF

(b)Set: PATH variable set binary folder in Environment variables. (b)设置:PATH变量设置环境变量中的二进制文件夹。

Step3: Code in Python to Download: Step3:下载Python代码:

(i) Already Saved HTML page (i)已保存的HTML页面

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

(ii) Convert by website URL (ii)通过网站URL转换

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

(iii) Store text in PDF (iii)以PDF格式存储文本

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

Not sure if this would assist anyone but my issue was capturing Bootstrap5 elements as a pdf.不确定这是否会帮助任何人,但我的问题是将 Bootstrap5 元素捕获为 pdf。 pdfkit did not do so and heres a work around on windows using html2image and PIL 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')

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

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