Django

Code

Changeset 2332

Show
Ignore:
Timestamp:
02/18/06 10:43:17 (3 years ago)
Author:
adrian
Message:

Rolled tips and doc improvements from Web-page comments into docs/outputting_pdf.txt. Thanks to various contributors.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/outputting_pdf.txt

    r772 r2332  
    8080      dialogue, etc. 
    8181 
     82    * The ``Content-Disposition`` header starts with ``'attachment; '`` in this 
     83      example. This forces Web browsers to pop-up a dialog box 
     84      prompting/confirming how to handle the document even if a default is set 
     85      on the machine. If you leave off ``'attachment;'``, browsers will handle 
     86      the PDF using whatever program/plugin they've been configured to use for 
     87      PDFs. Here's what that code would look like:: 
     88 
     89          response['Content-Disposition'] = 'filename=somefilename.pdf' 
     90 
    8291    * Hooking into the ReportLab API is easy: Just pass ``response`` as the 
    8392      first argument to ``canvas.Canvas``. The ``Canvas`` class expects a 
     
    8998    * Finally, it's important to call ``showPage()`` and ``save()`` on the PDF 
    9099      file. 
     100 
     101Complex PDFs 
     102============ 
     103 
     104If you're creating a complex PDF document with ReportLab, consider using the 
     105cStringIO_ library as a temporary holding place for your PDF file. The 
     106cStringIO library provides a file-like object interface that is particularly 
     107efficient. Here's the above "Hello World" example rewritten to use 
     108``cStringIO``:: 
     109 
     110    from cStringIO import StringIO 
     111    from reportlab.pdfgen import canvas 
     112    from django.utils.httpwrappers import HttpResponse 
     113 
     114    def some_view(request): 
     115        # Create the HttpResponse object with the appropriate PDF headers. 
     116        response = HttpResponse(mimetype='application/pdf') 
     117        response['Content-Disposition'] = 'attachment; filename=somefilename.pdf' 
     118 
     119        buffer = String() 
     120 
     121        # Create the PDF object, using the StringIO object as its "file." 
     122        p = canvas.Canvas(buffer) 
     123 
     124        # Draw things on the PDF. Here's where the PDF generation happens. 
     125        # See the ReportLab documentation for the full list of functionality. 
     126        p.drawString(100, 100, "Hello world.") 
     127 
     128        # Close the PDF object cleanly. 
     129        p.showPage() 
     130        p.save() 
     131 
     132        # Get the value of the StringIO buffer and write it to the response. 
     133        pdf = buffer.getvalue() 
     134        buffer.close() 
     135        response.write(pdf) 
     136        return response 
     137 
     138.. cStringIO: http://www.python.org/doc/current/lib/module-cStringIO.html 
     139 
     140Further resources 
     141================= 
     142 
     143    * PDFlib_ is another PDF-generation library that has Python bindings. To 
     144      use it with Django, just use the same concepts explained in this article. 
     145    * HTMLdoc_ is a command-line script that can convert HTML to PDF. It 
     146      doesn't have a Python interface, but you can escape out to the shell 
     147      using ``system`` or ``popen`` and retrieve the output in Python. 
     148    * `forge_fdf in Python`_ is a library that fills in PDF forms. 
     149 
     150.. _PDFlib: http://www.pdflib.org/ 
     151.. _HTMLdoc: http://www.htmldoc.org/ 
     152.. _forge_fdf in Python: http://www.accesspdf.com/article.php/20050421092951834