Django Return Pdf With Reportlab

Fundor333 | | Reading time 2 minutes | Word count 287

#####


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  ↩︎

This post is part of the Django Tricks series
  1. Django Rest Framework: Multiple post
  2. Make a Excel Django combo
  3. Django With Barcode and Qrcode
  4. Django Return Pdf With Reportlab
  5. Django List View With Show More
  6. Message and Allert With Django and Boostrap
  7. Csv From Django
This post is part of the Printing With ReportLab series
  1. Letterhead With ReportLab
  2. Django Return Pdf With Reportlab

Comments

To reply to this post, you can send a Webmention or you can toot me at [email protected]
You mentioned this post on your site? Send a Webmention


See Also