Skip to content

Instantly share code, notes, and snippets.

@7bitlyrus
Last active October 11, 2022 17:11
Show Gist options
  • Save 7bitlyrus/80a2a0065d123cb2c10ee3842ff6cb91 to your computer and use it in GitHub Desktop.
Save 7bitlyrus/80a2a0065d123cb2c10ee3842ff6cb91 to your computer and use it in GitHub Desktop.
Unflattens/unlocks form fields for a pdf file.
import pdfrw # pip install pdfrw
import sys
if not len(sys.argv) != 1:
print(f'usage: {sys.argv[0]} filename')
exit()
filename = sys.argv[1]
template_pdf = pdfrw.PdfReader(filename)
for Page in template_pdf.pages:
if Page['/Annots']:
for annotation in Page['/Annots']:
annotation.update(pdfrw.PdfDict(Ff=0))
if template_pdf.Root.AcroForm is not None:
template_pdf.Root.AcroForm.update(pdfrw.PdfDict(NeedAppearances=pdfrw.PdfObject('true')))
else:
print("form not found")
exit()
pdfrw.PdfWriter().write('unflatten-' + filename, template_pdf)
print(f'Written to unflatten-{filename}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment