Django PDF

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.

snippet
$ pip install reportlab

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 rookienerd". This library provides a canvas and tools that are used to generate customized PDF. See the example.

// views.py

snippet
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, rookienerd.")
    p.showPage()
    p.save()
    return response

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.

// urls.py

snippet
path('pdf',views.getpdf)

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.

Output:

django pdf

A PDF file is generated and ready to download. Download the file and open it, it shows the string message that we wrote.

django pdf 1

Apart from it, this library contains the lots of other methods to design and generate PDF dynamically.

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +