Last modified: Feb 07, 2026 By Alexander Williams
Create PDF with Python | Easy Tutorial
Python is a powerful tool for automation. One common task is creating PDF files. You can generate reports, invoices, or documents programmatically.
This guide will show you how. We will cover the best libraries and provide clear examples. You will learn to create PDFs from scratch and manipulate existing ones.
Why Create PDFs with Python?
PDFs are the standard for sharing documents. They preserve formatting across devices and operating systems. Creating them manually is time-consuming.
Python automates this process. You can generate hundreds of personalized documents in minutes. This is perfect for business reports, certificates, or data exports.
Using a script ensures consistency and reduces human error. It integrates seamlessly into larger data processing workflows.
Top Python Libraries for PDF Creation
Several excellent libraries exist. Your choice depends on your needs. Do you need simple text or complex layouts with images?
Here are the most popular options for creating and manipulating PDFs in Python.
1. ReportLab: For Professional Reports
ReportLab is the industry standard for complex PDF generation. It offers precise control over every element on the page.
You can create text, tables, graphs, and add images. It is powerful but has a steeper learning curve. It's ideal for automated reporting systems.
For a deeper dive into available tools, see our Python PDF Libraries Guide.
2. FPDF: Simple and Lightweight
FPDF stands for "Free PDF." It is a simpler alternative to ReportLab. The syntax is straightforward and easy to learn.
It is great for basic PDFs with text and simple shapes. If you are a beginner, start here. You can quickly create functional documents.
3. PyPDF2 / PyPDF4: For PDF Manipulation
These libraries are for working with existing PDFs. You can merge, split, rotate, and watermark pages. They are less for creation from scratch.
They are essential for document processing pipelines. You can use them with a creation library for a complete solution.
Learn more about reading files in our Python PDF Reader Guide.
Creating a Basic PDF with ReportLab
Let's create a simple PDF. First, install ReportLab using pip.
pip install reportlab
Now, write a script to generate a PDF with a title and a paragraph.
from reportlab.pdfgen import canvas
# Create a canvas object to draw on
pdf = canvas.Canvas("first_report.pdf")
# Set the title
pdf.setTitle("My First Python PDF")
pdf.setFont("Helvetica-Bold", 24)
pdf.drawString(100, 750, "Welcome to Python PDF Creation")
# Add a paragraph
pdf.setFont("Helvetica", 12)
text_object = pdf.beginText(100, 700)
text_object.textLine("This PDF was created automatically using Python.")
text_object.textLine("With ReportLab, you can add text, shapes, and images.")
pdf.drawText(text_object)
# Save the PDF file
pdf.save()
print("PDF created successfully: first_report.pdf")
This code creates a file named first_report.pdf. The canvas.Canvas object is your drawing space. You use methods like drawString to place text at specific coordinates (X, Y).
The save() method finalizes and writes the file to disk.
Creating a PDF with FPDF
FPDF uses a different, more intuitive approach. Install it first.
pip install fpdf
Here is how to create a similar document with FPDF.
from fpdf import FPDF
# Create an instance of FPDF
pdf = FPDF()
# Add a page
pdf.add_page()
pdf.set_font("Arial", 'B', 24)
# Add a title cell (ln=1 moves to next line)
pdf.cell(0, 20, txt="Welcome to Python PDF Creation", ln=1, align='C')
# Set font for body text
pdf.set_font("Arial", size=12)
# Add a multi-cell for paragraph text
pdf.multi_cell(0, 10, txt="This PDF was created automatically using Python. With FPDF, the syntax is simple and easy to understand for beginners.")
# Output the PDF file
pdf.output("simple_fpdf_document.pdf")
print("PDF created successfully: simple_fpdf_document.pdf")
FPDF uses a cell-based layout. The add_page() method starts a new page. The cell() method creates a text box. The multi_cell() method is for text that wraps.
It's simpler than managing X,Y coordinates manually.
Adding Advanced Features
Basic text is just the start. Real-world PDFs need more.
Adding Images
Both libraries support images. Here's how to add one with ReportLab.
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
pdf = canvas.Canvas("report_with_image.pdf")
pdf.drawString(100, 750, "Report with Company Logo")
# Draw an image (path, x, y, width, height)
pdf.drawImage("logo.png", 100, 650, width=2*inch, height=1*inch)
pdf.save()
Creating Tables
Tables are crucial for data reports. ReportLab's Table class is powerful.
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle
from reportlab.lib import colors
# Data for the table
data = [
['Product', 'Quantity', 'Price'],
['Laptop', '5', '$1200'],
['Mouse', '23', '$25'],
['Keyboard', '15', '$45']
]
# Create the PDF document
pdf = SimpleDocTemplate("sales_report.pdf")
story = []
# Create and style the table
table = Table(data)
style = 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)
])
table.setStyle(style)
story.append(table)
# Build the PDF
pdf.build(story)
Adding Metadata and Bookmarks
Professional PDFs include metadata like author and keywords. You can also add bookmarks for navigation.
For example, PyPDF2's PdfWriter.add_metadata function lets you embed this information. Similarly, PdfWriter.add_bookmark creates a clickable table of contents.
Explore these features in our dedicated guides on adding PDF metadata and adding bookmarks.
Choosing the Right Library
How do you pick the right tool? Consider your project's requirements.
Use ReportLab for data-heavy, formatted reports with charts and precise layouts. It's the most capable.
Use FPDF for simple, text-based documents like letters or basic forms. It's the easiest to learn.
Use PyPDF2 when you need to edit, merge, or watermark existing PDF files rather than create new ones from scratch.
For a comprehensive overview of generators, check our Python PDF Generator Guide.
Conclusion
Creating PDFs with Python is a valuable skill. It automates document generation, saving time and ensuring accuracy.
Start with FPDF for simple tasks. Graduate to ReportLab for complex reports. Use PyPDF2 to manipulate existing documents.
Combine these libraries to build powerful document automation systems. The examples here give you a solid foundation. Now you can start creating your own dynamic PDFs.