22from backend import Fill
33from commonforms import prepare_form
44from pypdf import PdfReader
5+ from pathlib import Path
6+ from typing import Union
57
68def input_fields (num_fields : int ):
79 fields = []
@@ -10,7 +12,7 @@ def input_fields(num_fields: int):
1012 fields .append (field )
1113 return fields
1214
13- def run_pdf_fill_process (user_input : str , definitions : list , pdf_form_path : str ):
15+ def run_pdf_fill_process (user_input : str , definitions : list , pdf_form_path : Union [ str , os . PathLike ] ):
1416 """
1517 This function is called by the frontend server.
1618 It receives the raw data, runs the PDF filling logic,
@@ -20,6 +22,9 @@ def run_pdf_fill_process(user_input: str, definitions: list, pdf_form_path: str)
2022 print ("[1] Received request from frontend." )
2123 print (f"[2] PDF template path: { pdf_form_path } " )
2224
25+ # Normalize Path/PathLike to a plain string for downstream code
26+ pdf_form_path = os .fspath (pdf_form_path )
27+
2328 if not os .path .exists (pdf_form_path ):
2429 print (f"Error: PDF template not found at { pdf_form_path } " )
2530 return None # Or raise an exception
@@ -45,11 +50,14 @@ def run_pdf_fill_process(user_input: str, definitions: list, pdf_form_path: str)
4550
4651
4752if __name__ == "__main__" :
48- file = "[ENTER_DIR_HERE]/FireForm/src/inputs/file.pdf"
53+ BASE_DIR = Path (__file__ ).resolve ().parent
54+ file = BASE_DIR / "inputs" / "file.pdf"
55+ prepared_pdf = BASE_DIR / "temp_outfile.pdf"
56+
57+ #file = "/Users/vincentharkins/Desktop/FireForm/src/inputs/file.pdf"
4958 user_input = "Hi. The employee's name is John Doe. His job title is managing director. His department supervisor is Jane Doe. His phone number is 123456. His email is jdoe@ucsc.edu. The signature is <Mamañema>, and the date is 01/02/2005"
5059 descriptions = ["Employee's name" , "Employee's job title" , "Employee's department supervisor" , "Employee's phone number" , "Employee's email" , "Signature" , "Date" ]
51- prepared_pdf = "temp_outfile.pdf"
52- prepare_form (file ,prepared_pdf )
60+ prepare_form (str (file ), str (prepared_pdf ))
5361
5462 reader = PdfReader (prepared_pdf )
5563 fields = reader .get_fields ()
@@ -58,8 +66,6 @@ def run_pdf_fill_process(user_input: str, definitions: list, pdf_form_path: str)
5866 else :
5967 num_fields = 0
6068
69+ descriptions = input_fields (num_fields ) # Uncomment to edit fields
6170
62-
63- #descriptions = input_fields(num_fields) # Uncomment to edit fields
64-
65- run_pdf_fill_process (user_input , descriptions , file )
71+ run_pdf_fill_process (user_input , descriptions , str (file ))
0 commit comments