Sooner or later, almost every business application needs to produce PDFs: invoices, receipts, salary slips, certificates, reports. Python's ReportLab library has been doing this reliably for over two decades, and it's still one of the best tools for generating documents on a server without a browser or Word installed.
In this tutorial, we'll build a clean invoice generator with a header, billing details, a line-item table, GST calculation and totals. The complete code is below, and it runs as-is.
Why ReportLab?
- Pure Python, no external rendering engine required
- Fast, suitable for generating thousands of PDFs in batch jobs
- Precise layout control with tables, styles and page templates
- Works on Windows Server, Linux and in containers
ReportLab offers two ways to build PDFs:
- canvas — What it is: Draw text and shapes at exact coordinates; Best for: Certificates, labels, fixed forms
- Platypus — What it is: Flowable layout engine (paragraphs, tables, spacers) that handles page breaks; Best for: Invoices, reports, multi-page documents
For invoices, Platypus is the better choice because the number of line items varies and the layout needs to flow onto new pages automatically.
Step 1: Install ReportLab
pip install reportlab
Step 2: The complete invoice generator
You'll get invoice.pdf with a seller header, invoice details on the right, the buyer address, a styled line-item table, subtotal, GST and a bold total.
The document template
SimpleDocTemplate sets the page size (A4) and margins. You add "flowables" to a story list, and ReportLab lays them out top to bottom, creating new pages as needed.
Paragraphs and styles
Paragraph objects support simple HTML-like markup (b tag, br tag, i tag). We create a small 9-point style for most text and a right-aligned version for the invoice details.
Tables
Table takes a list of rows. We:
- set explicit column widths so the layout is predictable
- wrap descriptions in a Paragraph so long text wraps inside the cell
- use repeatRows=1 so the header row repeats if the table continues onto another page
TableStyle
TableStyle commands use cell coordinates as (column, row). Negative indices count from the end, so (-1, -1) is the bottom-right cell. This makes it easy to style the last three total rows differently from the item rows.
Step 3: Handling money correctly
The tutorial uses floats to keep the code short.
Also calculate tax according to your actual rules. For example, Indian invoices may need CGST + SGST for intra-state supplies and IGST for inter-state supplies, along with HSN/SAC codes. Confirm invoice requirements with your accountant.
Step 4: Using a proper rupee symbol
The built-in PDF fonts (Helvetica, Times) don't include the ₹ symbol, which is why the example uses "Rs.". To print ₹, register a TrueType font that contains it:
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
pdfmetrics.registerFont(TTFont("DejaVu", "DejaVuSans.ttf"))
pdfmetrics.registerFont(TTFont("DejaVu-Bold", "DejaVuSans-Bold.ttf"))
Then use fontName="DejaVu" in your paragraph and table styles. Ship the font files with your application so the server always has them. The same technique works for Hindi and other Indian languages, using a font that supports Devanagari.
Step 5: Add a logo
from reportlab.platypus import Image
logo = Image("logo.png", width=35*mm, height=12*mm)
story.insert(0, logo)
Use a reasonably sized PNG. Very large images slow down generation and bloat file size.
Step 7: Generate PDFs in a web application
Instead of saving to disk, write to memory and return the bytes:
from io import BytesIO
buffer = BytesIO()
build_invoice(buffer, invoice) # SimpleDocTemplate accepts file-like objects
pdf_bytes = buffer.getvalue()
In Flask, FastAPI or Django, return pdf_bytes with the content type application/pdf.
Production tips
- Separate data from layout. Keep invoice data in a dictionary or model; keep the PDF code focused on presentation.
- Store generated PDFs if invoices must be legally retained, rather than regenerating them later with possibly changed data.
- Use background jobs for bulk generation (month-end invoices for thousands of customers).
- Validate inputs: escape user-provided text used inside Paragraph, since its markup is parsed. Angle brackets and ampersands should be escaped with xml.sax.saxutils.escape.
- Test with long data: very long item descriptions, 100+ line items and large amounts.
Common errors
- Black boxes instead of ₹ or Hindi — Cause: Font lacks those characters; Fix: Register a TTF font that supports them
- paraparser: syntax error — Cause: Unescaped angle bracket or ampersand in text; Fix: Escape user text before using it in Paragraph
- Table runs off the page — Cause: Column widths exceed page width; Fix: Sum of widths must fit within page minus margins
- Text overflows cells — Cause: Plain strings don't wrap; Fix: Wrap long text in Paragraph objects
Wrapping up
ReportLab's Platypus engine gives you reliable, precise PDF generation with very little code. Start with the invoice above, add your logo, fonts and tax logic, and you'll have a document generator that can run quietly on a server for years.
Next, try the same approach for salary slips, quotations or monthly statements. The pattern is the same: data in, styled story out.
Leave a Comment