How to create a PDF document with differing page sizes in reportlab, python

Yes, this should be possible, since PDF supports this, it's just a question of how to make it happen in ReportLab. I've never done this, but the following should work:

c = reportlab.pdfgen.canvas.Canvas("test.pdf")
# draw some stuff on c
c.showPage()
c.setPageSize((700, 500)) #some page size, given as a tuple in points
# draw some more stuff on c
c.showPage()
c.save()

And your document should now have two pages, one with a default size page and one with a page of size 700 pt by 500 pt.

If you are using PLATYPUS you should be able to achieve the same sort of thing, but will probably require getting fancy in a BaseDocTemplate subclass to handle changing page sizes, since I'm pretty sure the PageTemplate machinery doesn't already support this since each PageTemplate is mainly a way of changing the way frames are laid out on each page. But it is technically possible, it just isn't documented and you'll probably have to spend some time reading and understanding how PLATYPUS works internally.


my use case was to create a big table inside pdf. as table was big it was getting cropped on both sides. this is how to create a pdf with custom size.i am using platypus from reportlab.

from reportlab.platypus import  SimpleDocTemplate
from reportlab.lib.units import mm, inch

pagesize = (20 * inch, 10 * inch)  # 20 inch width and 10 inch height.
doc = SimpleDocTemplate('sample.pdf', pagesize=pagesize)
data =    [['sarath', 'indiana', 'usa'],
                            ['jose', 'indiana', 'shhs']]
table = Table(data)
elems = []
elems.append(table)
doc.build(elems)

one downside of this technique is the size is same for all pages.But will help people looking to create pdf with custom size (same for all pages)