PDF ausfüllen: Textfarbe in den Summenfeldern unter Tabelle von weiß auf schwarz geändert
This commit is contained in:
1 parent
bf671a9acd
commit
c51eddcb73
1 file changed
+22
-9
+22
-9
@@ -11,7 +11,7 @@ from pathlib import Path
|
|||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
from pypdf import PdfReader, PdfWriter
|
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 .config_loader import load_config
|
||||||
from .exceptions import PdfTemplateError
|
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
|
hidden bit at runtime when ``anwesend.N`` is selected. Because we write
|
||||||
values directly without executing JavaScript we must clear the hidden bit
|
values directly without executing JavaScript we must clear the hidden bit
|
||||||
ourselves for every row where a value is present.
|
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_FIELD_PREFIXES = ("ist.", "plan.")
|
||||||
HIDDEN_BIT = 2 # PDF annotation flag bit 2 = Hidden
|
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:
|
for page in writer.pages:
|
||||||
annots = page.get("/Annots")
|
annots = page.get("/Annots")
|
||||||
@@ -117,15 +125,20 @@ def _update_widget_visibility(writer: PdfWriter, fields: Dict[str, str]) -> None
|
|||||||
else:
|
else:
|
||||||
full_name = t
|
full_name = t
|
||||||
|
|
||||||
if not any(full_name.startswith(p) for p in HIDDEN_FIELD_PREFIXES):
|
# --- Hidden-bit fix for ist.N / plan.N ---
|
||||||
continue
|
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, ""))
|
# --- /DA color fix for summary fields ---
|
||||||
current_f = int(annot.get("/F", 4))
|
if full_name in SUM_FIELDS:
|
||||||
# Remove hidden bit when value present, keep/set it when absent
|
da = str(annot.get("/DA", ""))
|
||||||
new_f = (current_f & ~HIDDEN_BIT) if has_value else (current_f | HIDDEN_BIT)
|
if da and "1 g" in da:
|
||||||
if new_f != current_f:
|
annot[NameObject("/DA")] = TextStringObject(da.replace("1 g", "0 g"))
|
||||||
annot[NameObject("/F")] = NumberObject(new_f)
|
|
||||||
|
|
||||||
|
|
||||||
def fill_pdf(
|
def fill_pdf(
|
||||||
|
|||||||
Reference in new issue
Block a user