In my current Project I need to read signature annotations and then remove them from the PDF completely. Since the Project is Laravel based I really wanna use PHP to achieve that too.
It seems so that there is no modern library to do that.
I’ve found plenty of libraries like FPDI, FPDF, TCPDF but they cant really work with manipulating. They dont even support importing and preserve form fields (dynamic fields) in general.
In python its like an 50 Line thing to achieve what I want, maybe it helps to understand, what I wanna achieve in PHP:
import pikepdf
with pikepdf.Pdf.open('test_pdfs/test.pdf') as pdf:
signatures = []
fields = pdf.Root.get('/AcroForm').get('/Fields')
if fields:
for field_num, field in enumerate(fields):
if field.get('/FT') == '/Sig':
fields[field_num] = None
for page_num, page in enumerate(pdf.pages):
if "/Annots" in page:
pageSize = page['/MediaBox']
for annot_num, annot in enumerate(page['/Annots']):
if "/FT" in annot and annot['/FT'] == "/Sig":
name = annot['/T']
rect = annot['/Rect']
# get the coordinates
x = rect[0] / pageSize[2]
y = (pageSize[3] - rect[1]) / pageSize[3]
width = (rect[2] - rect[0]) / pageSize[2]
height = (rect[3] - rect[1]) / pageSize[3]
signatures.append({
"name": name,
"x": x,
"y": y,
"width": width,
"height": height
})
# remove the annotation
pdf.pages[page_num]['/Annots'][annot_num] = None
print(signatures)
pdf.save('test_pdfs/test_nosigs.pdf')
Maybe I missed a library for that. Would be great if someone knows something 🙂
user25511795 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.