2
0

outputting-pdf.txt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. =======================
  2. How to create PDF files
  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://docs.reportlab.com/
  14. .. _kusports.com: https://www2.kusports.com/
  15. Install ReportLab
  16. =================
  17. The ReportLab library is :pypi:`available on PyPI <reportlab>`. A `user guide`_
  18. (not coincidentally, a PDF file) is also available for download.
  19. You can install ReportLab with ``pip``:
  20. .. console::
  21. $ python -m pip install reportlab
  22. Test your installation by importing it in the Python interactive interpreter:
  23. .. code-block:: pycon
  24. >>> import reportlab
  25. If that command doesn't raise any errors, the installation worked.
  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. buffer.seek(0)
  50. return FileResponse(buffer, as_attachment=True, filename="hello.pdf")
  51. The code and comments should be self-explanatory, but a few things deserve a
  52. mention:
  53. * The response will automatically set the MIME type :mimetype:`application/pdf`
  54. based on the filename extension. This tells browsers that the document is a
  55. PDF file, rather than an HTML file or a generic
  56. :mimetype:`application/octet-stream` binary content.
  57. * When ``as_attachment=True`` is passed to ``FileResponse``, it sets the
  58. appropriate ``Content-Disposition`` header and that tells web browsers to
  59. pop-up a dialog box prompting/confirming how to handle the document even if a
  60. default is set on the machine. If the ``as_attachment`` parameter is omitted,
  61. browsers will handle the PDF using whatever program/plugin they've been
  62. configured to use for PDFs.
  63. * You can provide an arbitrary ``filename`` parameter. It'll be used by browsers
  64. in the "Save as..." dialog.
  65. * You can hook into the ReportLab API: The same buffer passed as the first
  66. argument to ``canvas.Canvas`` can be fed to the
  67. :class:`~django.http.FileResponse` class.
  68. * Note that all subsequent PDF-generation methods are called on the PDF
  69. object (in this case, ``p``) -- not on ``buffer``.
  70. * Finally, it's important to call ``showPage()`` and ``save()`` on the PDF
  71. file.
  72. .. note::
  73. ReportLab is not thread-safe. Some of our users have reported odd issues
  74. with building PDF-generating Django views that are accessed by many people
  75. at the same time.
  76. Other formats
  77. =============
  78. Notice that there isn't a lot in these examples that's PDF-specific -- just the
  79. bits using ``reportlab``. You can use a similar technique to generate any
  80. arbitrary format that you can find a Python library for. Also see
  81. :doc:`/howto/outputting-csv` for another example and some techniques you can use
  82. when generated text-based formats.
  83. .. seealso::
  84. Django Packages provides a `comparison of packages
  85. <https://djangopackages.org/grids/g/pdf/>`_ that help generate PDF files
  86. from Django.