diff --git a/src/backend/expungeservice/form_filling.py b/src/backend/expungeservice/form_filling.py index 55247c3f8..125fd1407 100644 --- a/src/backend/expungeservice/form_filling.py +++ b/src/backend/expungeservice/form_filling.py @@ -648,32 +648,11 @@ def build_zip(record_summary: RecordSummary, user_information_dict: Dict[str, st osp_file_info = FormFilling._create_and_write_pdf(user_information_dict_2, temp_dir) zip_file.write(*osp_file_info[0:2]) - #todo: refactor and build separate method to compose compiled if all_motions_to_set_aside: - compiled = PdfWriter() - - # Must rename all the fields in the file so they are unique so that Acrobat doesn't mess with their values. - reader = PdfReader(all_motions_to_set_aside.pop(0)[0]) - start_index = 0 - field_count = FormFilling.rename_fields(reader, start_index) - start_index += field_count - compiled.addpages(reader.pages) - for f in all_motions_to_set_aside: - reader = PdfReader(f[0]) - field_count = FormFilling.rename_fields(reader, start_index) - start_index += field_count - compiled.addpages(reader.pages) - - reader = PdfReader(osp_file_info[0]) - FormFilling.rename_fields(reader, start_index) - compiled.addpages(reader.pages) - - # Must update the appearances property so that the fields render correctly in Acrobat. - compiled.trailer.Root.AcroForm = PdfDict(NeedAppearances=PdfObject("true")) - + file_paths = [f[0] for f in all_motions_to_set_aside] + [osp_file_info[0]] comp_name = "COMPILED.pdf" comp_path = path.join(temp_dir, comp_name) - compiled.write(comp_path) + FormFilling.compile_pdfs(file_paths, comp_path) zip_file.write(comp_path, comp_name) @@ -697,6 +676,18 @@ def rename_fields(reader: PdfReader, start_index: int) -> int: field[PdfName('T')] = new_name return len(fields) + @staticmethod + def compile_pdfs(file_paths: List[str], output_path: str) -> None: + compiled = PdfWriter() + start_index = 0 + for file_path in file_paths: + reader = PdfReader(file_path) + field_count = FormFilling.rename_fields(reader, start_index) + start_index += field_count + compiled.addpages(reader.pages) + compiled.trailer.Root.AcroForm = PdfDict(NeedAppearances=PdfObject("true")) + compiled.write(output_path) + @staticmethod def build_summary_filename(aliases): first_alias = aliases[0] diff --git a/src/backend/expungeservice/form_filling_2026.py b/src/backend/expungeservice/form_filling_2026.py index e8bbabd3e..8e18dc54f 100644 --- a/src/backend/expungeservice/form_filling_2026.py +++ b/src/backend/expungeservice/form_filling_2026.py @@ -17,8 +17,6 @@ from typing import Any, Dict, List, Optional, Tuple from zipfile import ZipFile -from pdfrw import PdfReader, PdfWriter, PdfDict, PdfObject - from expungeservice.form_filling import ( CaseResults, DA_ADDRESSES, @@ -325,29 +323,10 @@ def build_zip( # Build compiled PDF if all_motions_to_set_aside: - compiled = PdfWriter() - - # Must rename all the fields in the file so they are unique so that Acrobat doesn't mess with their values. - reader = PdfReader(all_motions_to_set_aside.pop(0)[0]) - start_index = 0 - field_count = OldFormFilling.rename_fields(reader, start_index) - start_index += field_count - compiled.addpages(reader.pages) - for f in all_motions_to_set_aside: - reader = PdfReader(f[0]) - field_count = OldFormFilling.rename_fields(reader, start_index) - start_index += field_count - compiled.addpages(reader.pages) - - reader = PdfReader(osp_file_info[0]) - OldFormFilling.rename_fields(reader, start_index) - compiled.addpages(reader.pages) - - compiled.trailer.Root.AcroForm = PdfDict(NeedAppearances=PdfObject("true")) - + file_paths = [f[0] for f in all_motions_to_set_aside] + [osp_file_info[0]] comp_name = "COMPILED.pdf" comp_path = path.join(temp_dir, comp_name) - compiled.write(comp_path) + OldFormFilling.compile_pdfs(file_paths, comp_path) zip_file.write(comp_path, comp_name) # Add summary PDF diff --git a/src/backend/expungeservice/waiver_form_filling.py b/src/backend/expungeservice/waiver_form_filling.py index 460407724..b9d3d08c5 100644 --- a/src/backend/expungeservice/waiver_form_filling.py +++ b/src/backend/expungeservice/waiver_form_filling.py @@ -10,7 +10,7 @@ from expungeservice.models.record_summary import RecordSummary from expungeservice.models.case import Case -from expungeservice.form_filling import DA_ADDRESSES +from expungeservice.form_filling import DA_ADDRESSES, FormFilling def wrap_text(text): text = re.sub(r'\r\n?|\n', ' ', text) @@ -84,6 +84,7 @@ def build_zip( zip_path = path.join(mkdtemp(), zip_file_name) zip_file = ZipFile(zip_path, "w") + all_waiver_files = [] for case in record_summary.record.cases: if ( case.summary.balance_due_in_cents > 0 @@ -92,6 +93,13 @@ def build_zip( case_data = CaseData(case, user_information_dict, waiver_information_dict) file_info = WaiverFormFilling._create_and_write_pdf(case_data, temp_dir) zip_file.write(*file_info[0:2]) + all_waiver_files.append(file_info) + + if all_waiver_files: + file_paths = [f[0] for f in all_waiver_files] + comp_path = path.join(temp_dir, "COMPILED_FINES_AND_FEES.pdf") + FormFilling.compile_pdfs(file_paths, comp_path) + zip_file.write(comp_path, "COMPILED_FINES_AND_FEES.pdf") zip_file.close()