diff --git a/src/pdf_filler.py b/src/pdf_filler.py index b5f63a4..2ca82da 100644 --- a/src/pdf_filler.py +++ b/src/pdf_filler.py @@ -11,7 +11,7 @@ from pathlib import Path from typing import Any, Dict, Optional from pypdf import PdfReader, PdfWriter -from pypdf.generic import BooleanObject, NameObject, NumberObject +from pypdf.generic import BooleanObject, NameObject, NumberObject, TextStringObject from .config_loader import load_config from .exceptions import PdfTemplateError @@ -99,9 +99,17 @@ def _update_widget_visibility(writer: PdfWriter, fields: Dict[str, str]) -> None hidden bit at runtime when ``anwesend.N`` is selected. Because we write values directly without executing JavaScript we must clear the hidden bit ourselves for every row where a value is present. + + Also patches the Default Appearance (/DA) of summary fields: the template + uses ``1 g`` (white fill color) because JavaScript normally writes the /AP. + With NeedAppearances=True pymupdf regenerates /AP from /DA — white text on + a white background is invisible. Replace ``1 g`` with ``0 g`` (black) so + the regenerated text is visible. """ HIDDEN_FIELD_PREFIXES = ("ist.", "plan.") HIDDEN_BIT = 2 # PDF annotation flag bit 2 = Hidden + # Summary fields whose /DA may use white text (1 g); patch to black (0 g). + SUM_FIELDS = {"anwesend_sum", "pauschale_5_sum", "pauschale_25_sum", "pauschale_sum"} for page in writer.pages: annots = page.get("/Annots") @@ -117,15 +125,20 @@ def _update_widget_visibility(writer: PdfWriter, fields: Dict[str, str]) -> None else: full_name = t - if not any(full_name.startswith(p) for p in HIDDEN_FIELD_PREFIXES): - continue + # --- Hidden-bit fix for ist.N / plan.N --- + if any(full_name.startswith(p) for p in HIDDEN_FIELD_PREFIXES): + has_value = bool(fields.get(full_name, "")) + current_f = int(annot.get("/F", 4)) + # Remove hidden bit when value present, keep/set it when absent + new_f = (current_f & ~HIDDEN_BIT) if has_value else (current_f | HIDDEN_BIT) + if new_f != current_f: + annot[NameObject("/F")] = NumberObject(new_f) - has_value = bool(fields.get(full_name, "")) - current_f = int(annot.get("/F", 4)) - # Remove hidden bit when value present, keep/set it when absent - new_f = (current_f & ~HIDDEN_BIT) if has_value else (current_f | HIDDEN_BIT) - if new_f != current_f: - annot[NameObject("/F")] = NumberObject(new_f) + # --- /DA color fix for summary fields --- + if full_name in SUM_FIELDS: + da = str(annot.get("/DA", "")) + if da and "1 g" in da: + annot[NameObject("/DA")] = TextStringObject(da.replace("1 g", "0 g")) def fill_pdf(