Here, we will learn how to design and generate PDF file using Django view. To generate PDF, we will use ReportLab Python PDF library that creates customized dynamic PDF.
It is an open source library and can be downloaded easily by using the following command in Ubuntu.
After installing, we can import it by import keyword in the view file.
Below is a simple PDF example, in which we are outputting a string message "Hello form javatpoint". This library provides a canvas and tools that are used to generate customized PDF. See the example.
First, provide MIME (content) type as application/pdf, so that output generates as PDF rather than HTML,
Set Content-Disposition in which provide header as attachment and output file name.
Pass response argument to the canvas and drawstring to write the string after that apply to the save() method and return response.
Set the above code in urls.py to call view function.
Run server and access this view on the browser that creates a pdf file. See the examples.
A PDF file is generated and ready to download. Download the file and open it, it shows the string message that we wrote.
Apart from it, this library contains the lots of other methods to design and generate PDF dynamically.