outputting-pdf.txt 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. .. _howto-outputting-pdf:
  2. ===========================
  3. Outputting PDFs with Django
  4. ===========================
  5. This document explains how to output PDF files dynamically using Django views.
  6. This is made possible by the excellent, open-source ReportLab_ Python PDF
  7. library.
  8. The advantage of generating PDF files dynamically is that you can create
  9. customized PDFs for different purposes -- say, for different users or different
  10. pieces of content.
  11. For example, Django was used at kusports.com_ to generate customized,
  12. printer-friendly NCAA tournament brackets, as PDF files, for people
  13. participating in a March Madness contest.
  14. .. _ReportLab: http://www.reportlab.org/rl_toolkit.html
  15. .. _kusports.com: http://www.kusports.com/
  16. Install ReportLab
  17. =================
  18. Download and install the ReportLab library from http://www.reportlab.org/downloads.html.
  19. The `user guide`_ (not coincidentally, a PDF file) explains how to install it.
  20. Test your installation by importing it in the Python interactive interpreter::
  21. >>> import reportlab
  22. If that command doesn't raise any errors, the installation worked.
  23. .. _user guide: http://www.reportlab.com/docs/reportlab-userguide.pdf
  24. Write your view
  25. ===============
  26. The key to generating PDFs dynamically with Django is that the ReportLab API
  27. acts on file-like objects, and Django's :class:`~django.http.HttpResponse`
  28. objects are file-like objects.
  29. Here's a "Hello World" example::
  30. from reportlab.pdfgen import canvas
  31. from django.http import HttpResponse
  32. def some_view(request):
  33. # Create the HttpResponse object with the appropriate PDF headers.
  34. response = HttpResponse(mimetype='application/pdf')
  35. response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'
  36. # Create the PDF object, using the response object as its "file."
  37. p = canvas.Canvas(response)
  38. # Draw things on the PDF. Here's where the PDF generation happens.
  39. # See the ReportLab documentation for the full list of functionality.
  40. p.drawString(100, 100, "Hello world.")
  41. # Close the PDF object cleanly, and we're done.
  42. p.showPage()
  43. p.save()
  44. return response
  45. The code and comments should be self-explanatory, but a few things deserve a
  46. mention:
  47. * The response gets a special MIME type, ``application/pdf``. This tells
  48. browsers that the document is a PDF file, rather than an HTML file. If
  49. you leave this off, browsers will probably interpret the output as HTML,
  50. which would result in ugly, scary gobbledygook in the browser window.
  51. * The response gets an additional ``Content-Disposition`` header, which
  52. contains the name of the PDF file. This filename is arbitrary: Call it
  53. whatever you want. It'll be used by browsers in the "Save as..."
  54. dialogue, etc.
  55. * The ``Content-Disposition`` header starts with ``'attachment; '`` in this
  56. example. This forces Web browsers to pop-up a dialog box
  57. prompting/confirming how to handle the document even if a default is set
  58. on the machine. If you leave off ``'attachment;'``, browsers will handle
  59. the PDF using whatever program/plugin they've been configured to use for
  60. PDFs. Here's what that code would look like::
  61. response['Content-Disposition'] = 'filename=somefilename.pdf'
  62. * Hooking into the ReportLab API is easy: Just pass ``response`` as the
  63. first argument to ``canvas.Canvas``. The ``Canvas`` class expects a
  64. file-like object, and :class:`~django.http.HttpResponse` objects fit the
  65. bill.
  66. * Note that all subsequent PDF-generation methods are called on the PDF
  67. object (in this case, ``p``) -- not on ``response``.
  68. * Finally, it's important to call ``showPage()`` and ``save()`` on the PDF
  69. file.
  70. Complex PDFs
  71. ============
  72. If you're creating a complex PDF document with ReportLab, consider using the
  73. cStringIO_ library as a temporary holding place for your PDF file. The cStringIO
  74. library provides a file-like object interface that is particularly efficient.
  75. Here's the above "Hello World" example rewritten to use ``cStringIO``::
  76. from cStringIO import StringIO
  77. from reportlab.pdfgen import canvas
  78. from django.http import HttpResponse
  79. def some_view(request):
  80. # Create the HttpResponse object with the appropriate PDF headers.
  81. response = HttpResponse(mimetype='application/pdf')
  82. response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'
  83. buffer = StringIO()
  84. # Create the PDF object, using the StringIO object as its "file."
  85. p = canvas.Canvas(buffer)
  86. # Draw things on the PDF. Here's where the PDF generation happens.
  87. # See the ReportLab documentation for the full list of functionality.
  88. p.drawString(100, 100, "Hello world.")
  89. # Close the PDF object cleanly.
  90. p.showPage()
  91. p.save()
  92. # Get the value of the StringIO buffer and write it to the response.
  93. pdf = buffer.getvalue()
  94. buffer.close()
  95. response.write(pdf)
  96. return response
  97. .. _cStringIO: http://docs.python.org/library/stringio.html#module-cStringIO
  98. Further resources
  99. =================
  100. * PDFlib_ is another PDF-generation library that has Python bindings. To
  101. use it with Django, just use the same concepts explained in this article.
  102. * `Pisa HTML2PDF`_ is yet another PDF-generation library. Pisa ships with
  103. an example of how to integrate Pisa with Django.
  104. * HTMLdoc_ is a command-line script that can convert HTML to PDF. It
  105. doesn't have a Python interface, but you can escape out to the shell
  106. using ``system`` or ``popen`` and retrieve the output in Python.
  107. * `forge_fdf in Python`_ is a library that fills in PDF forms.
  108. .. _PDFlib: http://www.pdflib.org/
  109. .. _`Pisa HTML2PDF`: http://www.htmltopdf.org/
  110. .. _HTMLdoc: http://www.htmldoc.org/
  111. .. _forge_fdf in Python: http://www.accesspdf.com/article.php/20050421092951834
  112. Other formats
  113. =============
  114. Notice that there isn't a lot in these examples that's PDF-specific -- just the
  115. bits using ``reportlab``. You can use a similar technique to generate any
  116. arbitrary format that you can find a Python library for. Also see
  117. :ref:`howto-outputting-csv` for another example and some techniques you can use
  118. when generated text-based formats.