diff --git a/src/__init__.py b/src/__init__.py index dee3ae6f9..fdc31f010 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -5571,6 +5571,49 @@ def insert_pdf( self.Graftmaps[isrt] = None #log( 'insert_pdf(): returning') + def recalculate_form(self): + """Recalculate the PDF form fields having /AA/C JavaScripts. + + We first remove all existing appearance dictionaries to + enforce their regeneration via JavaScript code.""" + + pdfdoc = _as_pdf_document(self) # underlying PDF document + + def drop_appearances(obj, visited): + """Recursively remove /AP objects.""" + xref = obj.pdf_to_num() + if xref in visited: # avoid any cycles + return + visited.add(xref) + + obj.pdf_dict_del(PDF_NAME("AP")) # is a no-op if not found + + kids = obj.pdf_dict_get(PDF_NAME("Kids")) + for i in range(kids.pdf_array_len()): + drop_appearances(kids.pdf_array_get(i), visited) + + # relevant fields are in the following global array + CO = mupdf.pdf_dict_getl( + mupdf.pdf_trailer(pdfdoc), + PDF_NAME("Root"), + PDF_NAME("AcroForm"), + PDF_NAME("CO"), + ) + visited = set() + + for i in range(CO.pdf_array_len()): + drop_appearances(CO.pdf_array_get(i), visited) + + # enable JavaScript and regenerate all cross-calculated fields + try: + mupdf.pdf_enable_js(pdfdoc) + if not mupdf.pdf_js_supported(pdfdoc): + raise RuntimeError("JavaScript engine failure") + mupdf.pdf_calculate_form(pdfdoc) + except Exception as e: + message(f"cannot recalculate the PDF: {e}") + return + @property def is_dirty(self): pdf = _as_pdf_document(self, required=0) diff --git a/src/unified_field_model.py b/src/unified_field_model.py index f7b25afa9..7a16e228d 100644 --- a/src/unified_field_model.py +++ b/src/unified_field_model.py @@ -4,17 +4,11 @@ import re import time from io import StringIO - import pymupdf from pymupdf import NON_WIDGET_KEYS, PDF_NAME, mupdf -try: - import pandas as PANDAS -except ImportError: - PANDAS = None - PDF_NAME_Blur = mupdf.pdf_new_name("Bl") # missing in mupdf.py -PDF_NAME_Focus = mupdf.pdf_new_name("Fo") # codespell:ignore Fo +PDF_NAME_Focus = mupdf.pdf_new_name("Fo") class FormFields: @@ -44,9 +38,11 @@ def __init__(self, doc, repair=False, verbose=False): self.count_flat_fields or self.count_flat_rbgs or self.count_field_widgets ): pymupdf.message("Found PDF specification issues:") - pymupdf.message(f" flat RBGs: {self.count_flat_rbgs}\n" - f" flat fields: {self.count_flat_fields}\n" - f" field-widgets: {self.count_field_widgets}") + pymupdf.message( + f" flat RBGs: {self.count_flat_rbgs}\n" + f" flat fields: {self.count_flat_fields}\n" + f" field-widgets: {self.count_field_widgets}" + ) if not self.repair: pymupdf.message("Repair is recommended.\n") else: @@ -82,7 +78,7 @@ def refresh(self): self.count_flat_rbgs = 0 self.count_field_widgets = 0 self.page_xrefs = {} # xref to page number if widgets present - self.template_xrefs = [] # list of /Type/Template xrefs having widgets + self.template_xrefs = set() # list of /Type/Template xrefs having widgets self.widgets = self._iter_page_widgets() # walk through page PDF objects self._repair_widgets() # indispensable repairs! # extract form fields starting from /AcroForm/Fields @@ -463,7 +459,7 @@ def walk_field(field, prefix=""): value_dict = {"field_value": value, "pages": sorted(pages)} if templates: value_dict["template_xrefs"] = sorted(templates) - self.template_xrefs.extend(templates) + self.template_xrefs |= templates if xrefs: value_dict["xref"] = field_xref value_dict["terminal"] = terminal @@ -810,12 +806,15 @@ def classify_rbg(widgets): choice_xrefs = [] for choice in choices: if rbg_type == "js": - choice_xref = [w.xref for w in widgets if w.field_name == choice][0] + choice_xref = next( + (w.xref for w in widgets if w.field_name == choice), 0 + ) else: - choice_xref = [w.xref for w in widgets if w.btn_on_state == choice][ - 0 - ] - choice_xrefs.append(choice_xref) + choice_xref = next( + (w.xref for w in widgets if w.btn_on_state == choice), 0 + ) + if choice_xref: + choice_xrefs.append(choice_xref) entry["choice_xrefs"] = choice_xrefs def _determine_field_type(self, entry): @@ -827,6 +826,19 @@ def _determine_field_type(self, entry): w = widgets[0] ft = w.field_type_string.lower() + # take note if field is computed + if w.script_calc: + entry["script_calc"] = True + elif entry.get("terminal") and entry.get("xref"): + xref = entry["xref"] + if isinstance(xref, (list, tuple)): + xref = xref[0] if xref else 0 + if xref: + entry_obj = mupdf.pdf_load_object(self.pdfdoc, xref) + aac = entry_obj.pdf_dict_getl(PDF_NAME("AA"), PDF_NAME("C")) + if aac.pdf_is_dict(): + entry["script_calc"] = True + # TEXT FIELD if "text" in ft: entry["type"] = "text" @@ -1122,12 +1134,14 @@ def repair_field_widget(self, field_name): self.fields[field_name] = entry return True - def set_value(self, field_name, value): + def set_value(self, field_name, value, recalculate=False): """Set the value of a field identified by field_name. Args: field_name (str): The name of the field to update. value: The value to set for the field. + recalculate (bool): Whether to recalculate all fields. This will + trigger recalculation of all inter-fields dependencies. """ def update_widget_lowlevel(w, value, choices=None, rbg_type=None): @@ -1155,7 +1169,10 @@ def update_widget_lowlevel(w, value, choices=None, rbg_type=None): parent = w_obj.pdf_dict_get(PDF_NAME("Parent")) if not parent.pdf_is_dict(): # if no parent, use the widget parent = w_obj - if value in (on, on_decoded): # pylint: disable=possibly-used-before-assignment + if value in ( + on, # pylint: disable=possibly-used-before-assignment + on_decoded, # pylint: disable=possibly-used-before-assignment + ): w_obj.pdf_dict_put(PDF_NAME("AS"), onstate) parent.pdf_dict_put(PDF_NAME("V"), onstate) else: @@ -1264,6 +1281,8 @@ def update_widget_lowlevel(w, value, choices=None, rbg_type=None): update_widget_lowlevel(w, value, choices=choices, rbg_type=rbg_type) entry["value"] = value + if recalculate: + self.doc.recalculate_form() return True elif ftype == "choice": @@ -1277,7 +1296,8 @@ def update_widget_lowlevel(w, value, choices=None, rbg_type=None): # --- update widgets --- for w in widgets: update_widget_lowlevel(w, value, choices=choices, rbg_type=rbg_type) - + if recalculate: + self.doc.recalculate_form() return True def to_dicts(self): @@ -1319,7 +1339,7 @@ def to_json(self, file=None): else: return json_str - def from_dicts(self, dicts): + def from_dicts(self, dicts, recalculate=True): """Import form field value changes from a Python list of dictionaries.""" for item in dicts: field_name = item.get("field") @@ -1333,7 +1353,7 @@ def from_dicts(self, dicts): pymupdf.message(f"Warning: unknown field '{field_name}' in import") continue - ok = self.set_value(field_name, new_value) + ok = self.set_value(field_name, new_value, recalculate=recalculate) if not ok: pymupdf.message(f"Failed to set '{field_name}' = '{new_value}'") else: @@ -1341,10 +1361,10 @@ def from_dicts(self, dicts): pymupdf.message("Import from dicts completed.") - def from_json(self, json_str): + def from_json(self, json_str, recalculate=True): """Import form field value changes from a JSON string.""" dicts = json.loads(json_str) - return self.from_dicts(dicts) + return self.from_dicts(dicts, recalculate=recalculate) def to_csv(self, file=None): """ @@ -1400,7 +1420,7 @@ def to_csv(self, file=None): return return csv_content - def _apply_csv_updates(self, csv_str): + def _apply_csv_updates(self, csv_str, recalculate=True): """ Read a CSV with a 'field' and 'new_value' column and apply updates. """ @@ -1410,19 +1430,21 @@ def _apply_csv_updates(self, csv_str): name = row.get("field") new_value = row.get("new_value") if name and new_value not in (None, ""): - self.set_value(name, new_value) + self.set_value(name, new_value, recalculate=recalculate) - def from_csv(self, filepath): + def from_csv(self, filepath, recalculate=True): csv_str = pathlib.Path(filepath).read_bytes().decode() - self._apply_csv_updates(csv_str) + self._apply_csv_updates(csv_str, recalculate=recalculate) pymupdf.message(f"from_csv({filepath}) completed.") def to_pandas(self): """ - Return a pandas DataFrame with all editable fields and metadata. + Return a pandas DataFrame with editable fields. """ - if PANDAS is None: - raise ImportError("pandas is required but not installed.") + try: + import pandas as pd + except ImportError as e: + raise ImportError("pandas is required but not installed.") from e buffer = StringIO(self.to_csv()) - df = PANDAS.read_csv(buffer) + df = pd.read_csv(buffer) return df diff --git a/tests/test_formfields.py b/tests/test_formfields.py index 1de5b9cb8..119d8dac0 100644 --- a/tests/test_formfields.py +++ b/tests/test_formfields.py @@ -20,7 +20,7 @@ def test_set_value(): doc = pymupdf.open(filename) ff = doc.get_form_fields() field_info = ff.field_info("Person.MaritalStatus") - assert field_info["value"] == "married", f'{field_info["value"]=}' + assert field_info["value"] == "married", f"{field_info['value']=}" ff.set_value("Person.MaritalStatus", "single") data = doc.tobytes() doc.close() @@ -139,8 +139,16 @@ def test_to_csv(): assert len(lines) > 1 # At least header + one field # Check header (strip \r from Windows line endings) - header = lines[0].rstrip('\r') - expected_headers = ["field", "type", "format", "editable", "choices", "current_value", "new_value"] + header = lines[0].rstrip("\r") + expected_headers = [ + "field", + "type", + "format", + "editable", + "choices", + "current_value", + "new_value", + ] assert header == ",".join(expected_headers) # Verify at least one data row @@ -153,7 +161,7 @@ def test_to_csv_file_export(): doc = pymupdf.open(filename) ff = doc.get_form_fields() - with tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False) as tmp: + with tempfile.NamedTemporaryFile(mode="w", suffix=".csv", delete=False) as tmp: tmp_path = tmp.name try: @@ -161,7 +169,7 @@ def test_to_csv_file_export(): assert result is None # Should not return when writing to file # Verify file was created and contains data - with open(tmp_path, 'r') as f: + with open(tmp_path, "r") as f: content = f.read() assert len(content) > 0 assert "field" in content @@ -198,7 +206,7 @@ def test_to_json_file_export(): doc = pymupdf.open(filename) ff = doc.get_form_fields() - with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as tmp: + with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as tmp: tmp_path = tmp.name try: @@ -206,7 +214,7 @@ def test_to_json_file_export(): assert result is None # Should not return when writing to file # Verify file was created and contains valid JSON - with open(tmp_path, 'r') as f: + with open(tmp_path, "r") as f: data = json.load(f) assert isinstance(data, list) assert len(data) > 0 @@ -244,16 +252,11 @@ def test_from_dicts(): original_value = ff.field_info("Person.MaritalStatus")["value"] valid_choices = ff.field_info("Person.MaritalStatus")["choices"] - # Use a valid choice (if original is "married", use "single", else use "married") + # Use a valid choice (if original is 'married', use 'single', else use 'married') new_value = "single" if original_value == "married" else "married" # Create update dict - updates = [ - { - "field": "Person.MaritalStatus", - "new_value": new_value - } - ] + updates = [{"field": "Person.MaritalStatus", "new_value": new_value}] ff.from_dicts(updates) @@ -274,12 +277,7 @@ def test_from_json(): new_value = "single" if original_value == "married" else "married" # Create JSON with updates using a valid choice - updates = [ - { - "field": "Person.MaritalStatus", - "new_value": new_value - } - ] + updates = [{"field": "Person.MaritalStatus", "new_value": new_value}] json_str = json.dumps(updates) ff.from_json(json_str) @@ -330,7 +328,11 @@ def test_multiple_field_updates(): # Get additional field names to update if available for name, info in ff.fields.items(): - if info["terminal"] and info["type"] == "text" and name != "Person.MaritalStatus": + if ( + info["terminal"] + and info["type"] == "text" + and name != "Person.MaritalStatus" + ): updates.append({"field": name, "new_value": "TestValue"}) break @@ -360,7 +362,15 @@ def test_field_type_detection(): assert len(field_types) > 0 # Each type should have valid format - valid_types = {"text", "radio", "checkbox", "choice", "button", "signature", "unknown"} + valid_types = { + "text", + "radio", + "checkbox", + "choice", + "button", + "signature", + "unknown", + } for field_type in field_types: assert field_type in valid_types @@ -376,3 +386,157 @@ def test_field_diagnosis(): diagnosis = info["diagnosis"] assert "ok" in diagnosis assert isinstance(diagnosis["ok"], bool) + + +def test_recalculate_form(): + """We make a non-trivial Form PDF with a calculated field. + + Then check whether the cross-field calculation works correctly. + """ + from pymupdf import mupdf, _as_pdf_document, PDF_NAME + + BLUE = pymupdf.pdfcolor["blue"] + GRAY = pymupdf.pdfcolor["lightgray"] + + doc = pymupdf.open() + pdfdoc = pymupdf._as_pdf_document(doc) + + page = doc.new_page() + page.insert_text( + (150, 70), "Customer Information Form", fontname="hebo", fontsize=20, color=BLUE + ) + # define rectangles for the label and value fields + r1 = pymupdf.Rect(50, 100, 150, 120) # rect of field label + r2 = r1 + (r1.width + 10, 0, r1.width + 50, 0) # rect of field value + delta = r1.height + 10 # distance to next row + + # insert some text to the left + page.insert_textbox(r1, "First Name:", align=pymupdf.TEXT_ALIGN_RIGHT) + w = pymupdf.Widget() + w.field_name = "Customer.Name.First" + w.field_value = "John" + w.field_type = pymupdf.PDF_WIDGET_TYPE_TEXT + w.rect = r2 + w.border_color = BLUE + w.border_width = 1 + w.fill_color = GRAY + annot = page.add_widget(w) + + r1 += (0, delta, 0, delta) + r2 += (0, delta, 0, delta) + + page.insert_textbox(r1, "Last Name:", align=pymupdf.TEXT_ALIGN_RIGHT) + w = pymupdf.Widget() + w.field_name = "Customer.Name.Last" + w.field_value = "Doe" + w.field_type = pymupdf.PDF_WIDGET_TYPE_TEXT + w.rect = r2 + w.border_color = BLUE + w.border_width = 1 + w.fill_color = GRAY + annot = page.add_widget(w) + + r1 += (0, delta, 0, delta) + r2 += (0, delta, 0, delta) + + page.insert_textbox(r1, "Full Name:", align=pymupdf.TEXT_ALIGN_RIGHT) + w = pymupdf.Widget() + w.field_name = "Customer.Fullname" + # w.field_value = '' + w.field_type = pymupdf.PDF_WIDGET_TYPE_TEXT + w.field_flags = w.field_flags | pymupdf.PDF_FIELD_IS_READ_ONLY + w.rect = r2 + w.fill_color = GRAY + w.script_calc = """event.value = this.getField('Customer.Name.First').value + ' ' + this.getField('Customer.Name.Last').value;""" + annot = page.add_widget(w) + + r1 += (0, 2 * delta, 0, 2 * delta) + r2 += (0, 2 * delta, 0, 2 * delta) + + page.insert_textbox(r1, "Street:", align=pymupdf.TEXT_ALIGN_RIGHT) + w = pymupdf.Widget() + w.field_name = "Customer.Address.Street" + w.field_value = "123 Birch Street" + w.field_type = pymupdf.PDF_WIDGET_TYPE_TEXT + w.rect = r2 + w.border_color = BLUE + w.border_width = 1 + w.fill_color = GRAY + annot = page.add_widget(w) + + r1 += (0, delta, 0, delta) + r2 += (0, delta, 0, delta) + + page.insert_textbox(r1, "City:", align=pymupdf.TEXT_ALIGN_RIGHT) + w = pymupdf.Widget() + w.field_name = "Customer.Address.City" + w.field_value = "Lakeside" + w.field_type = pymupdf.PDF_WIDGET_TYPE_TEXT + w.rect = r2 + w.border_color = BLUE + w.border_width = 1 + w.fill_color = GRAY + annot = page.add_widget(w) + + r1 += (0, delta, 0, delta) + r2 += (0, delta, 0, delta) + r2.x1 = r2.x0 + 20 # make the checkbox smaller + + # This is about how this Customer knows about about us. + page.insert_textbox(r1, "Word Of Mouth:", align=pymupdf.TEXT_ALIGN_RIGHT) + w = pymupdf.Widget() + w.field_name = "Customer.WordOfMouth" + w.field_type = pymupdf.PDF_WIDGET_TYPE_CHECKBOX + w.rect = r2 + w.field_value = True + w.border_color = BLUE + w.border_width = 1 + w.fill_color = GRAY + annot = page.add_widget(w) + + r1 += (0, delta, 0, delta) + r2 += (0, delta, 0, delta) + + page.insert_textbox(r1, "Marital Status", align=pymupdf.TEXT_ALIGN_RIGHT) + + r1 += (0, delta, 0, delta) + r2 += (0, delta, 0, delta) + + page.insert_textbox(r1, "Married", align=pymupdf.TEXT_ALIGN_RIGHT) + w = pymupdf.Widget() + w.field_name = "Customer.MaritalStatus" + w.field_value = "married" + w.field_type = pymupdf.PDF_WIDGET_TYPE_RADIOBUTTON + w.rect = r2 + w.border_color = BLUE + w.border_width = 1 + w.fill_color = GRAY + annot = page.add_widget(w) + + r1 += (0, delta, 0, delta) + r2 += (0, delta, 0, delta) + + # Adding a RadioButton with the same field name will add alternatives + # to the same button group. + # Note that the group buttons may be on different pages. + page.insert_textbox(r1, "Single", align=pymupdf.TEXT_ALIGN_RIGHT) + w = pymupdf.Widget() + w.field_name = "Customer.MaritalStatus" + w.field_value = "single" + w.field_type = pymupdf.PDF_WIDGET_TYPE_RADIOBUTTON + w.rect = r2 + w.border_color = BLUE + w.border_width = 1 + w.fill_color = GRAY + annot = page.add_widget(w) + # extract all field values to verify correctness + field_values_0 = {w.field_name: w.field_value for w in page.widgets()} + assert field_values_0["Customer.Fullname"] == "" # field not yet computed + doc.recalculate_form() + field_values_1 = {w.field_name: w.field_value for w in page.widgets()} + # assert that only the cross-computed field has changed + for k, v in field_values_1.items(): + if k == "Customer.Fullname": + assert v == "John Doe" + else: + assert v == field_values_0[k]