2021-07-13 07:25:37 +00:00
|
|
|
=======================
|
|
|
|
How to create PDF files
|
|
|
|
=======================
|
2005-09-30 22:02:51 +00:00
|
|
|
|
|
|
|
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 15:17:22 +00:00
|
|
|
customized PDFs for different purposes -- say, for different users or different
|
2005-09-30 22:02:51 +00:00
|
|
|
pieces of content.
|
|
|
|
|
2006-02-18 16:45:36 +00:00
|
|
|
For example, Django was used at kusports.com_ to generate customized,
|
2005-09-30 22:02:51 +00:00
|
|
|
printer-friendly NCAA tournament brackets, as PDF files, for people
|
|
|
|
participating in a March Madness contest.
|
|
|
|
|
2023-05-17 10:13:10 +00:00
|
|
|
.. _ReportLab: https://docs.reportlab.com/
|
2023-11-28 16:57:48 +00:00
|
|
|
.. _kusports.com: https://www2.kusports.com/
|
2005-09-30 22:02:51 +00:00
|
|
|
|
|
|
|
Install ReportLab
|
|
|
|
=================
|
|
|
|
|
2023-04-16 23:14:09 +00:00
|
|
|
The ReportLab library is :pypi:`available on PyPI <reportlab>`. A `user guide`_
|
|
|
|
(not coincidentally, a PDF file) is also available for download.
|
2014-12-19 17:07:03 +00:00
|
|
|
You can install ReportLab with ``pip``:
|
2012-04-22 17:18:14 +00:00
|
|
|
|
2018-01-20 17:38:48 +00:00
|
|
|
.. console::
|
2012-04-22 17:18:14 +00:00
|
|
|
|
2019-04-14 15:02:36 +00:00
|
|
|
$ python -m pip install reportlab
|
2005-09-30 22:02:51 +00:00
|
|
|
|
2023-02-09 15:48:46 +00:00
|
|
|
Test your installation by importing it in the Python interactive interpreter:
|
|
|
|
|
|
|
|
.. code-block:: pycon
|
2005-09-30 22:02:51 +00:00
|
|
|
|
2005-09-30 22:05:44 +00:00
|
|
|
>>> import reportlab
|
2005-09-30 22:02:51 +00:00
|
|
|
|
|
|
|
If that command doesn't raise any errors, the installation worked.
|
|
|
|
|
2018-01-07 13:28:41 +00:00
|
|
|
.. _user guide: https://www.reportlab.com/docs/reportlab-userguide.pdf
|
2005-09-30 22:02:51 +00:00
|
|
|
|
|
|
|
Write your view
|
|
|
|
===============
|
|
|
|
|
|
|
|
The key to generating PDFs dynamically with Django is that the ReportLab API
|
2018-05-15 16:12:11 +00:00
|
|
|
acts on file-like objects, and Django's :class:`~django.http.FileResponse`
|
|
|
|
objects accept file-like objects.
|
2005-09-30 22:02:51 +00:00
|
|
|
|
|
|
|
Here's a "Hello World" example::
|
|
|
|
|
2018-05-15 16:12:11 +00:00
|
|
|
import io
|
|
|
|
from django.http import FileResponse
|
2018-05-12 17:37:42 +00:00
|
|
|
from reportlab.pdfgen import canvas
|
2005-09-30 22:02:51 +00:00
|
|
|
|
2023-02-28 19:53:28 +00:00
|
|
|
|
2005-09-30 22:02:51 +00:00
|
|
|
def some_view(request):
|
2018-05-15 16:12:11 +00:00
|
|
|
# Create a file-like buffer to receive PDF data.
|
|
|
|
buffer = io.BytesIO()
|
2005-09-30 22:02:51 +00:00
|
|
|
|
2018-05-15 16:12:11 +00:00
|
|
|
# Create the PDF object, using the buffer as its "file."
|
|
|
|
p = canvas.Canvas(buffer)
|
2005-09-30 22:02:51 +00:00
|
|
|
|
|
|
|
# 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()
|
2018-05-15 16:12:11 +00:00
|
|
|
|
|
|
|
# FileResponse sets the Content-Disposition header so that browsers
|
|
|
|
# present the option to save the file.
|
2019-08-17 07:20:36 +00:00
|
|
|
buffer.seek(0)
|
2023-02-28 19:53:28 +00:00
|
|
|
return FileResponse(buffer, as_attachment=True, filename="hello.pdf")
|
2005-09-30 22:02:51 +00:00
|
|
|
|
|
|
|
The code and comments should be self-explanatory, but a few things deserve a
|
|
|
|
mention:
|
|
|
|
|
2018-05-15 16:12:11 +00:00
|
|
|
* The response will automatically set the MIME type :mimetype:`application/pdf`
|
|
|
|
based on the filename extension. This tells browsers that the document is a
|
2020-03-31 08:37:38 +00:00
|
|
|
PDF file, rather than an HTML file or a generic
|
|
|
|
:mimetype:`application/octet-stream` binary content.
|
2005-09-30 22:02:51 +00:00
|
|
|
|
2018-05-15 16:12:11 +00:00
|
|
|
* When ``as_attachment=True`` is passed to ``FileResponse``, it sets the
|
2021-07-23 06:48:16 +00:00
|
|
|
appropriate ``Content-Disposition`` header and that tells web browsers to
|
2018-05-15 16:12:11 +00:00
|
|
|
pop-up a dialog box prompting/confirming how to handle the document even if a
|
|
|
|
default is set on the machine. If the ``as_attachment`` parameter is omitted,
|
|
|
|
browsers will handle the PDF using whatever program/plugin they've been
|
|
|
|
configured to use for PDFs.
|
2006-02-18 16:43:17 +00:00
|
|
|
|
2018-05-15 16:12:11 +00:00
|
|
|
* You can provide an arbitrary ``filename`` parameter. It'll be used by browsers
|
|
|
|
in the "Save as..." dialog.
|
2006-02-18 16:43:17 +00:00
|
|
|
|
2019-06-17 14:54:55 +00:00
|
|
|
* You can hook into the ReportLab API: The same buffer passed as the first
|
2018-05-15 16:12:11 +00:00
|
|
|
argument to ``canvas.Canvas`` can be fed to the
|
|
|
|
:class:`~django.http.FileResponse` class.
|
2005-09-30 22:02:51 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Note that all subsequent PDF-generation methods are called on the PDF
|
2018-05-15 16:12:11 +00:00
|
|
|
object (in this case, ``p``) -- not on ``buffer``.
|
2005-09-30 22:02:51 +00:00
|
|
|
|
2011-10-14 00:12:01 +00:00
|
|
|
* Finally, it's important to call ``showPage()`` and ``save()`` on the PDF
|
|
|
|
file.
|
2006-02-18 16:43:17 +00:00
|
|
|
|
2012-06-08 16:46:45 +00: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.
|
|
|
|
|
2008-08-23 22:25:40 +00: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-19 19:27:44 +00:00
|
|
|
:doc:`/howto/outputting-csv` for another example and some techniques you can use
|
2008-08-23 22:25:40 +00:00
|
|
|
when generated text-based formats.
|
2016-10-25 15:58:19 +00:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
|
|
Django Packages provides a `comparison of packages
|
|
|
|
<https://djangopackages.org/grids/g/pdf/>`_ that help generate PDF files
|
|
|
|
from Django.
|