Last modified: Feb 07, 2026 By Alexander Williams

Python PDF Generator Guide | Create PDFs

Python is a powerful tool for automation. One common task is generating PDF files. You can create invoices, reports, or certificates. This guide shows you how.

We will explore popular libraries. You will learn to add text, images, and tables. Let's start creating professional PDFs with Python.

Why Generate PDFs with Python?

PDFs are a universal document format. They preserve layout across devices. Manually creating them is tedious and error-prone.

Python automates this process. You can generate hundreds of documents from data. It's perfect for batch operations and dynamic content.

Common use cases include automated invoicing, data reporting, and document generation for web applications. Python makes it efficient.

Top Python Libraries for PDF Generation

Several libraries can create PDFs. Each has its strengths. We will focus on two: ReportLab and FPDF.

ReportLab is the most powerful and feature-rich. It is great for complex, styled documents. It is often used in commercial applications.

FPDF is very simple and lightweight. It is easier to learn for beginners. It is good for basic text-based PDFs.

Other options include WeasyPrint for HTML to PDF and PyPDF2 for manipulating existing PDFs. For reading existing files, see our Python PDF Reader Guide.

Getting Started with ReportLab

First, install ReportLab using pip. Open your terminal and run the command below.


pip install reportlab
    

ReportLab uses a canvas-based approach. You draw elements onto a page. Let's create a simple "Hello World" PDF.


from reportlab.pdfgen import canvas

# Create a canvas object for our PDF
c = canvas.Canvas("hello_reportlab.pdf")

# Draw a string at coordinates (100, 750)
c.drawString(100, 750, "Hello, World from ReportLab!")

# Save the PDF file
c.save()
print("PDF created successfully!")
    

The canvas.Canvas() function creates a new PDF. The drawString() method adds text. Coordinates (x, y) start from the bottom-left. The save() method writes the file to disk.

Creating a Structured Document with ReportLab

Real documents need structure. ReportLab's PLATYPUS (Page Layout and Typography Using Scripts) is perfect for this. It uses flowables like paragraphs and tables.

Let's create a document with a title, paragraph, and a simple table.


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

# Create the document object
doc = SimpleDocTemplate("structured_doc.pdf", pagesize=letter)
story = []  # This list will hold our flowables
styles = getSampleStyleSheet()

# Add a Title
title = Paragraph("My Generated Report", styles['Title'])
story.append(title)
story.append(Spacer(1, 0.25*inch))

# Add a Paragraph
text = "This is a sample paragraph in our PDF document. It demonstrates how to use PLATYPUS flowables for automatic layout."
para = Paragraph(text, styles['Normal'])
story.append(para)
story.append(Spacer(1, 0.5*inch))

# Add a Simple Table
data = [['Item', 'Quantity', 'Price'],
        ['Apple', '5', '$2.50'],
        ['Banana', '3', '$1.20'],
        ['Orange', '8', '$3.00']]
table = Table(data)
story.append(table)

# Build the PDF
doc.build(story)
print("Structured PDF created!")
    

This code produces a clean, formatted PDF. The SimpleDocTemplate manages pages. Paragraph handles styled text. Table creates data grids. The build() method renders everything.

Getting Started with FPDF

FPDF is simpler. Install it with pip.


pip install fpdf
    

FPDF uses a cell-based model. You define cells for text and borders. Here is a basic example.


from fpdf import FPDF

# Create a PDF object
pdf = FPDF()
pdf.add_page()  # Add a page
pdf.set_font("Arial", size=16)  # Set font

# Add a cell with text
pdf.cell(200, 10, txt="Hello, World from FPDF!", ln=1, align='C')

# Save the PDF
pdf.output("hello_fpdf.pdf")
print("FPDF file created!")
    

The add_page() method starts a new page. set_font() defines the style. The cell() method creates a text box. The output() method saves the file.

Adding Images and Lines with FPDF

FPDF makes it easy to add images and shapes. This is great for logos or separators.


from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=14)

# Add a title
pdf.cell(200, 10, "Company Invoice", ln=1, align='C')
pdf.line(10, 20, 200, 20)  # Draw a line under the title

# Add an image (ensure 'logo.png' exists)
pdf.image('logo.png', x=10, y=30, w=30)

# Add some text next to the logo
pdf.set_xy(50, 35)  # Set position
pdf.cell(100, 10, "Invoice #: 001", ln=1)
pdf.cell(100, 10, "Date: 2023-10-27", ln=1)

pdf.output("invoice_fpdf.pdf")
print("Invoice PDF with image created!")
    

The image() method inserts an image. The line() method draws a straight line. The set_xy() method moves the cursor to specific coordinates.

Adding Metadata and Bookmarks

Professional PDFs need metadata like title and author. Some libraries also support bookmarks for navigation.

In ReportLab, you can set metadata when creating the SimpleDocTemplate. For FPDF, use the set_title() method.

Adding bookmarks (or outlines) creates a clickable table of contents. This is crucial for long documents. To learn how to extract such structures from existing PDFs, check out our guide on Python PdfReader.getOutlines.

If you need to add this information to a PDF you are creating, you can explore functions like Python PdfWriter.add_metadata and Python PdfWriter.add_bookmark when working with PyPDF2 for manipulation tasks.

Choosing the Right Library

How do you choose between ReportLab and FPDF? Consider your project needs.

Use ReportLab for complex, styled reports. It is ideal for invoices, official documents, and anything requiring precise typography and layout. The learning curve is steeper.

Use FPDF for quick, simple PDFs. It is great for internal reports, basic forms, or when you need a lightweight solution. It is very easy to learn.

For converting existing PDF pages into images, perhaps for thumbnails, a different toolset is required. You can learn about that process in our Python PDF to Image Conversion Guide.

Common Challenges and Tips

You might face some challenges. Here are solutions.

Text Overflow: Text might run off the page. Always check coordinates and page size. Use PLATYPUS in ReportLab for automatic text flow.

Font Issues: Embedded fonts ensure consistency. ReportLab handles this well. In FPDF, use add_font() for custom fonts.

Performance: Generating many PDFs can be slow. Use efficient loops. Consider caching static elements like headers and footers.

Always test your PDF on different viewers. This ensures compatibility.

Conclusion

Generating PDFs with Python is a valuable skill. It automates document creation and saves time.

We covered two main libraries. ReportLab is powerful for complex documents. FPDF is simple and great for basics.

Start with a simple "Hello World". Then add structure, images, and tables. Remember to add metadata for professionalism.

Now you have the tools. Go ahead and automate your PDF generation tasks. Your workflow will thank you.