Search code examples
python-2.7pypdf

PYPDF watermarking returns error


hi im trying to watermark a pdf fileusing pypdf2 though i get this error i cant figure out what goes wrong.

i get the following error:

Traceback (most recent call last):   File "test.py", line 13, in <module>
    page.mergePage(watermark.getPage(0))   File "C:\Python27\site-packages\PyPDF2\pdf.py", line 1594, in mergePage
    self._mergePage(page2)   File "C:\Python27\site-packages\PyPDF2\pdf.py", line 1651, in _mergePage
    page2Content, rename, self.pdf)   File "C:Python27\site-packages\PyPDF2\pdf.py", line 1547, in
_contentStreamRename
    op = operands[i] KeyError: 0

using python 2.7.6 with pypdf2 1.19 on windows 32bit. hopefully someone can tell me what i do wrong.

my python file:

from PyPDF2 import PdfFileWriter, PdfFileReader

output = PdfFileWriter()
input = PdfFileReader(open("test.pdf", "rb"))
watermark = PdfFileReader(open("watermark.pdf", "rb"))

# print how many pages input1 has:
print("test.pdf has %d pages." % input.getNumPages())
print("watermark.pdf has %d pages." % watermark.getNumPages())

# add page 0 from input, but first add a watermark from another PDF:
page = input.getPage(0)
page.mergePage(watermark.getPage(0))
output.addPage(page)

# finally, write "output" to document-output.pdf
outputStream = file("outputs.pdf", "wb")
output.write(outputStream)
outputStream.close()

Solution

  • Try writing to a StringIO object instead of a disk file. So, replace this:

    outputStream = file("outputs.pdf", "wb")
    output.write(outputStream)
    outputStream.close()
    

    with this:

    outputStream = StringIO.StringIO()
    output.write(outputStream) #write merged output to the StringIO object
    outputStream.close()
    

    If above code works, then you might be having file writing permission issues. For reference, look at the PyPDF working example in my article.