Creating PDF Documents with Grid and 2D List Data Using ReportLab
Part I: Simple grid added to a PDF page.
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
my_path='E:\\testing\\my_pdf\\my_pdf.pdf'# file path
c = canvas.Canvas(my_path,bottomup=1,pagesize=A4)
w,h=A4 # width and height of the page
xlist = [20, 70, 120, 170] # Horizontal coordinates
ylist = [h - 20, h - 70, h - 120, h - 170] # vertical
c.grid(xlist, ylist) # adding grid
c.save() # stores the file and close the canvas
Adding Grid with text to PDF Files Using ReportLab Canvas on Colab Part-1
Part II : Using List of Data to place inside a grid
We'll populate each cell of the grid with data from a 2D list. This approach is useful for creating structured documents like tables and reports programmatically.
1. Importing Necessary Libraries
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
- reportlab.pdfgen.canvas: Used to create PDF files.
- reportlab.lib.pagesizes.A4: Provides the A4 page size.
2. Defining the File Path and Canvas
my_path='E:\\testing\\my_pdf\\my_pdf.pdf' # File path
c = canvas.Canvas(my_path, bottomup=1, pagesize=A4)
w, h = int(A4[0]), int(A4[1]) # Width and height as integers
- Define the file path where the PDF will be saved.
- Create a canvas object with A4 page size.
3. Preparing Data
l1 = [('id', 'name', 'class', 'mark', 'gender'),
(1, 'John Deo', 'Four', 75, 'female'),
(2, 'Max Ruin', 'Three', 85, 'male'),
(3, 'Arnold', 'Three', 55, 'male')] # Data to be used
- Create a list of tuples representing the data to be placed in the grid.
4. Calculating Grid Dimensions
rows, cols = len(l1), len(l1[0]) # Rows and columns of the grid
width_cell, height_cell = 75, 20 # Width and height of each cell
- Calculate the number of rows and columns in the grid.
- Define the dimensions of each cell.
5. Generating Grid Coordinates
xlist=list(range(20,width_cell*(cols+1),width_cell))
ylist=list(range(h-20,h-(height_cell*(rows+2)),-height_cell))
c.grid(xlist, ylist) # add the grid to Canvas
Data from the list of tuple is added to each cell by looping
#Adding Data inside each cell of the grid by looping
for i in range(rows):
for j in range(cols):
c.drawString(xlist[j]+2,ylist[i]-int(height_cell/1.5),
str(i) + str(j) +'-'+ str(l1[i][j]))
Save the pdf file as per the path given above.
c.save()
Full code is here
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
my_path='E:\\testing\\my_pdf\\my_pdf.pdf'# file path
c = canvas.Canvas(my_path,bottomup=1,pagesize=A4)
w,h=int(A4[0]),int(A4[1]) # Width and height as Integer
l1=[('id', 'name', 'class', 'mark', 'gender'),
(1, 'John Deo', 'Four', 75, 'female'),
(2, 'Max Ruin', 'Three', 85, 'male'),
(3, 'Arnold', 'Three', 55, 'male')] # data to be used
rows,cols=len(l1),len(l1[0]) # rows and columns of the grid
width_cell,height_cell=75,20 # Update to match the data
xlist=list(range(20,width_cell*(cols+1),width_cell))
ylist=list(range(h-20,h-(height_cell*(rows+2)),-height_cell))
c.grid(xlist, ylist) # add the grid to Canvas
#Adding Data inside each cell of the grid by looping
for i in range(rows):
for j in range(cols):
c.drawString(xlist[j]+2,ylist[i]-int(height_cell/1.5),
str(i) + str(j) +'-'+ str(l1[i][j]))
print(xlist,ylist) # print the list used for grid
c.save()
Creating PDF and Populating Data grid from a 2D List Using ReportLab in Python on Colab Part-2
When to Use grid
Simple Layouts: When we need a simple grid structure to align text or basic elements without complex formatting.
Custom Drawings: When we need to combine grid lines with custom drawing operations on the canvas (e.g., shapes, annotations).
Manual Control: When we require manual control over each cell's content and placement, especially if the content varies significantly in format or size.
Background Grids: When we need a background grid to assist with aligning other elements on the canvas.
Non-tabular Data: When the data isn’t strictly tabular and doesn’t need the rich features of a table (like spanning cells, automatic layout, etc.).
When to Use Table
Tabular Data: When presenting data in a structured, table format with multiple rows and columns.
Rich Formatting: When we need advanced formatting capabilities, such as cell spanning, border control, padding, and alignment.
Ease of Use: When we prefer to use higher-level abstractions for creating and styling tables rather than manually drawing each element.
Automatic Layout: When we want ReportLab to handle the layout, spacing, and adjustments automatically.