Producing PDFs in landscape orientation with ReportLab

Osmond Bishop picture Osmond Bishop · Mar 19, 2013 · Viewed 7.7k times · Source

I am working on a Python script that produces a PDF report using ReportLab. I need to produce the pages in landscape orientation and I have already looked through the ReportLab manual but I can't find a way of doing this. Any ideas or suggestions?

Answer

Kyle Heuton picture Kyle Heuton · Mar 19, 2013

Make sure you've imported

from reportlab.lib.pagesizes import letter, landscape

And then:

canvas.setPageSize(landscape(letter))

Or more generally,

canvas.setPageSize(width, height)

and you can set it to be any size you like. Remember reportlab uses points, where 1 point is 1/72 of an inch. You can also import:

from reportlab.lib.units import inch

Which lets you do things like:

canvas.setPageSize(11*inch, 8.5*inch)

EDIT: Added all the built in page sizes inspired by yentsun.

Diving into the reportlab.lib.pagesizes source code, we find in all:

A0,A1,A2,A3,A4,A5,A6
B0,B1,B2,B3,B4,B5,B6
LETTER, LEGAL, ELEVENSEVENTEEN

The comments claims that the lowercase letter has been deprecated since 2001, but is still available. Additionally, two functions are avaialable, portrait and landscape that each take in a tuple pagesize, and portrait returns the tuple with the smaller dimension first, and landscape returns the tuple with the larger dimension first.