Skip to content

Instantly share code, notes, and snippets.

@llllvvuu
Last active August 16, 2024 11:41
Show Gist options
  • Save llllvvuu/4a977b5cab1c0fe0e82894fd278ff20c to your computer and use it in GitHub Desktop.
Save llllvvuu/4a977b5cab1c0fe0e82894fd278ff20c to your computer and use it in GitHub Desktop.
Salt pdf form fields to prevent collision when merging with another pdf. Tested with Python 3.9.18.
"""
Adds a salt to every form field key in the PDF so that
when you merge it with other PDFs, the values don't get clobbered.
"""
import argparse
import uuid
from PyPDF2 import PdfReader, PdfWriter
from PyPDF2.generic import IndirectObject, PdfObject, TextStringObject
def add_salt_to_pdf_field(input_obj: object, salt: uuid.UUID):
if not input_obj:
return
while isinstance(input_obj, IndirectObject):
input_obj = input_obj.get_object()
if isinstance(input_obj, list):
for input_obj in input_obj:
add_salt_to_pdf_field(input_obj, salt)
return
if isinstance(input_obj, PdfObject):
if input_obj.get("/Subtype") != "/Widget":
return
key = input_obj.get("/T")
print("current form key:", key)
print("new form key:", f"{key}_{salt}")
print("form value:", input_obj.get("/V"))
print()
input_obj.update({"/T": TextStringObject(f"{key}_{salt}")})
def add_salt_to_pdf_fields(input_pdf: str, output_pdf: str):
salt = uuid.uuid4()
with open(input_pdf, "rb") as file:
pdf_reader = PdfReader(file)
pdf_writer = PdfWriter()
for page in pdf_reader.pages:
add_salt_to_pdf_field(page.get("/Annots", None), salt)
_ = pdf_writer.add_page(page)
with open(output_pdf, "wb") as output:
_ = pdf_writer.write(output)
print(f"Modified PDF saved as: {output_pdf}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=__doc__)
_ = parser.add_argument("input_pdf", help="Path to the input PDF file")
_ = parser.add_argument("output_pdf", help="Path to save the modified PDF file")
args = parser.parse_args()
add_salt_to_pdf_fields(args.input_pdf, args.output_pdf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment