Add text to Existing PDF using Python Add text to Existing PDF using Python python python

Add text to Existing PDF using Python


Example for [Python 2.7]:

from pyPdf import PdfFileWriter, PdfFileReaderimport StringIOfrom reportlab.pdfgen import canvasfrom reportlab.lib.pagesizes import letterpacket = StringIO.StringIO()can = canvas.Canvas(packet, pagesize=letter)can.drawString(10, 100, "Hello world")can.save()#move to the beginning of the StringIO bufferpacket.seek(0)# create a new PDF with Reportlabnew_pdf = PdfFileReader(packet)# read your existing PDFexisting_pdf = PdfFileReader(file("original.pdf", "rb"))output = PdfFileWriter()# add the "watermark" (which is the new pdf) on the existing pagepage = existing_pdf.getPage(0)page.mergePage(new_pdf.getPage(0))output.addPage(page)# finally, write "output" to a real fileoutputStream = file("destination.pdf", "wb")output.write(outputStream)outputStream.close()

Example for Python 3.x:


from PyPDF2 import PdfFileWriter, PdfFileReaderimport iofrom reportlab.pdfgen import canvasfrom reportlab.lib.pagesizes import letterpacket = io.BytesIO()can = canvas.Canvas(packet, pagesize=letter)can.drawString(10, 100, "Hello world")can.save()#move to the beginning of the StringIO bufferpacket.seek(0)# create a new PDF with Reportlabnew_pdf = PdfFileReader(packet)# read your existing PDFexisting_pdf = PdfFileReader(open("original.pdf", "rb"))output = PdfFileWriter()# add the "watermark" (which is the new pdf) on the existing pagepage = existing_pdf.getPage(0)page.mergePage(new_pdf.getPage(0))output.addPage(page)# finally, write "output" to a real fileoutputStream = open("destination.pdf", "wb")output.write(outputStream)outputStream.close()


I know this is an older post, but I spent a long time trying to find a solution. I came across a decent one using only ReportLab and PyPDF so I thought I'd share:

  1. read your PDF using PdfFileReader(), we'll call this input
  2. create a new pdf containing your text to add using ReportLab, save this as a string object
  3. read the string object using PdfFileReader(), we'll call this text
  4. create a new PDF object using PdfFileWriter(), we'll call this output
  5. iterate through input and apply .mergePage(*text*.getPage(0)) for each page you want the text added to, then use output.addPage() to add the modified pages to a new document

This works well for simple text additions. See PyPDF's sample for watermarking a document.

Here is some code to answer the question below:

packet = StringIO.StringIO()can = canvas.Canvas(packet, pagesize=letter)<do something with canvas>can.save()packet.seek(0)input = PdfFileReader(packet)

From here you can merge the pages of the input file with another document.


pdfrw will let you read in pages from an existing PDF and draw them to a reportlab canvas (similar to drawing an image). There are examples for this in the pdfrw examples/rl1 subdirectory on github. Disclaimer: I am the pdfrw author.