@@ -616,7 +616,6 @@ class FormFilling:
616616 COUNTIES_NEEDING_CONVICTION_OR_ARREST_ORDER = ["multnomah" ]
617617 COUNTIES_NEEDING_COUNTY_SPECIFIC_DOWNLOAD_NAME : List [str ] = []
618618 OSP_PDF_NAME = "OSP_Form"
619- #COMPILED_PDF_NAME = "COMPILED_MOTIONS"
620619
621620 @staticmethod
622621 def build_zip (record_summary : RecordSummary , user_information_dict : Dict [str , str ], summary : bytes , summary_filename : str ) -> Tuple [str , str ]:
@@ -649,20 +648,11 @@ def build_zip(record_summary: RecordSummary, user_information_dict: Dict[str, st
649648 osp_file_info = FormFilling ._create_and_write_pdf (user_information_dict_2 , temp_dir )
650649 zip_file .write (* osp_file_info [0 :2 ])
651650
652- #todo: refactor and build separate method to compose compiled
653651 if all_motions_to_set_aside :
654- compiled = PdfWriter ()
655- compiled .addpages (PdfReader (all_motions_to_set_aside .pop (0 )[0 ]).pages )
656- for f in all_motions_to_set_aside :
657- compiled .addpages (PdfReader (f [0 ]).pages )
658-
659- compiled .addpages (PdfReader (osp_file_info [0 ]).pages )
660-
661- compiled .trailer .Root .AcroForm = PdfDict (NeedAppearances = PdfObject ("true" ))
662-
652+ file_paths = [f [0 ] for f in all_motions_to_set_aside ] + [osp_file_info [0 ]]
663653 comp_name = "COMPILED.pdf"
664654 comp_path = path .join (temp_dir , comp_name )
665- compiled . write ( comp_path )
655+ FormFilling . compile_pdfs ( file_paths , comp_path )
666656 zip_file .write (comp_path , comp_name )
667657
668658
@@ -673,6 +663,31 @@ def build_zip(record_summary: RecordSummary, user_information_dict: Dict[str, st
673663
674664 return zip_path , zip_file_name
675665
666+ @staticmethod
667+ def rename_fields (reader : PdfReader , start_index : int ) -> int :
668+ acro_form = reader .Root .AcroForm
669+ if not acro_form or not acro_form .Fields :
670+ return 0
671+ fields = acro_form .Fields
672+ for i , field in enumerate (fields ):
673+ if field .get (PdfName ('T' )):
674+ old_name = field [PdfName ('T' )].to_unicode ()
675+ new_name = f"{ old_name } _{ start_index + i } "
676+ field [PdfName ('T' )] = new_name
677+ return len (fields )
678+
679+ @staticmethod
680+ def compile_pdfs (file_paths : List [str ], output_path : str ) -> None :
681+ compiled = PdfWriter ()
682+ start_index = 0
683+ for file_path in file_paths :
684+ reader = PdfReader (file_path )
685+ field_count = FormFilling .rename_fields (reader , start_index )
686+ start_index += field_count
687+ compiled .addpages (reader .pages )
688+ compiled .trailer .Root .AcroForm = PdfDict (NeedAppearances = PdfObject ("true" ))
689+ compiled .write (output_path )
690+
676691 @staticmethod
677692 def build_summary_filename (aliases ):
678693 first_alias = aliases [0 ]
0 commit comments