Add text to Existing PDF using Python
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:
- read your PDF using
PdfFileReader()
, we'll call this input - create a new pdf containing your text to add using ReportLab, save this as a string object
- read the string object using
PdfFileReader()
, we'll call this text - create a new PDF object using
PdfFileWriter()
, we'll call this output - iterate through input and apply
.mergePage(*text*.getPage(0))
for each page you want the text added to, then useoutput.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.
Adding Text to a PDF via Python
try this as already mentioned here
from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter
copyfile("newPDF.pdf", "basePDF.pdf")
packet = io.BytesIO()
# do whatever writing you want to do
can = canvas.Canvas(packet, pagesize=letter)
can.drawString(10, 100, "Hello world")
can.save()
#move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(open("basePDF.pdf", "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
# finally, write "output" to a real file
outputStream = open("destination.pdf", "wb")
output.write(outputStream)
outputStream.close()
Add text to existing PDF document in Python
You should look at Add text to Existing PDF using Python and also Python as PDF Editing and Processing Framework. These will point you in the right direction.
If you do what you've proposed in the question, when you export back to .pdf
, it will really just be an image file embedded in a .pdf
, it won't be text.
Related Topics
Financial Charts/Graphs in Ruby or Python
How to Write to a CSV Line by Line
Python and Regular Expression with Unicode
Calling Java/Scala Function from a Task
Dictionaries and Default Values
R and Python in One Jupyter Notebook
Pelican 3.3 Pelican-Quickstart Error "Valueerror: Unknown Locale: Utf-8"
Conda Reports Packagesnotfounderror: Python=3.1 for Reticulate Environment
Placing Custom Images in a Plot Window--As Custom Data Markers or to Annotate Those Markers
Why Are Scripting Languages (E.G. Perl, Python, and Ruby) Not Suitable as Shell Languages
Parallel Processing from a Command Queue on Linux (Bash, Python, Ruby... Whatever)
Is There a Built in Package to Parse HTML into Dom
Socketserver.Threadingtcpserver - Cannot Bind to Address After Program Restart
How to Log While Using Multiprocessing in Python
Authenticate from Linux to Windows SQL Server with Pyodbc
Plot Different Color for Different Categorical Levels Using Matplotlib