I have an issue with my generated pdf, the generated pdf file has 4 pages and I use an existing pdf with two pages. I have to write some text using pyPD2
and reportlab
.
When I usegetPage()
method I thought to make a page copy before merging, but for some reasons it uses the same page and I got my text printed several times.
How can I avoid that?
My code:
packet = io.BytesIO()
can = canvas.Canvas(packet)
can.drawString(0,0, "Print on page 1")
can.showPage()
can.drawString(200,200, "Print on page 2")
can.showPage()
can.drawString(0,0, "Print on page 3")
can.showPage()
can.drawString(200,200, "Print on page 4")
can.save()
new_pdf = PdfFileReader(packet)
path_of_two_page_pdf = 'overview.pdf'
existing_pdf = PdfFileReader(file(path_of_two_page_pdf, "rb"))
output = PdfFileWriter()
for pi in range(4):
page = existing_pdf.getPage(pi % 2)
page.mergePage(new_pdf.getPage(pi))
output.addPage(page)
outputStream = file("NEW_PDF.pdf", "wb")
output.write(outputStream)
outputStream.close()
The problem is that
getPage()
actually return the pointer to the page instead of a completely new page. So whenmergePage()
is applied it merge it into the existing pdf.The easiest workaround is to first create a blank page and use that to merge the other pages onto, that will look like this:
One note, instead of creating a completely separate blank page using
createBlankPage
you could also useoutput.addBlankPage
but this requires setting the page size manually which might not be ideal. Therefor thepdf
keyword ofcreateBlankPage
is used, this sets the page size to the last page of the supplied pdf.