简体   繁体   中英

How to manually add a new page to a PDF document in reportlab?

I'm trying to create and add pages manually to a PDF file, using Python 3 and reportlab.

I can easily create the first page, and draw on it. But I don't know how to manually add new pages.

Example:

from reportlab.pdfgen.canvas import Canvas
canvas = Canvas(pdf_path, pagesize=(WIDTH_OF_PAGE1,HEIGHT_OF_PAGE1))    
canvas.rect(100,100,200,200,fill=1, stroke=1) # draw on the first page, this is just an example...
# Now create and add a new page here, but how???
canvas2.rect(200,200,300,300,fill=1, stroke=1) # draw on the second page, this is just an example...
# Finally, save the PDF
canvas.save()

How do I start a new page of a specific size?

Please note that I'm not using Platypus. These pages don't have a fixed height. Every page will have a different height, that is pre-calculated by some algorithm. So the concept of "flowables" cannot be used here, and I need to add pages of specific (different) sizes to a PDF document.

A workaround solution could be to create a separate PDF file for each page, then concatenate them with an external program, but I don't like that idea.

Finally, I figured out how to create a new page. It is mentioned in the reportlab documentation, I just did not notice.

canvas.showPage() #Close the current page and possibly start on a new page.

There is also a solution for changing the page size for individual pages - this is something that Platypus cannot do:

canvas.showPage() # Close the current page and possibly start on a new page.
canvas.setPageSize(A3) # Change size of the current page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM