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

The advantage of generating PDF files dynamically is that you can createcustomized PDFs for different purposes – say, for different users or differentpieces of content.

For example, Django was used at kusports.com to generate customized,printer-friendly NCAA tournament brackets, as PDF files, for peopleparticipating in a March Madness contest.

Install ReportLab

The ReportLab library is available on PyPI. A user guide (notcoincidentally, a PDF file) is also available for download.You can install ReportLab with pip:

  1. $ python -m pip install reportlab
  1. ...\> py -m pip install reportlab

Test your installation by importing it in the Python interactive interpreter:

  1. >>> import reportlab

If that command doesn’t raise any errors, the installation worked.

Write your view

The key to generating PDFs dynamically with Django is that the ReportLab APIacts on file-like objects, and Django’s FileResponseobjects accept file-like objects.

Here’s a “Hello World” example:

  1. import io
  2. from django.http import FileResponse
  3. from reportlab.pdfgen import canvas
  4.  
  5. def some_view(request):
  6. # Create a file-like buffer to receive PDF data.
  7. buffer = io.BytesIO()
  8.  
  9. # Create the PDF object, using the buffer as its "file."
  10. p = canvas.Canvas(buffer)
  11.  
  12. # Draw things on the PDF. Here's where the PDF generation happens.
  13. # See the ReportLab documentation for the full list of functionality.
  14. p.drawString(100, 100, "Hello world.")
  15.  
  16. # Close the PDF object cleanly, and we're done.
  17. p.showPage()
  18. p.save()
  19.  
  20. # FileResponse sets the Content-Disposition header so that browsers
  21. # present the option to save the file.
  22. buffer.seek(0)
  23. return FileResponse(buffer, as_attachment=True, filename='hello.pdf')

The code and comments should be self-explanatory, but a few things deserve amention:

  • The response will automatically set the MIME type _application/pdf_based on the filename extension. This tells browsers that the document is aPDF file, rather than an HTML file or a generic _application/octet-stream_binary content.
  • When as_attachment=True is passed to FileResponse, it sets theappropriate Content-Disposition header and that tells Web browsers topop-up a dialog box prompting/confirming how to handle the document even if adefault is set on the machine. If the as_attachment parameter is omitted,browsers will handle the PDF using whatever program/plugin they’ve beenconfigured to use for PDFs.
  • You can provide an arbitrary filename parameter. It’ll be used by browsersin the “Save as…” dialog.
  • You can hook into the ReportLab API: The same buffer passed as the firstargument to canvas.Canvas can be fed to theFileResponse class.
  • Note that all subsequent PDF-generation methods are called on the PDFobject (in this case, p) – not on buffer.
  • Finally, it’s important to call showPage() and save() on the PDFfile.

Note

ReportLab is not thread-safe. Some of our users have reported odd issueswith building PDF-generating Django views that are accessed by many peopleat the same time.

Other formats

Notice that there isn’t a lot in these examples that’s PDF-specific – just thebits using reportlab. You can use a similar technique to generate anyarbitrary format that you can find a Python library for. Also seeOutputting CSV with Django for another example and some techniques you can usewhen generated text-based formats.

See also

Django Packages provides a comparison of packages that help generate PDF filesfrom Django.