Case Study: Generating a Receipt PDF Using Python’s ReportLab

In this case study, we will explore a Python script that uses the ReportLab library to generate a PDF receipt for a fictional entity called “Lingaraj-Techhub.” The receipt includes details about transactions, subscriptions, and prices. The case study will break down the code and explain its key components.

Problem Statement

The goal is to create a well-structured PDF receipt with a title, a table of transaction details, and some basic formatting using the ReportLab library.

Solution Overview

Imports and Data

The script begins by importing necessary modules from ReportLab. It also defines a sample dataset named DATA, which includes transaction information.

Creating a PDF

  1. A PDF document template is created using SimpleDocTemplate, specifying the output file as “receipt.pdf” and the page size as A4.
  2. The script fetches a sample stylesheet for styling the document.

Styling the Title

  1. The title style is defined by fetching the “Heading1” style from the sample stylesheet.
  2. The alignment of the title is set to center (1).
  3. A paragraph named title is created with the title text “Lingaraj-Techhub” and the defined title style.

Styling the Table

  1. A TableStyle object is created to define the styles for the table.
  2. Various styles are applied to the table, including adding a border, gridlines, background colors, text color, and center alignment.
  3. The style is applied to the table object, which contains the transaction data.

Building the PDF

Finally, the PDF is built using the pdf.build() method, which combines the title and table to create the actual PDF document.

from reportlab.platypus import SimpleDocTemplate, Table, Paragraph, TableStyle
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet
  
DATA = [
    [ "Date" , "Name", "Subscription", "Price (Rs.)" ],
    [
        "16/11/2020",
        "Full Stack Development with React & Node JS - Live",
        "Lifetime",
        "10,999.00/-",
    ],
    [ "16/11/2020", "Lingaraj Classes: Live Session", "6 months", "9,999.00/-"],
    [ "Sub Total", "", "", "20,998.00/-"],  # Corrected the subtotal value
    [ "Discount", "", "", "-3,000.00/-"],
    [ "Total", "", "", "17,998.00/-"],
]
  
# creating a Base Document Template of page size A4
pdf = SimpleDocTemplate( "receipt.pdf" , pagesize = A4 )
  
# standard stylesheet defined within reportlab itself
styles = getSampleStyleSheet()
  
# fetching the style of Top-level heading (Heading1)
title_style = styles[ "Heading1" ]
  
# 0: left, 1: center, 2: right
title_style.alignment = 1
  
# creating the paragraph with 
# the heading text and passing the styles of it
title = Paragraph( "Lingaraj-Techhub" , title_style )
  
# creates a Table Style object and in it,
# defines the styles row-wise
# the tuples which look like coordinates 
# are nothing but rows and columns
style = TableStyle(
    [
        ( "BOX" , ( 0, 0 ), ( -1, -1 ), 1 , colors.black ),
        ( "GRID" , ( 0, 0 ), ( 4 , 4 ), 1 , colors.black ),
        ( "BACKGROUND" , ( 0, 0 ), ( 3, 0 ), colors.gray ),
        ( "TEXTCOLOR" , ( 0, 0 ), ( -1, 0 ), colors.whitesmoke ),
        ( "ALIGN" , ( 0, 0 ), ( -1, -1 ), "CENTER" ),
        ( "BACKGROUND" , ( 0 , 1 ) , ( -1 , -1 ), colors.beige ),
    ]
)
  
# creates a table object and passes the style to it
table = Table( DATA , style = style )
  
# final step which builds the
# actual pdf putting together all the elements
pdf.build([ title , table ])

Output:

Code Breakdown

Let’s delve deeper into some key parts of the code:

Styling

The script demonstrates the ability to style both the title and the table. It uses ReportLab’s TableStyle to define different formatting elements such as borders, gridlines, background colors, text color, and alignment. This allows for fine-grained control over the appearance of the PDF.

Data Structure

The DATA variable is structured as a list of lists. Each inner list represents a row in the table, and the first row contains column headers. This structure makes it easy to organize and display tabular data.

PDF Generation

The SimpleDocTemplate and Table classes from ReportLab are utilized for creating the PDF. The script first defines a document template, sets up the styles, creates a title paragraph, defines the table style, and finally builds the PDF by combining the title and table.

Conclusion

The provided Python script showcases how to use the ReportLab library to generate a PDF receipt with a structured table and various formatting options. This case study highlights the process of creating a PDF document from scratch and customizing its appearance to meet specific requirements.

Leave a Reply

Your email address will not be published. Required fields are marked *