ReportLab Toolkit

4.4.10 · active · verified Sun Mar 29

The ReportLab Toolkit is an open-source Python library for generating high-quality PDF documents and graphics. It provides both low-level APIs for precise control over PDF elements and a higher-level framework (PLATYPUS) for creating complex, structured documents with flowables like paragraphs and tables. Currently at version 4.4.10, ReportLab is actively maintained with regular updates and is widely used for dynamic, data-driven report generation, including by Wikipedia for its 'Download as PDF' feature.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a basic PDF document using ReportLab's higher-level PLATYPUS framework. It initializes a `SimpleDocTemplate`, defines a 'story' (a list of 'flowables' like `Paragraph` and `Spacer`), applies standard styles, and then builds the PDF.

from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.pagesizes import letter

def create_pdf(filename="hello_reportlab.pdf"): 
    doc = SimpleDocTemplate(filename, pagesize=letter)
    styles = getSampleStyleSheet()
    story = []

    # Add a title
    story.append(Paragraph("Hello, ReportLab!", styles['h1']))
    story.append(Spacer(1, 0.2 * 25.4))

    # Add a paragraph
    long_text = "This is an example of a simple PDF document generated using the ReportLab Toolkit. It demonstrates how to use `SimpleDocTemplate` and `Paragraph` for basic text content. ReportLab provides extensive capabilities for layouts, tables, images, and graphics." 
    story.append(Paragraph(long_text, styles['Normal']))

    # Build the PDF document
    doc.build(story)
    print(f"PDF created: {filename}")

if __name__ == "__main__":
    create_pdf()

view raw JSON →