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.
|
|
|
|
|
|
|
|
.. _ReportLab: http://www.reportlab.org/rl_toolkit.html
|
2006-02-19 00:45:36 +08:00
|
|
|
.. _kusports.com: http://www.kusports.com/
|
2005-10-01 06:02:51 +08:00
|
|
|
|
|
|
|
Install ReportLab
|
|
|
|
=================
|
|
|
|
|
2005-10-01 06:07:19 +08:00
|
|
|
Download and install the ReportLab library from http://www.reportlab.org/downloads.html.
|
2005-10-01 06:02:51 +08:00
|
|
|
The `user guide`_ (not coincidentally, a PDF file) explains how to install it.
|
|
|
|
|
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.
|
|
|
|
|
2007-02-10 11:39:14 +08:00
|
|
|
.. _user guide: http://www.reportlab.com/docs/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
|
|
|
|
acts on file-like objects, and Django's ``HttpResponse`` objects are file-like
|
|
|
|
objects.
|
|
|
|
|
|
|
|
.. admonition:: Note
|
|
|
|
|
|
|
|
For more information on ``HttpResponse`` objects, see
|
|
|
|
`Request and response objects`_.
|
|
|
|
|
2007-01-25 04:08:47 +08:00
|
|
|
.. _Request and response objects: ../request_response/
|
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.
|
|
|
|
response = HttpResponse(mimetype='application/pdf')
|
|
|
|
response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
|
|
|
|
* The response gets a special 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.
|
|
|
|
|
|
|
|
* The response gets an additional ``Content-Disposition`` header, 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.
|
|
|
|
|
2006-02-19 00:43:17 +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::
|
|
|
|
|
|
|
|
response['Content-Disposition'] = 'filename=somefilename.pdf'
|
|
|
|
|
2005-10-01 06:02:51 +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 ``HttpResponse`` objects fit the bill.
|
|
|
|
|
|
|
|
* Note that all subsequent PDF-generation methods are called on the PDF
|
|
|
|
object (in this case, ``p``) -- not on ``response``.
|
|
|
|
|
|
|
|
* Finally, it's important to call ``showPage()`` and ``save()`` on the PDF
|
|
|
|
file.
|
2006-02-19 00:43:17 +08:00
|
|
|
|
|
|
|
Complex PDFs
|
|
|
|
============
|
|
|
|
|
|
|
|
If you're creating a complex PDF document with ReportLab, consider using the
|
|
|
|
cStringIO_ library as a temporary holding place for your PDF file. The
|
|
|
|
cStringIO library provides a file-like object interface that is particularly
|
|
|
|
efficient. Here's the above "Hello World" example rewritten to use
|
|
|
|
``cStringIO``::
|
|
|
|
|
|
|
|
from cStringIO import StringIO
|
|
|
|
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.
|
|
|
|
response = HttpResponse(mimetype='application/pdf')
|
|
|
|
response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'
|
|
|
|
|
2006-03-24 10:28:47 +08:00
|
|
|
buffer = StringIO()
|
2006-02-19 00:43:17 +08:00
|
|
|
|
|
|
|
# Create the PDF object, using the StringIO object as its "file."
|
|
|
|
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()
|
|
|
|
|
|
|
|
# Get the value of the StringIO buffer and write it to the response.
|
|
|
|
pdf = buffer.getvalue()
|
|
|
|
buffer.close()
|
|
|
|
response.write(pdf)
|
|
|
|
return response
|
|
|
|
|
2006-02-19 00:44:20 +08:00
|
|
|
.. _cStringIO: http://www.python.org/doc/current/lib/module-cStringIO.html
|
2006-02-19 00:43:17 +08:00
|
|
|
|
|
|
|
Further resources
|
|
|
|
=================
|
|
|
|
|
|
|
|
* PDFlib_ is another PDF-generation library that has Python bindings. To
|
|
|
|
use it with Django, just use the same concepts explained in this article.
|
2008-06-06 23:27:21 +08:00
|
|
|
* `Pisa HTML2PDF`_ is yet another PDF-generation library. Pisa ships with
|
|
|
|
an example of how to integrate Pisa with Django.
|
2006-02-19 00:43:17 +08:00
|
|
|
* 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 ``system`` or ``popen`` and retrieve the output in Python.
|
|
|
|
* `forge_fdf in Python`_ is a library that fills in PDF forms.
|
|
|
|
|
|
|
|
.. _PDFlib: http://www.pdflib.org/
|
2008-06-06 23:27:21 +08:00
|
|
|
.. _`Pisa HTML2PDF`: http://www.htmltopdf.org/
|
2006-02-19 00:43:17 +08:00
|
|
|
.. _HTMLdoc: http://www.htmldoc.org/
|
|
|
|
.. _forge_fdf in Python: http://www.accesspdf.com/article.php/20050421092951834
|