TodayTools

Make PDFs with ReportLab flashcards

Generate invoices, statements and reports from Python: exact drawing with the canvas, flowing layout with Platypus.

Donate
Beginner friendly, starts from zero
Section
How to study
0
Make PDFs with ReportLabStart from zero · 1 of 12

What is ReportLab, in one plain sentence?

A free Python , a box of ready-made code, that lets your program create files like bills, tickets and reports.

Python writes the for you.

All topics

Make PDFs with ReportLab at a glance

ReportLab is a Python library that lets your program create PDF files such as bills, tickets and reports.

Like writing on graph paper: you say exactly which box each word goes in, and it lands there.

Words you will meet

PDF
A document file that looks the same on every device and printer
Library
A box of ready-made code you add to your program
Canvas
The blank page you draw text, lines and pictures on
x and y
How far from the left edge and how far up from the bottom
Point
The PDF unit of size; 72 points make one inch
Font
A style of letters, like Helvetica, with a size in points
Platypus
The part of ReportLab that flows text and tables across pages

The steps

  1. 1. Start from zero (12 cards): What ReportLab and PDFs are, the key words in plain English, and your first PDF.
  2. 2. Core basics (15 cards): The canvas, coordinates, units, fonts, shapes, colours and images.
  3. 3. Next steps (16 cards): Platypus documents, styles, safe markup, real tables, page furniture and web responses.
  4. 4. Go deeper (18 cards): Fonts and scripts, page templates, page x of y, charts, barcodes, overlays, security and scale.
All 12 cards in Start from zero
What is ReportLab, in one plain sentence?
A free Python library, a box of ready-made code, that lets your program create PDF files like bills, tickets and reports.
What is a PDF, and why is it so popular?
A PDF is a document file that looks exactly the same on every phone, computer and printer. That is why bills, tickets and certificates come as PDFs.
What problem does ReportLab solve?
Without it, someone types each bill or certificate by hand in a word processor. With 5,000 customers that takes days and invites typing mistakes.
Where do you already meet PDFs made by code?
Your monthly phone bill, a bank statement, a train ticket, a salary slip and an exam hall ticket. Nobody typed yours; a program made it from data.
What is an everyday picture for how ReportLab draws?
A sheet of graph paper. You say 'write this word 2 boxes from the left and 3 boxes up from the bottom', and it is written exactly there.
What do canvas and x, y mean?
The canvas is your blank page that you draw on. x is how far from the left edge, y is how far up from the bottom edge. Together they place anything exactly.
What are points and page size?
A point is the small unit used to measure a PDF page: 72 points make one inch. Page size is the paper shape, like A4 (common in India and Europe) or US Letter.
What is a font, and what does 16 pt mean?
A font is a style of letters, like Helvetica or Times. 16 pt is the letter size in points; body text is usually 10 to 12 pt and headings bigger.
What is Platypus?
The part of ReportLab that pours paragraphs and tables onto pages for you and starts a new page when one is full, like a word processor does.
What do you need before starting?
Python on your computer, a text editor and a PDF viewer. You install ReportLab with one command; basic Python like variables and loops is enough.
What is a first tiny win with ReportLab?
Make a canvas for a new file, use drawString to write text at x, y, then save to write the PDF.
What will you be able to do after this topic?
Draw exact layouts like tickets, make long reports with tables that flow across pages, add logos and fonts for any language, and make thousands of PDFs from data.
Make PDFs with ReportLab cheatsheet

Canvas

  • canvas.Canvas(buf, pagesize=A4) New PDF to a file or BytesIO
  • c.setFont("Helvetica-Bold", 16) Font for what follows
  • c.drawString(x, y, text) Text, x, y from bottom-left
  • c.drawRightString(x, y, text) Right-aligned at x
  • c.drawCentredString(x, y, text) Centred on x (British spelling)
  • c.rect(x, y, w, h, stroke=1, fill=0) Box from bottom-left corner
  • c.drawImage(path, x, y, w, h, mask="auto") Image, keeps PNG transparency
  • c.saveState() / c.restoreState() Protect graphics settings
  • c.linkURL(url, (x1, y1, x2, y2)) Clickable area
  • c.showPage(); c.save() Next page, then write the file

Platypus

  • SimpleDocTemplate(buf, leftMargin=2*cm) Document with margins
  • Paragraph(escape(text), style) Wrapped, styled text
  • Spacer(1, 12) 12 pt vertical space
  • Table(rows, colWidths=[...], repeatRows=1) Table, header repeats
  • Image(buf, 4*cm, 4*cm, kind="proportional") Picture that keeps shape
  • CondPageBreak(4*cm) Break if less space left
  • KeepTogether([a, b]) Never split this group
  • NextPageTemplate("later") Switch page design
  • doc.build(story, onFirstPage=f, onLaterPages=f) Footer on each page

TableStyle commands

  • ("GRID", (0, 0), (-1, -1), 0.5, colors.grey) All borders
  • ("BACKGROUND", (0, 0), (-1, 0), colors.lightgrey) Header row fill
  • ("ROWBACKGROUNDS", (0, 1), (-1, -1), [white, whitesmoke]) Zebra rows
  • ("ALIGN", (1, 1), (-1, -1), "RIGHT") Right-align numbers
  • ("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold") Bold header
  • ("VALIGN", (0, 0), (-1, -1), "MIDDLE") Vertical centre
  • ("SPAN", (0, -1), (2, -1)) Merge cells in the last row
  • ("LINEBELOW", (0, 0), (-1, 0), 1, colors.black) Rule under the header

Fonts and text

  • pdfmetrics.registerFont(TTFont("Noto", path)) Use a TrueType font
  • pdfmetrics.registerFontFamily("Noto", bold="Noto-Bold") Make <b> work
  • pdfmetrics.stringWidth(text, font, size) Measure text width
  • ParagraphStyle("note", parent=ss["Normal"], leading=10) Custom style
  • <b> <i> <font color> <a href> <br/> Paragraph markup

Units and sizes

  • 72 points = 1 inch Base unit
  • from reportlab.lib.units import cm, mm Readable sizes
  • A4 595.28 x 841.89 points
  • letter 612 x 792 points
  • landscape(A4) Swap width and height
  • (0, 0) Bottom-left corner
Donate