text printed twice on the same page

418 views Asked by At

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()
2

There are 2 answers

0
B8vrede On BEST ANSWER

The problem is that getPage() actually return the pointer to the page instead of a completely new page. So when mergePage() 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:

for pi in range(2*2):
    # By doing this we will always create a blank page with the same size as the last page of the existing_pdf
    new_page = PageObject.createBlankPage(pdf=existing_pdf)

    # Merge the pages onto the blank page
    new_page.mergePage(existing_pdf.getPage(pi % 2))
    new_page.mergePage(new_pdf.getPage(pi))

    # Add the new page to the output
    output.addPage(new_page)

One note, instead of creating a completely separate blank page using createBlankPage you could also use output.addBlankPage but this requires setting the page size manually which might not be ideal. Therefor the pdf keyword of createBlankPage is used, this sets the page size to the last page of the supplied pdf.

0
Kenly On

As @B8vrede mensioned above addBlankPage can be used, so instead of creating a separate blank page I called addBlankPage (which returns a reference to the new created page) specifying the A4 size.

from reportlab.lib.pagesizes import A4


for pi in range(4):
    new_page = output.addBlankPage(*A4)
    new_page.mergePage(existing_pdf.getPage(pi % 2))
    new_page.mergePage(new_pdf.getPage(pi))