Created
October 7, 2021 20:26
-
-
Save stephanGarland/2be8ba5efdd7bbd0e3aa795a1116fe3a to your computer and use it in GitHub Desktop.
Modify a pre-signed TriNet HSA Contribution form with today's date and a dollar amount
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import argparse | |
from datetime import date | |
from PyPDF2 import PdfFileReader, PdfFileWriter | |
from PyPDF2.generic import BooleanObject, NameObject, IndirectObject | |
def setup_args(): | |
parser = argparse.ArgumentParser(description="Modify a PDF's fields for HSA contribution changes.") | |
parser.add_argument("-i", "--input", help="The input PDF file.", required=True) | |
parser.add_argument("-o", "--output", help="The output PDF file.", required=True) | |
parser.add_argument("-a", "--amount", help="The amount of the HSA contribution.", required=True) | |
return parser.parse_args() | |
def set_need_appearances_writer(writer: PdfFileWriter) -> PdfFileWriter: | |
catalog = writer._root_object | |
if "/AcroForm" not in catalog: | |
writer._root_object.update({ | |
NameObject("/AcroForm"): IndirectObject(len(writer._objects), 0, writer) | |
}) | |
need_appearances = NameObject("/NeedAppearances") | |
writer._root_object["/AcroForm"][need_appearances] = BooleanObject(True) | |
return writer | |
def init_reader(infile: str) -> PdfFileReader: | |
reader = PdfFileReader(infile, strict=False) | |
if "/AcroForm" in reader.trailer["/Root"]: | |
reader.trailer["/Root"]["/AcroForm"].update({ | |
NameObject("/NeedAppearances"): BooleanObject(True) | |
}) | |
return reader | |
def init_writer() -> PdfFileWriter: | |
writer = PdfFileWriter() | |
set_need_appearances_writer(writer) | |
if "/AcroForm" in writer._root_object: | |
writer._root_object["/AcroForm"].update({ | |
NameObject("/NeedAppearances"): BooleanObject(True) | |
}) | |
return writer | |
if __name__ == "__main__": | |
args = setup_args() | |
field_dict = { | |
"12": args.amount, | |
"14": date.today().isoformat() | |
} | |
reader = init_reader(args.input) | |
writer = init_writer() | |
writer.addPage(reader.getPage(0)) | |
writer.updatePageFormFieldValues(writer.getPage(0), field_dict) | |
with open(args.output, "wb") as f: | |
writer.write(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment