Tutorials for generating PDF files with the Python Django framework

Source: Internet
Author: User
Portable Document Format (PDF) is a format developed by Adobe that is used primarily to render printable documents that contain pixel-perfect formats, embedded fonts, and 2D vector images. You can think of a PDF document as the digital equivalent of a printed document; Indeed, PDFs is often used in distributing documents for the purpose of printing them.

The easy-to-use Python and Django-generated PDF documents need to be attributed to an excellent open source library, Reportlab (http://www.reportlab.org/rl_toolkit.html). The benefit of dynamically generating PDF files is that in different situations, such as different users or different content, you can generate different PDF files on demand. The advantage of generating PDF files dynamically is so can create customized PDFs for different purposes say, for D Ifferent users or different pieces of content.

The following example uses Django and Reportlab to generate a personalized printable NCAA schedule table (tournament brackets) on kusports.com.
Installing Reportlab

Before you generate a PDF file, you need to install the Reportlab library. This is usually a very simple process: its usually simple:just download and install the library from http://www.reportlab.org/downloads.html.

Note

If you are using some new Linux distributions, you can check the package management software before installing. Reportlab is added to most of the package repositories.

For example, if you use the (outstanding) Ubuntu release, you simply need a simple apt-get install Python-reportlab line command to complete the installation.

The manual (original only in PDF format) can be downloaded from http://www.reportlab.org/rsrc/userguide.pdf, which contains some additional installation guides.

Import this package in a Python interactive environment to check if the installation was successful.

>>> Import Reportlab

If there is no error in that command, the installation is successful.
Writing views

Like CSV, it's easy to generate PDF files dynamically by Django because the Reportlab API can also use similar file objects.

Here is an example of Hello world:

From Reportlab.pdfgen import canvasfrom django.http import httpresponsedef hello_pdf (Request):  # Create the HttpResponse object with the appropriate PDF headers.  Response = HttpResponse (mimetype= ' application/pdf ')  response[' content-disposition '] = ' attachment; filename= Hello.pdf '  # Create the PDF object, using the response object as its "file."  p = Canvas. Canvas (response)  # Draw things on the PDF. Here's where the PDF generation happens.  # See the Reportlab documentation for the full list of functionality.  P.drawstring (+, "Hello world.")  # Close The PDF object cleanly, and we ' re done.  P.showpage ()  p.save ()  return response

The following points need to be noted:

The MIME type we use here is application/pdf. This tells the browser that the document is a PDF document, not an HTML document. If this argument is omitted, the browser may look at the file as an HTML document, which will cause strange text to appear in the browser's window. If you leave off this information, browsers'll probably interpret the response as HTML, which'll result in scary gobbl Edygook in the browser window.

Using the Reportlab API is simple: You only need to use the response object as a canvas. The first parameter of a Canvas is passed in.

All subsequent PDF generation methods need to be called by the PDF object (in this case p), not the response object.

Finally you need to call the ShowPage () and Save () method on the PDF file (otherwise you will get a corrupted PDF file).

A complex PDF file

If you are creating a complex PDF document (or any large block of data), use the Cstringio library to store the temporary generated PDF file. Cstringio provides an interface for similar file objects written in C, which makes the system the most efficient.

Here is an example of the Hello world rewritten with Cstringio:

From Cstringio import stringiofrom reportlab.pdfgen import canvasfrom django.http import httpresponsedef hello_pdf ( Request):  # Create The HttpResponse object with the appropriate PDF headers.  Response = HttpResponse (mimetype= ' application/pdf ')  response[' content-disposition '] = ' attachment; filename= Hello.pdf '  temp = Stringio ()  # Create The PDF object, using the Stringio object as its "file."  p = Canvas. Canvas (temp)  # Draw things on the PDF. Here's where the PDF generation happens.  # See the Reportlab documentation for the full list of functionality.  P.drawstring (+, "Hello world.")  # Close the PDF object cleanly.  P.showpage ()  p.save ()  # Get The value of the Stringio buffer and write it to the response.  Response.Write (Temp.getvalue ())  return response

Other possibilities.

There are many other types of content that can be generated using Python, and the following are some other ideas and libraries that you can use to implement them. Here is a few more ideas and some pointers to libraries your could use to implement them:

ZIP file: The Python standard library contains the ZipFile module, which can read and write compressed zip files. It can be used to generate compressed packages of files on demand, or to compress large documents when needed. If it is a TAR file, you can use the standard library Tarfile module.

Animated images: The Python image processing library (PIL; http://www.pythonware.com/products/pil/) is a great tool for generating images (PNG, JPEG, GIF, and many other formats). It can be used to automatically generate thumbnails for pictures, compress multiple images into a separate frame, or do Web-based picture processing.

Chart: Python has a number of great and powerful chart libraries for charting, on-demand maps, tables, and more. We can't list them all, so here's a list of the leaders.

Matplotlib (http://matplotlib.sourceforge.net/) can be used to generate high-quality diagrams that are typically generated by MATLAB or Mathematica.

Pygraphviz (Https://networkx.lanl.gov/wiki/pygraphviz) is a Python interface for a Graphviz graphical layout tool (http://graphviz.org/). can be used to generate structured charts and networks.

In summary, all libraries that can write files can be used concurrently with Django. The possibilities is immense.

We've learned the basics of generating "non-HTML" content, so let's take a closer look. Django has a number of built-in tools for generating a variety of "non-HTML" content.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.