PDF Files That Have Obnoxious Security Features

wiki.TerraBase.info
Revision as of 18:00, 21 October 2025 by Root (talk | contribs)
Jump to navigation Jump to search

...let's solve that problem;

For Rocky Linux 9;

dnf dnf install -y python3-pip perl-Image-ExifTool qpdf

pip3 install --user pikepdf

The below script actually works (but could be improved)

...just change permissions chmod 755 WhatEverPYName.py, and then: ./WhatEverPYName.py

python3 - << 'PY'
from pikepdf import Pdf, Name

src = "cs.pdf"
dst = "cs_unlocked.pdf"

with Pdf.open(src) as pdf:
	# Get catalog (Root) via trailer — works across pikepdf versions
	root = pdf.trailer['/Root']

	# Remove document-level certification/permissions and related dicts
	for key in ('/Perms', '/DSS', '/VRI'):
		if key in root:
			del root[key]

	# Clean AcroForm: drop signature flags and Sig fields; keep other fields/buttons
	acro = root.get('/AcroForm')
	if acro:
		if '/SigFlags' in acro:
			del acro['/SigFlags']
		fields = acro.get('/Fields')
		if fields:
			new_fields = []
			for f in fields:
				if f.get('/FT') == Name('/Sig'):
					continue  # remove signature field
				new_fields.append(f)
			acro['/Fields'] = new_fields

	# Also remove signature widgets from page annotations (keeps other widgets)
	for page in pdf.pages:
		annots = page.get('/Annots')
		if annots:
			page['/Annots'] = [a for a in annots if a.get('/FT') != Name('/Sig')]

	pdf.save(dst)
print("Wrote", dst)
PY