We can use the ReportLab Python PDF library to generate PDF that creates customized dynamic PDF. It is an open source library and can be downloaded easily by using the following command in Ubuntu.
$ pip install reportlab
After installing successfully, we can import it by import keyword in the view file.// views.py
from reportlab.pdfgen import canvas from django.http import HttpResponse def getpdf(request): response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="file.pdf"' p = canvas.Canvas(response) p.setFont("Times-Roman", 55) p.drawString(100,700, "Hello, Javatpoint.") p.showPage() p.save() return response
First we provide MIME type as application/pdf so that the output generates as PDF rather than HTML.
Now, 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.// urls.py
We will set the above code in urls.py to call view function.
Now Run the server and access this view on the browser that creates a pdf file.
A PDF file will be generated and ready to download. Download this file and open it, it will show the string message that we wrote.