PDF Files That Have Obnoxious Security Features

wiki.TerraBase.info
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

***Keep in mind there is a 'funky hoop jumping thing' if using Adobe Acrobat "classicish" version, where if you want to change say the Document Title, do the following;

  • Open the File
  • Select File, Properties, Security Tab, Change Settings (enable everything, set a blank or WhatEver Password)
  • Save the File
  • Reopen the File
  • Select File, Properties, Security Tab, Change Settings, just click OK, then OK again.
  • Select File, Properties, Security Tab, Change Settings (yes again, back to the same place), Description Tab
  • It is now editable (for now, rinse and repeat for later)
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