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.