Reputation: 39
I am trying to add a png sign to the PDF by using a python code and the code that i'm running use PyMuPDF
and fitz
library.
import fitz
input_file = "example.pdf"
output_file = "example-with-sign.pdf"
barcode_file = "sign.png"
# define the position (upper-right corner)
image_rectangle = fitz.Rect(450,20,550,120)
# retrieve the first page of the PDF
file_handle = fitz.open(input_file)
first_page = file_handle[0]
# add the image
first_page.insertImage(image_rectangle, fileName=barcode_file)
file_handle.save(output_file)
Upvotes: 0
Views: 7681
Reputation: 1754
I got stuck with a very similar error to your question. The insert_image
solution you posted is correct, but I think the reason is that from a certain version of PyMuPDF, camelCase (which was good to use before) was totally deprecated, and was replaced by under_score_case. I think it's necessary to mark the PyMuPDF version here, just in case somebody was confused by these two coding styles when they met similar errors in the future.
Currently, I'm under PyMuPDF==1.21.0
, in which I'm pretty sure camelCase was totally deprecated. So if you met a similar error, just try to convert your method someMethod()
into some_method()
.
See: doc is here
Upvotes: 1
Reputation: 39
Thank you for 'insert_image' correction. It currently works as follows:
import fitz
input_file = "example.pdf"
output_file = "example-with-sign.pdf"
# define the position (upper-right corner)
image_rectangle = fitz.Rect(450,20,550,120)
# retrieve the first page of the PDF
file_handle = fitz.open(input_file)
first_page = file_handle[0]
img = open("sign.png", "rb").read() # an image file
img_xref = 0
first_page.insert_image(image_rectangle,stream=img,xref=img_xref)
file_handle.save(output_file)
Upvotes: 3