Skip to content
Open
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
43 changes: 43 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
86 changes: 54 additions & 32 deletions src/unified_field_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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"
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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":
Expand All @@ -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):
Expand Down Expand Up @@ -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")
Expand All @@ -1333,18 +1353,18 @@ 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:
pymupdf.message(f"Set '{field_name}' = '{new_value}'")

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):
"""
Expand Down Expand Up @@ -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.
"""
Expand All @@ -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
Loading
Loading