使用 Python/Flask 将 html 转换为 pdf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28165704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert html to pdf using Python/Flask
提问by Dmitry_Mahrachev
I want to generate pdf file from html using Python + Flask. To do this, I use xhtml2pdf. Here is my code:
我想使用 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
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. I found another instrument(wkhtmltopdf). But when I wrote something like:
在这个代码文件中是动态生成的。但!xhtml2pdf 不支持 CSS 中的许多样式,因为正确标记页面这个大问题。我找到了另一种乐器(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.
预先感谢您的回答。
采纳答案by Quan Brew
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?
回答by chfw
Have you tried with Flask-WeasyPrint, which uses WeasyPrint? There are good examples in their web sites so I don't replicate them here.
您是否尝试过使用WeasyPrint 的Flask- WeasyPrint?他们的网站上有很好的例子,所以我不在这里复制它们。
回答by shaurya uppal
Conversion in 3 Steps from Webpage/HTML to PDF
Step1:Download library pdfkit
Step1:下载库pdfkit
$ pip install pdfkit
Step2:Download wkhtmltopdf
Step2:下载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)Set: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')