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

Sjoerd Rijpkema picture Sjoerd Rijpkema · May 4, 2011 · Viewed 11k times · Source

Is it possible to create a PDF document with differing page sizes in reportlab?

I would like to create a document where the first page has a different size then the other pages. Can anyone help?

Answer

G Gordon Worley III picture G Gordon Worley III · May 5, 2011

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.