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. $ 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 HttpResponseobjects are file-like objects.

Here's a "Hello World" example:

  1. from django.http import HttpResponse
  2. from reportlab.pdfgen import canvas
  3.  
  4. def some_view(request):
  5. # Create the HttpResponse object with the appropriate PDF headers.
  6. response = HttpResponse(content_type='application/pdf')
  7. response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
  8.  
  9. # Create the PDF object, using the response object as its "file."
  10. p = canvas.Canvas(response)
  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. return response

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

  • The response gets a special MIME type, application/pdf. Thistells browsers that the document is a PDF file, rather than an HTML file.If you leave this off, browsers will probably interpret the output asHTML, which would result in ugly, scary gobbledygook in the browserwindow.

  • The response gets an additional Content-Disposition header, whichcontains the name of the PDF file. This filename is arbitrary: Call itwhatever you want. It'll be used by browsers in the "Save as…" dialog, etc.

  • The Content-Disposition header starts with 'attachment; ' in thisexample. This forces Web browsers to pop-up a dialog boxprompting/confirming how to handle the document even if a default is seton the machine. If you leave off 'attachment;', browsers will handlethe PDF using whatever program/plugin they've been configured to use forPDFs. Here's what that code would look like:

  1. response['Content-Disposition'] = 'filename="somefilename.pdf"'
  • Hooking into the ReportLab API is easy: Just pass response as thefirst argument to canvas.Canvas. The Canvas class expects afile-like object, and HttpResponse objects fit thebill.

  • Note that all subsequent PDF-generation methods are called on the PDFobject (in this case, p) — not on response.

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

Complex PDFs

If you're creating a complex PDF document with ReportLab, consider using theio library as a temporary holding place for your PDF file. Thislibrary provides a file-like object interface that is particularly efficient.Here's the above "Hello World" example rewritten to use io:

  1. from io import BytesIO
  2. from reportlab.pdfgen import canvas
  3. from django.http import HttpResponse
  4.  
  5. def some_view(request):
  6. # Create the HttpResponse object with the appropriate PDF headers.
  7. response = HttpResponse(content_type='application/pdf')
  8. response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
  9.  
  10. buffer = BytesIO()
  11.  
  12. # Create the PDF object, using the BytesIO object as its "file."
  13. p = canvas.Canvas(buffer)
  14.  
  15. # Draw things on the PDF. Here's where the PDF generation happens.
  16. # See the ReportLab documentation for the full list of functionality.
  17. p.drawString(100, 100, "Hello world.")
  18.  
  19. # Close the PDF object cleanly.
  20. p.showPage()
  21. p.save()
  22.  
  23. # Get the value of the BytesIO buffer and write it to the response.
  24. pdf = buffer.getvalue()
  25. buffer.close()
  26. response.write(pdf)
  27. return response

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.