2005-10-01 06:02:51 +08:00
|
|
|
===========================
|
|
|
|
Outputting PDFs with Django
|
|
|
|
===========================
|
|
|
|
|
|
|
|
This document explains how to output PDF files dynamically using Django views.
|
|
|
|
This is made possible by the excellent, open-source ReportLab_ Python PDF
|
|
|
|
library.
|
|
|
|
|
|
|
|
The advantage of generating PDF files dynamically is that you can create
|
2005-10-04 23:17:22 +08:00
|
|
|
customized PDFs for different purposes -- say, for different users or different
|
2005-10-01 06:02:51 +08:00
|
|
|
pieces of content.
|
|
|
|
|
2006-02-19 00:45:36 +08:00
|
|
|
For example, Django was used at kusports.com_ to generate customized,
|
2005-10-01 06:02:51 +08:00
|
|
|
printer-friendly NCAA tournament brackets, as PDF files, for people
|
|
|
|
participating in a March Madness contest.
|
|
|
|
|
2018-01-07 21:28:41 +08:00
|
|
|
.. _ReportLab: https://www.reportlab.com/opensource/
|
2006-02-19 00:45:36 +08:00
|
|
|
.. _kusports.com: http://www.kusports.com/
|
2005-10-01 06:02:51 +08:00
|
|
|
|
|
|
|
Install ReportLab
|
|
|
|
=================
|
|
|
|
|
2014-12-20 01:07:03 +08:00
|
|
|
The ReportLab library is `available on PyPI`_. A `user guide`_ (not
|
|
|
|
coincidentally, a PDF file) is also available for download.
|
|
|
|
You can install ReportLab with ``pip``:
|
2012-04-23 01:18:14 +08:00
|
|
|
|
2018-01-21 01:38:48 +08:00
|
|
|
.. console::
|
2012-04-23 01:18:14 +08:00
|
|
|
|
2014-09-22 01:58:44 +08:00
|
|
|
$ pip install reportlab
|
2005-10-01 06:02:51 +08:00
|
|
|
|
2005-10-01 06:05:44 +08:00
|
|
|
Test your installation by importing it in the Python interactive interpreter::
|
2005-10-01 06:02:51 +08:00
|
|
|
|
2005-10-01 06:05:44 +08:00
|
|
|
>>> import reportlab
|
2005-10-01 06:02:51 +08:00
|
|
|
|
|
|
|
If that command doesn't raise any errors, the installation worked.
|
|
|
|
|
2018-04-18 06:19:29 +08:00
|
|
|
.. _available on PyPI: https://pypi.org/project/reportlab/
|
2018-01-07 21:28:41 +08:00
|
|
|
.. _user guide: https://www.reportlab.com/docs/reportlab-userguide.pdf
|
2005-10-01 06:02:51 +08:00
|
|
|
|
|
|
|
Write your view
|
|
|
|
===============
|
|
|
|
|
|
|
|
The key to generating PDFs dynamically with Django is that the ReportLab API
|
2008-08-24 06:25:40 +08:00
|
|
|
acts on file-like objects, and Django's :class:`~django.http.HttpResponse`
|
|
|
|
objects are file-like objects.
|
2005-10-01 06:02:51 +08:00
|
|
|
|
|
|
|
Here's a "Hello World" example::
|
|
|
|
|
|
|
|
from reportlab.pdfgen import canvas
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.http import HttpResponse
|
2005-10-01 06:02:51 +08:00
|
|
|
|
|
|
|
def some_view(request):
|
|
|
|
# Create the HttpResponse object with the appropriate PDF headers.
|
2013-01-31 20:39:29 +08:00
|
|
|
response = HttpResponse(content_type='application/pdf')
|
2012-10-04 02:43:36 +08:00
|
|
|
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
|
2005-10-01 06:02:51 +08:00
|
|
|
|
|
|
|
# Create the PDF object, using the response object as its "file."
|
|
|
|
p = canvas.Canvas(response)
|
|
|
|
|
|
|
|
# Draw things on the PDF. Here's where the PDF generation happens.
|
|
|
|
# See the ReportLab documentation for the full list of functionality.
|
|
|
|
p.drawString(100, 100, "Hello world.")
|
|
|
|
|
|
|
|
# Close the PDF object cleanly, and we're done.
|
|
|
|
p.showPage()
|
|
|
|
p.save()
|
|
|
|
return response
|
|
|
|
|
|
|
|
The code and comments should be self-explanatory, but a few things deserve a
|
|
|
|
mention:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The response gets a special MIME type, :mimetype:`application/pdf`. This
|
|
|
|
tells browsers that the document is a PDF file, rather than an HTML file.
|
|
|
|
If you leave this off, browsers will probably interpret the output as
|
|
|
|
HTML, which would result in ugly, scary gobbledygook in the browser
|
|
|
|
window.
|
2005-10-01 06:02:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The response gets an additional ``Content-Disposition`` header, which
|
|
|
|
contains the name of the PDF file. This filename is arbitrary: Call it
|
2015-08-02 05:56:04 +08:00
|
|
|
whatever you want. It'll be used by browsers in the "Save as..." dialog, etc.
|
2005-10-01 06:02:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* The ``Content-Disposition`` header starts with ``'attachment; '`` in this
|
|
|
|
example. This forces Web browsers to pop-up a dialog box
|
|
|
|
prompting/confirming how to handle the document even if a default is set
|
|
|
|
on the machine. If you leave off ``'attachment;'``, browsers will handle
|
|
|
|
the PDF using whatever program/plugin they've been configured to use for
|
|
|
|
PDFs. Here's what that code would look like::
|
2006-02-19 00:43:17 +08:00
|
|
|
|
2012-10-04 02:43:36 +08:00
|
|
|
response['Content-Disposition'] = 'filename="somefilename.pdf"'
|
2006-02-19 00:43:17 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Hooking into the ReportLab API is easy: Just pass ``response`` as the
|
|
|
|
first argument to ``canvas.Canvas``. The ``Canvas`` class expects a
|
|
|
|
file-like object, and :class:`~django.http.HttpResponse` objects fit the
|
|
|
|
bill.
|
2005-10-01 06:02:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Note that all subsequent PDF-generation methods are called on the PDF
|
|
|
|
object (in this case, ``p``) -- not on ``response``.
|
2005-10-01 06:02:51 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
* Finally, it's important to call ``showPage()`` and ``save()`` on the PDF
|
|
|
|
file.
|
2006-02-19 00:43:17 +08:00
|
|
|
|
2012-06-09 00:46:45 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
ReportLab is not thread-safe. Some of our users have reported odd issues
|
|
|
|
with building PDF-generating Django views that are accessed by many people
|
|
|
|
at the same time.
|
|
|
|
|
2006-02-19 00:43:17 +08:00
|
|
|
Complex PDFs
|
|
|
|
============
|
|
|
|
|
|
|
|
If you're creating a complex PDF document with ReportLab, consider using the
|
2012-05-06 01:47:03 +08:00
|
|
|
:mod:`io` library as a temporary holding place for your PDF file. This
|
2008-08-24 06:25:40 +08:00
|
|
|
library provides a file-like object interface that is particularly efficient.
|
2012-05-06 01:47:03 +08:00
|
|
|
Here's the above "Hello World" example rewritten to use :mod:`io`::
|
2006-02-19 00:43:17 +08:00
|
|
|
|
2012-05-06 01:47:03 +08:00
|
|
|
from io import BytesIO
|
2006-02-19 00:43:17 +08:00
|
|
|
from reportlab.pdfgen import canvas
|
2006-07-08 22:14:28 +08:00
|
|
|
from django.http import HttpResponse
|
2006-02-19 00:43:17 +08:00
|
|
|
|
|
|
|
def some_view(request):
|
|
|
|
# Create the HttpResponse object with the appropriate PDF headers.
|
2013-01-31 20:39:29 +08:00
|
|
|
response = HttpResponse(content_type='application/pdf')
|
2012-10-04 02:43:36 +08:00
|
|
|
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
|
2006-02-19 00:43:17 +08:00
|
|
|
|
2012-05-06 01:47:03 +08:00
|
|
|
buffer = BytesIO()
|
2006-02-19 00:43:17 +08:00
|
|
|
|
2012-05-06 01:47:03 +08:00
|
|
|
# Create the PDF object, using the BytesIO object as its "file."
|
2006-02-19 00:43:17 +08:00
|
|
|
p = canvas.Canvas(buffer)
|
|
|
|
|
|
|
|
# Draw things on the PDF. Here's where the PDF generation happens.
|
|
|
|
# See the ReportLab documentation for the full list of functionality.
|
|
|
|
p.drawString(100, 100, "Hello world.")
|
|
|
|
|
|
|
|
# Close the PDF object cleanly.
|
|
|
|
p.showPage()
|
|
|
|
p.save()
|
|
|
|
|
2012-05-06 01:47:03 +08:00
|
|
|
# Get the value of the BytesIO buffer and write it to the response.
|
2006-02-19 00:43:17 +08:00
|
|
|
pdf = buffer.getvalue()
|
|
|
|
buffer.close()
|
|
|
|
response.write(pdf)
|
|
|
|
return response
|
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
Other formats
|
|
|
|
=============
|
|
|
|
|
|
|
|
Notice that there isn't a lot in these examples that's PDF-specific -- just the
|
|
|
|
bits using ``reportlab``. You can use a similar technique to generate any
|
|
|
|
arbitrary format that you can find a Python library for. Also see
|
2010-08-20 03:27:44 +08:00
|
|
|
:doc:`/howto/outputting-csv` for another example and some techniques you can use
|
2008-08-24 06:25:40 +08:00
|
|
|
when generated text-based formats.
|
2016-10-25 23:58:19 +08:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
Django Packages provides a `comparison of packages
|
|
|
|
<https://djangopackages.org/grids/g/pdf/>`_ that help generate PDF files
|
|
|
|
from Django.
|