[英]Generate PDF from HTML using Django and Reportlab
I am coming back with a new question which I am unable to answer, having scratched my head the whole day on it.我带着一个我无法回答的新问题回来了,我整天都在挠头。
I want to generate a PDF from a webpage by clicking on a "Download PDF" button.我想通过单击“下载 PDF”按钮从网页生成 PDF。 I tried several modules including Reportlab and XHTML2PDF but I am not able to generate any pdf nor downloading it... Here is what I did with Reportlab, following Render HTML to PDF in Django site
我尝试了几个模块,包括 Reportlab 和 XHTML2PDF,但我无法生成任何 pdf 或下载它......这是我用 Reportlab 所做的,遵循Django 站点中的 Render HTML to PDF
- - views.py - - - - 视图.py - -
import cStringIO as StringIO
import ho.pisa as pisa
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
from cgi import escape
def index_data(request):
#Code to generate data
return render(request, "analytics/stat.html", locals())
return render_to_pdf(
'analytics/stat.html',
{
'pagesize':'A4',
'mylist': results,
}
)
def render_to_pdf(template_src, context_dict):
template = get_template(template_src)
context = Context(context_dict)
html = template.render(context)
result = StringIO.StringIO()
pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return HttpResponse('We had some errors<pre>%s</pre>' % escape(html))
- - urls.py - - - - urls.py - -
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', "analytics.views.main_page", name="main_page"),
url(r'^portail/$', "analytics.views.index_data", name="index"),
url(r'^generate_pdf/$', "analytics.views.GroupPDFGenerate.as_view()", name="generate_pdf")
] ]
- - Template analytics/stat.html - - - - 模板分析/stat.html - -
{% extends "analytics/layout.html" %}
{% block title %}
Audience
{% endblock title %}
{% block head %}
# Script to generate google charts
{% endblock head %}
{% block body %}
<div class="page-header">
<h1 align="center"> Audience </h1>
</div>
<div class="row">
<div class="col-md-1">
<h3 align="center"><a href="/logout/">Logout</a></h3>
<h3 align="center"><a href="statistiques.pdf">Download pdf</a></h3>
</div>
</div>
{% endblock %}
Also, is there a better module to process ?另外,是否有更好的模块来处理?
Since this answer is over half a decade old, some new solutions have become available.由于这个答案已有五年多的历史,因此一些新的解决方案已经可用。 These days, I tend to use WeasyPrint, which has the additional benefit of being BSD licensed instead of LGPL.
这些天,我倾向于使用 WeasyPrint,它具有获得 BSD 许可而不是 LGPL 的额外好处。 It is a tad slower, however.
然而,它有点慢。
https://weasyprint.org/ https://weasyprint.org/
I'd recommend using wkhtmltopdf.我建议使用 wkhtmltopdf。
The short answer?简短的回答? On Ubuntu, install a binary:
在 Ubuntu 上,安装一个二进制文件:
apt-get install wkhtmltopdf
On CentOS / RedHat:在 CentOS / RedHat 上:
yum install wkhtmltox-0.12.2.1_linux-centos6-amd64.rpm
Then pip install a Python package:然后 pip 安装一个 Python 包:
pip install pdfkit
Then the code:然后代码:
import pdfkit
input_filename = 'README.html'
output_filename = 'README.pdf'
with open(input_filename, 'r') as f:
html_text = f.read()
pdfkit.from_string(html_text, output_filename)
For the long answer and details, I put together a blog post:对于长答案和细节,我整理了一篇博文:
https://www.pyphilly.org/generating-pdf-markdown-or-html/ https://www.pyphilly.org/generating-pdf-markdown-or-html/
That should take care of the PDF creation;这应该负责创建 PDF; you'll have to decide how you want to handle the download.
您必须决定如何处理下载。 Good luck!
祝你好运!
If you say you are having problems even generating your PDF, I suggest you start by looking over the example I mentioned in this answer of using Reportlab, xhtml2pdf with django-easy-pdf.如果您说即使生成 PDF 也遇到问题,我建议您首先查看我在这个使用 Reportlab、xhtml2pdf 和 django-easy-pdf 的答案中提到的示例。 Get the PDF to render in the browser first and then move on to getting a link for downloading it.
首先获取要在浏览器中呈现的 PDF,然后继续获取下载链接。
from easy_pdf.rendering import render_to_pdf
def pdf(request):
with open('example.pdf', 'wb') as f2:
f2.write(render_to_pdf('you template', {'user': request.user}, encoding=u'utf-8'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.