Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions src/backend/expungeservice/form_filling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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]
Expand Down
25 changes: 2 additions & 23 deletions src/backend/expungeservice/form_filling_2026.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/backend/expungeservice/waiver_form_filling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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()

Expand Down