outputting-pdf.txt 6.1 KB

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