Python: How to make Reportlab move to next page in PDF output

user1145643 picture user1145643 · Mar 14, 2012 · Viewed 7.5k times · Source

I'm using the open source version Reportlab with Python on Windows. My code loops through multiple PNG files & combines them to form a single PDF. Each PNG is stretched to the full LETTER spec (8.5x11).

Problem is, all the images saved to output.pdf are sandwiched on top of each other and only the last image added is visible. Is there something that I need to add between each drawImage() to offset to a new page? Here's a simple linear view of what I'm doing -

WIDTH,HEIGHT = LETTER                                            
canv = canvas.Canvas('output.pdf',pagesize=LETTER)               
canv.setPageCompression(0)                                       

page = Image.open('one.png')                                     
canv.drawImage(ImageReader(page),0,0,WIDTH,HEIGHT)

page = Image.open('two.png')                                     
canv.drawImage(ImageReader(page),0,0,WIDTH,HEIGHT)

page = Image.open('three.png')                                   
canv.drawImage(ImageReader(page),0,0,WIDTH,HEIGHT)

canv.save()                                                      

Answer

Harshith J.V. picture Harshith J.V. · Mar 14, 2012

[Follow up of the post's comment]

Use canv.showPage() after you use canv.drawImage(...) each time. ( http://www.reportlab.com/apis/reportlab/dev/pdfgen.html#reportlab.pdfgen.canvas.Canvas.showPage )

Follow the source document(for that matter any tool you are using, you should dig into it's respective website documentation): http://www.reportlab.com/apis/reportlab/dev/pdfgen.html