Creating PDF from HTML with Django and Reportlab

I am returning with a new question that I cannot answer by scratching my head all day on it.

I want to create a PDF file from a web page by clicking the "Download PDF" button. I tried several modules, including Reportlab and XHTML2PDF, but I cannot create any PDF file and not load it ... Here is what I did with Reportlab, following Render HTML to PDF on the Django website

- views.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 -

 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 -

 {% 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 handle?

+6
source share
3 answers

I would recommend using wkhtmltopdf.

The short answer? In Ubuntu, install the binary file:

 apt-get install wkhtmltopdf 

On CentOS / RedHat:

 yum install wkhtmltox-0.12.2.1_linux-centos6-amd64.rpm 

Then install the Python package:

 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 a detailed answer and details, I put together a blog post:

https://www.pyphilly.org/generating-pdf-markdown-or-html/

This should take care of creating the PDF; You will have to decide how you want to handle the download. Good luck

+7
source

If you say that you have problems creating your PDF file, I suggest you start with the example mentioned in this answer using Reportlab, xhtml2pdf with django -Easy-PDF. First send the PDF file in the browser, and then go to the link to download it.

0
source
 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')) 
-2
source

All Articles