ReportLab Toolkit

raw JSON →
4.4.10 verified Tue May 12 auth: no python install: verified

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.

pip install reportlab
error ModuleNotFoundError: No module named 'reportlab'
cause The ReportLab library is not installed in the current Python environment or is not accessible within the project's interpreter.
fix
Install the ReportLab library using pip: pip install reportlab
error AttributeError: 'module' object has no attribute 'canvas'
cause The `canvas` class is imported incorrectly, typically by importing the `reportlab.pdfgen` module instead of importing `canvas` directly from it.
fix
Import the canvas class explicitly: from reportlab.pdfgen import canvas
error TypeError: __init__() missing 1 required positional argument: 'filename'
cause The `canvas.Canvas` constructor was called without providing the mandatory `filename` argument, which specifies the name of the output PDF file.
fix
Provide a string argument for the output PDF filename when initializing Canvas, e.g., c = canvas.Canvas("my_document.pdf")
error reportlab.pdfbase._fontdata.ReportLabFontError: Can't register font, not a TTF file?
cause An attempt was made to register a font file that is either not a valid TrueType Font (TTF) file, is corrupted, or the file path provided to `pdfmetrics.registerFont` is incorrect.
fix
Ensure the font file is a valid TTF and verify that the file path provided to pdfmetrics.registerFont is correct and accessible.
error reportlab.lib.utils.ImageReader.get==>Failed to find image data in PNG file
cause ReportLab was unable to read the image file, often because the file path is incorrect, the image file is corrupted, or the required `Pillow` library for advanced image processing is not installed.
fix
Verify the image file path is correct, ensure the image file is valid and supported, and install Pillow if it's missing: pip install Pillow
breaking ReportLab 3.0 (released 2014) introduced significant internal rewrites for Python 3 compatibility and consistent Unicode/bytes handling. While API changes were minimized, older code might encounter unexpected behavior related to string encoding.
fix Review and update code for Python 3 compatibility, particularly regarding string types. Refer to ReportLab's change logs for detailed transitions.
breaking Reusing flowable objects (e.g., `Paragraph`, `Table`) in multiple document builds or pages without re-instantiating them can lead to `LayoutError` exceptions. ReportLab modifies these objects in place during the layout process, which is not reset for subsequent uses.
fix Always create new instances of flowable objects for each use, especially if generating multiple documents or repeating elements across different sections/pages. For example, `story.append(Paragraph('Text', styles['Normal']))` should be done for each paragraph, not defining `my_paragraph = Paragraph(...)` once and appending it multiple times.
gotcha The default coordinate system in ReportLab's `canvas` module has its origin (0,0) at the bottom-left corner of the page. This is different from many other graphics libraries where the origin is top-left, and can lead to incorrect positioning if not accounted for.
fix Always remember that y-coordinates increase upwards. Adjust calculations accordingly, or use helper functions/classes that abstract this if needed.
gotcha Optional C extensions (e.g., `rl_accel`, `rl_renderpm`) are crucial for optimal performance, especially with bitmap image processing and complex graphics. If these are not installed (often requiring a C compiler during `pip install`), ReportLab will fall back to slower pure-Python implementations or lack certain features.
fix Ensure that development tools including a C compiler (e.g., Build Tools for Visual Studio on Windows, Xcode Command Line Tools on macOS, `build-essential` on Linux) are installed before running `pip install reportlab` to allow compilation of optional C extensions. Verify installation by checking for performance or missing features.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.59s 45.9M
3.10 alpine (musl) - - 0.68s 45.9M
3.10 slim (glibc) wheel 3.0s 0.43s 47M
3.10 slim (glibc) - - 0.48s 47M
3.11 alpine (musl) wheel - 1.36s 49.6M
3.11 alpine (musl) - - 1.55s 49.6M
3.11 slim (glibc) wheel 2.7s 1.24s 51M
3.11 slim (glibc) - - 1.16s 51M
3.12 alpine (musl) wheel - 0.88s 41.1M
3.12 alpine (musl) - - 1.02s 41.1M
3.12 slim (glibc) wheel 2.6s 1.03s 42M
3.12 slim (glibc) - - 1.06s 42M
3.13 alpine (musl) wheel - 0.97s 40.9M
3.13 alpine (musl) - - 0.99s 40.7M
3.13 slim (glibc) wheel 2.7s 0.99s 42M
3.13 slim (glibc) - - 1.04s 42M
3.9 alpine (musl) wheel - 0.49s 42.8M
3.9 alpine (musl) - - 0.62s 42.8M
3.9 slim (glibc) wheel 3.5s 0.43s 44M
3.9 slim (glibc) - - 0.50s 44M

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()