Last modified: Oct 10, 2026

Generate PDFs with ReportLab: Python Guide

ReportLab is a powerful Python library for creating PDF documents. It lets you build invoices, reports, and certificates programmatically. This guide will walk you through the essentials, from installation to advanced features.

What is ReportLab?

ReportLab is a proven industry-strength PDF generation library for Python. It allows rapid creation of rich PDF documents directly from your code. Unlike some tools, ReportLab does not require external system dependencies, making it a pure Python solution that works on all platforms[reference:0].

The library provides two main approaches. The Canvas API gives you low-level, coordinate-based control for pixel-perfect placement. The Platypus framework offers high-level document layout for complex, flowing documents with minimal code[reference:1].

Installation

Installing ReportLab is straightforward. You can use pip, the Python package manager. The library requires Python 3.7 or higher[reference:2].

 pip install reportlab 

Verify the installation by importing it in your Python interpreter. If no errors appear, you are ready to go.

 import reportlab print(reportlab.__version__) 
 4.3.1 

Creating Your First PDF with Canvas

The Canvas API is the foundation of ReportLab. It works like a digital drawing board. You create a canvas object and then draw text, shapes, and images at specific coordinates.

Here is a simple "Hello World" example. We use canvas.Canvas() to create the PDF and drawString() to place text.

 from reportlab.pdfgen import canvas # Create a canvas object p = canvas.Canvas("hello.pdf") # Draw text at coordinate (100, 100) p.drawString(100, 100, "Hello world.") # Finalize the PDF p.showPage() p.save() 

When you run this code, it creates a file named hello.pdf in your working directory. The drawString() method places text at the given x and y coordinates. The showPage() method finalizes the current page, and save() writes the file.

Building Documents with Platypus

For multi-page documents with flowing text, tables, and images, the Platypus framework is the better choice. Platypus stands for "Page Layout and Typography Using Scripts." It handles pagination automatically, so you don't have to calculate coordinates for every element.

Platypus uses a SimpleDocTemplate to define the document and a list of "flowables" to hold the content. Flowables are elements that flow through frames on the page[reference:3].

 from reportlab.lib.pagesizes import letter from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer from reportlab.lib.styles import getSampleStyleSheet from reportlab.lib.units import inch # Create the document template doc = SimpleDocTemplate("report.pdf", pagesize=letter) # Get default styles styles = getSampleStyleSheet() # Build the story (list of flowables) story = [] story.append(Paragraph("My First Report", styles['Title'])) story.append(Spacer(1, 0.2 * inch)) story.append(Paragraph("This is the body text of the report.", styles['BodyText'])) # Build the PDF doc.build(story) 

This code creates a nicely formatted PDF with a title and body text. The Spacer adds vertical whitespace between elements. The getSampleStyleSheet() provides pre-defined styles like 'Title' and 'BodyText'.

Adding Tables and Images

ReportLab excels at creating structured data. Tables are a common requirement for reports and invoices. The Table flowable makes this easy.

 from reportlab.platypus import Table, TableStyle from reportlab.lib import colors # Sample data data = [ ['Product', 'Quantity', 'Price'], ['Widget', '10', '$5.00'], ['Gadget', '5', '$12.50'], ] # Create the table table = Table(data) # Add styling table.setStyle(TableStyle([ ('BACKGROUND', (0, 0), (-1, 0), colors.grey), ('TEXTCOLOR', (0, 0), (-1, 0), colors.whitesmoke), ('ALIGN', (0, 0), (-1, -1), 'CENTER'), ('GRID', (0, 0), (-1, -1), 1, colors.black), ])) story.append(table) 

Images can also be added using the Image flowable. You provide the file path and the desired width and height. ReportLab will scale the image proportionally if you specify only one dimension.

 from reportlab.platypus import Image # Add an image img = Image("logo.png", width=2*inch, height=1*inch) story.append(img) 

Generating Barcodes and QR Codes

ReportLab supports a wide range of barcode formats, from Code128 to QR codes. This is incredibly useful for inventory, ticketing, and retail applications. You can draw barcodes directly on a canvas using the barcode sub-module[reference:4].

The Code128 barcode is versatile and encodes the full ASCII character set. Here is how to create one and place it on a canvas.

 from reportlab.pdfgen import canvas from reportlab.graphics.barcode import code128 from reportlab.lib.units import inch c = canvas.Canvas("barcode.pdf") # Create a Code128 barcode barcode = code128.Code128("HELLO123") # Draw the barcode at position (1 inch, 5 inches) barcode.drawOn(c, 1*inch, 5*inch) c.save() 

For QR codes, you can use the createBarcodeDrawing() function to generate the image and then draw it inline. This flexibility makes ReportLab a comprehensive solution for PDF generation.

Real-World Use Cases

ReportLab is used across many industries. Django, the popular web framework, uses ReportLab for dynamically generating PDF files from web views. The key is that ReportLab's API works on file-like objects, so you can stream PDFs directly to a web response[reference:5].

Common use cases include generating invoices, financial reports, shipping labels, event tickets, and personalized certificates. The library's ability to handle complex layouts and data-driven content makes it ideal for these tasks.

Conclusion

ReportLab is a robust and flexible library for generating PDFs in Python. It offers both low-level control with the Canvas API and high-level document building with Platypus. Whether you need a simple text PDF or a complex report with tables, images, and barcodes, ReportLab provides the tools you need.

Start with the Canvas API for simple, single-page documents. Move to Platypus when you need multi-page layouts and automatic pagination. With its comprehensive feature set and pure Python implementation, ReportLab is an excellent choice for any PDF generation task.