PDFMiner: PDFMiner is a text extraction tool for PDF documents.
Install PDFMiner : pip install pdfminer
PyMuPDF: Python bindings for the PDF toolkit and renderer MuPDF
Install PyMuPDF : pip install PyMuPDF
You can use this script to fill signature fields with signature image. pdftk library fill the fillable form but it not support image files like append user signature. Below code, that fill your signature field with image.
import sys
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdftypes import resolve1
from pdfminer.pdfpage import PDFPage
import fillpdf
from fillpdf import fillpdfs
import fitz
from PIL import Image
def fill_signature(input_path, output_path, field_name, sign_image, page_no):
fp = open(input_path, 'rb')
parser = PDFParser(fp)
doc = PDFDocument(parser)
fields = resolve1(doc.catalog['AcroForm'])['Fields']
#print(fields)
for i in fields:
field = resolve1(i)
if field.get('T') == bytes(field_name, encoding="ascii"):
#name, value, coordinate, page_no = field.get('T'), field.get('V'), field.get('Rect'), field.get('LTPage')
field_coordinate = field.get('Rect')
#print(field_coordinate)
# define the position (upper-right corner)
#image_rectangle = fitz.Rect(166.663, 437.55, 316.663, 469.55)
image_rectangle = fitz.Rect(field_coordinate[0], field_coordinate[1], field_coordinate[2], field_coordinate[3])
# retrieve the first page of the PDF
page_no = int(page_no)
file_handle = fitz.open(input_path)
first_page = file_handle[page_no]
# add the image
first_page.insertImage(image_rectangle, filename=sign_image)
file_handle.save(output_path)
input_path = sys.argv[1]
output_path = sys.argv[2]
field_name = sys.argv[3]
sign_image = sys.argv[4]
page_no = sys.argv[5]
fill_signature(input_path, output_path, field_name, sign_image, page_no)