Generate PDF in Django (1)

Source: Internet
Author: User



Requirement Description:
A website has an agreement with its users. Each agreement contains unique user-related information, and a PDF file must be generated and archived. The PDF file must contain enterprise LOGO, text description, and other information. The display format is similar to that of a word file. The layout is exquisite and the images are illustrated. In the following blog posts, I will record the entire PDF generation process.


Technical Research:
First of all, through Django official documentation to find the relevant modules of wood, very excited and a little lost (module address: https://docs.djangoproject.com/en/1.4/howto/outputting-pdf ). I'm excited that Django is considerate and provides users with a module for generating PDF files. What is lost is that what it provides cannot meet the existing needs. As a result, I found reportlab and found that it is indeed a very powerful library that can almost achieve the desired results.

Project preparation:
There are three ways to generate and save PDF files:
Method 1: use the relevant reportlab package to draw and save images directly with canvas. This method is the most primitive and troublesome to use. Different PDF documents need to be painted separately. In addition, you also need to have a detailed understanding of the API provided by reportlab. For each element to be drawn in a PDF file, whether it is text, straight line, or image, you need to manually color and precisely locate the image, operations are cumbersome, and the Code cannot be reused, which increases development and maintenance costs.
Method 2: The reportlab package is still used. The Report Markup Language (RML) is used to compile the file template (similar to the HTML page) for generating PDF files, we only need to provide different Report Templates. after implementing the tool class, we can call them directly, which is convenient and easy to maintain. However, the prerequisite is that you must be familiar with and understand the RML language.
Method 3: generate PDF directly through HTML, similar to method 2. render the template and generate a PDF file through style and data.

I personally think RML is more professional, because it is used to generate reports and has rich presentation forms, ranging from text and images, large to flowcharts, pie charts, bar charts, and graphs can be displayed.

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.