Django Return Pdf With Reportlab How to print a pdf with Django and return it from url - #dev #fingerfood #coding #django #reportlab


Django Return Pdf With Reportlab


2022-02-28 | #dev, #fingerfood, #coding, #django, #reportlab,
Reading time 2 minutes | Word count 288


How to print a pdf with Django and return it from url #dev #fingerfood #coding #django #reportlab #pdf #python
How to print a pdf with Django and return it from url #dev #fingerfood #coding #django #reportlab #pdf #python

Sometime you need to print a pdf from your site or your web app with some custom data. In Python you can make pdf with ReportLab12 and some other module but I LOVE ReportLab3.

What do I need?

You only need Django and ReportLab

pip install django
pip install reportlab

Write the pdf

When you have the requirements you can write the code for the pdf. For example this is a example of a class for printing A6 pdf

from reportlab.lib.pagesizes import A6, landscape
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Paragraph
from reportlab.platypus import SimpleDocTemplate

class CustomDocTemplate(SimpleDocTemplate):
    def __init__(
        self,
        filename,
        right_margin=7,
        left_margin=7,
        top_margin=7,
        bottom_margin=7,
        pagesize=landscape(A6),
        **kw,
    ):
        super().__init__(filename, **kw)
        self.rightMargin = right_margin
        self.leftMargin = left_margin
        self.topMargin = top_margin
        self.bottomMargin = bottom_margin
        self.pagesize = pagesize


class A6Printer:
    def get_pdf(self, buffer):
        self.buffer = buffer
        styles = getSampleStyleSheet()
        doc = CustomDocTemplate(buffer)
        elements = []
        elements.append(Paragraph("Title of the PDF", style=styles["Heading2"]))
        elements.append(Paragraph("Heading", style=styles["Heading4"]))
        elements.append(Paragraph("Text inside the pdf", style=styles["BodyText"]))
        doc.build(elements)
        pdf = buffer.getvalue()
        buffer.close()
        return pdf

Make the view

Now we need a view for Django. Usualy I write ClassView so this is an example for the printer define in the last example.

from django.views import View


class A6View(View):
    def get(self, request, *args, **kwargs):
        response = HttpResponse(content_type="application/pdf")
        response["Content-Disposition"] = f'attachment; filename="Example.pdf"'
        a6p = A6Printer()
        pdf = a6p.get_pdf(BytesIO())
        response.write(pdf)
        return response

With this ClassView you can download a generated pdf with static data, dinamic data or some static and some dynamic data. I am working on more post about Reportlab so if you have some suggestion or question Tweet me. Thanks


  1. Official site of ReportLab  ↩︎

  2. Famous library for python. I wrote an article about it some time ago Letterhead With ReportLab  ↩︎

  3. You can read more ReportLab: PDF Processing with Python  ↩︎

If you liked this article,

please share it on

or webmention it