Search code examples
pythoncanvaspypdf

Add text to multiple page long PDF using Python


My Situation

Hi, I'm relatively new to coding with Python, and I'm currently working on a "PDF-Writer" which should read in PDF files and then draw a certain String to it plus some other stuff. I'm doing this with canvas and PyPDF2 and it works fine. My question is referring to this question: (Add text to Existing PDF using Python) Others already commented under the post but didn't post any answers there that's why I'm asking this question. I would have commented under the post, but sadly, I don't have the reputation to comment. But I think resolving the issue by asking a new question is also fine.

My problems

  1. My first problem is that I can only write on the first page of the document. I would like to write on all pages of the document.
  2. My second problem is that the first page of the document is also the last page for some reason.

Here my Code:

from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.units import cm

file = input("Give path to the file >> ")
packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=letter)
# register font
pdfmetrics.registerFont(TTFont('Arial', 'Arial.ttf'))
can.setFont(psfontname= "Arial",size = 10)
# create a new string
can.drawString(93, 54.5, "2022-12-21")
# create a white rectangle 
can.setFillColorRGB(254,254,254)
can.rect(92.5,65,2*cm,0.34*cm, fill=1,stroke=0)
# save the pdf
can.save()
# move to the beginning of the StringIO buffer
packet.seek(0)
# create a new PDF with Reportlab
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(open(file, "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.pages[0]
page.merge_page(new_pdf.pages[0])
output.append_pages_from_reader(existing_pdf)
output.add_page(page)
# finally, write "output" to a real file
output_stream = open("destination3.pdf", "wb")
output.write(output_stream)
output_stream.close()
print("PDF created!")

What I've tried

Well, I added output.append_pages_from_reader(existing_pdf) to the code which led to the problems. Now I'm searching after a properly working Code :)


Solution

  • Well I got the answer, here is an excerpt:

            existing_pdf = PdfFileReader(open(filename2, "rb"))
            output = PdfFileWriter()
            # add the "watermark" (which is the new pdf) on the existing pages
            for x in range(existing_pdf.getNumPages()):
                page = existing_pdf.pages[x]
                page.merge_page(new_pdf.pages[0])
                output.add_page(page)
            # finally, write "output" to a real file
            output_stream = open(outputPath, "wb")
            output.write(output_stream)
            output_stream.close()
            print("PDF created!")
    

    This code does exactly what I was expecting in my question above.