outputting-pdf.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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: https://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. .. console::
  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.org/project/reportlab/
  26. .. _user guide: https://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.FileResponse`
  31. objects accept file-like objects.
  32. Here's a "Hello World" example::
  33. import io
  34. from django.http import FileResponse
  35. from reportlab.pdfgen import canvas
  36. def some_view(request):
  37. # Create a file-like buffer to receive PDF data.
  38. buffer = io.BytesIO()
  39. # Create the PDF object, using the buffer as its "file."
  40. p = canvas.Canvas(buffer)
  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. # FileResponse sets the Content-Disposition header so that browsers
  48. # present the option to save the file.
  49. return FileResponse(buffer, as_attachment=True, filename='hello.pdf')
  50. The code and comments should be self-explanatory, but a few things deserve a
  51. mention:
  52. * The response will automatically set the MIME type :mimetype:`application/pdf`
  53. based on the filename extension. This tells browsers that the document is a
  54. PDF file, rather than an HTML file or a generic `application/octet-stream`
  55. binary content.
  56. * When ``as_attachment=True`` is passed to ``FileResponse``, it sets the
  57. appropriate ``Content-Disposition`` header and that tells Web browsers to
  58. pop-up a dialog box prompting/confirming how to handle the document even if a
  59. default is set on the machine. If the ``as_attachment`` parameter is omitted,
  60. browsers will handle the PDF using whatever program/plugin they've been
  61. configured to use for PDFs.
  62. * You can provide an arbitrary ``filename`` parameter. It'll be used by browsers
  63. in the "Save as..." dialog.
  64. * Hooking into the ReportLab API is easy: The same buffer passed as the first
  65. argument to ``canvas.Canvas`` can be fed to the
  66. :class:`~django.http.FileResponse` class.
  67. * Note that all subsequent PDF-generation methods are called on the PDF
  68. object (in this case, ``p``) -- not on ``buffer``.
  69. * Finally, it's important to call ``showPage()`` and ``save()`` on the PDF
  70. file.
  71. .. note::
  72. ReportLab is not thread-safe. Some of our users have reported odd issues
  73. with building PDF-generating Django views that are accessed by many people
  74. at the same time.
  75. Other formats
  76. =============
  77. Notice that there isn't a lot in these examples that's PDF-specific -- just the
  78. bits using ``reportlab``. You can use a similar technique to generate any
  79. arbitrary format that you can find a Python library for. Also see
  80. :doc:`/howto/outputting-csv` for another example and some techniques you can use
  81. when generated text-based formats.
  82. .. seealso::
  83. Django Packages provides a `comparison of packages
  84. <https://djangopackages.org/grids/g/pdf/>`_ that help generate PDF files
  85. from Django.