Django is able to output PDF files dynamically using 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 customized PDFs for different purposes – say, for different users or different pieces of content.
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
Test your installation by importing it in the Python interactive interpreter:
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 API, like the
csv library acts on file-like objects, like Django’s
HttpResponse. Here’s a Hello World example:
The code and comments should be self-explanatory, but a few things deserve a mention:
- The response gets a special MIME type,
application/pdf. This tells browsers that the document is a PDF file, rather than an HTML file.
- The response gets an additional
Content-Dispositionheader, which contains the name of the PDF file. This filename is arbitrary: Call it whatever you want. It’ll be used by browsers in the Save as… dialogue, etc.
Content-Dispositionheader 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:
- Hooking into the ReportLab API is easy: Just pass
responseas the first argument to
Canvasclass expects a file-like object, and
HttpResponseobjects fit the bill.
- Note that all subsequent PDF-generation methods are called on the PDF object (in this case,
p) – not on
- Finally, it’s important to call
save()on the PDF file.
If you’re creating a complex PDF document with ReportLab, consider using the
io library as a temporary holding place for your PDF file. This library provides a file-like object interface that is particularly efficient. Here’s the above Hello World example rewritten to use
- PDFlib is another PDF-generation library that has Python bindings. To use it with Django, just use the same concepts explained in this article.
- Pisa XHTML2PDF is yet another PDF-generation library. Pisa ships with an example of how to integrate Pisa with Django.
- HTMLdoc is a command-line script that can convert HTML to PDF. It doesn’t have a Python interface, but you can escape out to the shell using
popenand retrieve the output in Python.
There’s a whole host of other types of content you can generate in Python. Here are a few more ideas and some pointers to libraries you could use to implement them:
- ZIP files: Python’s standard library ships with the
zipfilemodule, which can both read and write compressed ZIP files. You could use it to provide on-demand archives of a bunch of files, or perhaps compress large documents when requested. You could similarly produce TAR files using the standard library’s
- Dynamic images: The Python Imaging Library (PIL) is a fantastic toolkit for producing images (PNG, JPEG, GIF, and a whole lot more). You could use it to automatically scale down images into thumbnails,
composite multiple images into a single frame, or even do Web-based image processing.
- Plots and charts: There are a number of powerful Python plotting and charting libraries you could use to produce on-demand maps, charts, plots, and graphs. We can’t possibly list them all, so here are a couple of the highlights:
In general, any Python library capable of writing to a file can be hooked into Django. The possibilities are immense. Now that we’ve looked at the basics of generating non-HTML content, let’s step up a level of abstraction. Django ships with some pretty nifty built-in tools for generating some common types of non-HTML content.