0

This is a basic script that should insert a watermark image on the first page of a PDF and save it under a new name. I could do the same with the same files in pdfrw, but I'm stuck with PyMuPDF (which I would prefer to use...). The py file is in the same folder with the pdf and the png.

import fitz

input_file = "sample.pdf"
output_file = "sample_stamped.pdf"
stamp = "watermark.png"

doc = fitz.open(input_file)

rect = fitz.Rect(0, 0, 100, 100)
pix = fitz.Pixmap(stamp)

page = doc[0]
page.insertImage(rect, pixmap = pix, overlay = True)
doc.save(output_file)

I'm getting this error msg:

Traceback (most recent call last):
  File "D:/Google Drive/Python/PDF/pdfstamp.py", line 14, in <module>
    doc.save(output_file)
  File "D:\Python\lib\site-packages\fitz\fitz.py", line 2411, in save
    return _fitz.Document_save(self, filename, garbage, clean, deflate, incremental, ascii, expand, linear, pretty, decrypt)
RuntimeError: not a dict (array)

Thanks in advance for any help.

Krisztian
  • 21
  • 2
  • 4
  • 1
    Looks like issue with the pyMuPDF version you are using. The issue is fixed from version 1.14.19.2 and later Refer https://github.com/pymupdf/PyMuPDF/issues/332 – Suvin K S Sep 26 '19 at 06:41
  • Thanks @SuvinKS! This is the correct answer. The same code that I posted above works fine after upgrading the library. – Krisztian Sep 27 '19 at 10:15

0 Answers0