Keine Zentrierung für Tätigkeiten. Doppeltes Datum anzeigen.
This commit is contained in:
1 parent
87f8150bd3
commit
8fde588b8f
3 files changed
+21
-26
No files matched your search
+21
-26
@@ -102,10 +102,10 @@ def _scan_template_fields(
|
|||||||
template_path: Path,
|
template_path: Path,
|
||||||
mapping: list[OutputMapping],
|
mapping: list[OutputMapping],
|
||||||
fontsize: float,
|
fontsize: float,
|
||||||
) -> dict[str, dict]:
|
) -> dict[str, list[dict]]:
|
||||||
"""
|
"""
|
||||||
Scannt ALLE Seiten des PDF-Templates nach _Field-Platzhaltern.
|
Scannt ALLE Seiten des PDF-Templates nach _Field-Platzhaltern.
|
||||||
Rückgabe: dict[pdf_field] → {source_field, page_num, rect, fontsize, fontname, ...}
|
Rückgabe: dict[pdf_field] → Liste von {source_field, page_num, rect, fontsize, fontname, ...}
|
||||||
Felder mit pdf_field=None werden übersprungen.
|
Felder mit pdf_field=None werden übersprungen.
|
||||||
"""
|
"""
|
||||||
doc = fitz.open(template_path)
|
doc = fitz.open(template_path)
|
||||||
@@ -116,26 +116,23 @@ def _scan_template_fields(
|
|||||||
if m.pdf_field is not None:
|
if m.pdf_field is not None:
|
||||||
pdf_to_source[m.pdf_field] = m.source_field
|
pdf_to_source[m.pdf_field] = m.source_field
|
||||||
|
|
||||||
field_info: dict[str, dict] = {}
|
field_info: dict[str, list[dict]] = {}
|
||||||
|
|
||||||
# Per search_for die Rechtecke aller PDF-Feld-Marker finden (alle Seiten)
|
# Alle Vorkommen aller PDF-Feld-Marker finden (alle Seiten)
|
||||||
for pdf_field, source_field in pdf_to_source.items():
|
for pdf_field, source_field in pdf_to_source.items():
|
||||||
found_any = False
|
occurrences: list[dict] = []
|
||||||
for page_num in range(doc.page_count):
|
for page_num in range(doc.page_count):
|
||||||
page = doc[page_num]
|
page = doc[page_num]
|
||||||
found = page.search_for(pdf_field)
|
found = page.search_for(pdf_field)
|
||||||
if found:
|
for rect in found:
|
||||||
field_info[pdf_field] = {
|
occurrences.append({
|
||||||
"source_field": source_field,
|
"source_field": source_field,
|
||||||
"page_num": page_num,
|
"page_num": page_num,
|
||||||
"rect": found[0],
|
"rect": rect,
|
||||||
"fontsize": fontsize,
|
"fontsize": fontsize,
|
||||||
"fontname": "helv",
|
"fontname": "helv",
|
||||||
}
|
})
|
||||||
found_any = True
|
field_info[pdf_field] = occurrences
|
||||||
break # erstes Vorkommen (seitenübergreifend)
|
|
||||||
if not found_any:
|
|
||||||
field_info[pdf_field] = None # nicht gefunden
|
|
||||||
|
|
||||||
doc.close()
|
doc.close()
|
||||||
return field_info
|
return field_info
|
||||||
@@ -150,7 +147,7 @@ def _fill_pdf_pages(
|
|||||||
record: EmployeeRecord,
|
record: EmployeeRecord,
|
||||||
employee_name: str,
|
employee_name: str,
|
||||||
mapping: list[OutputMapping],
|
mapping: list[OutputMapping],
|
||||||
field_info: dict[str, dict],
|
field_info: dict[str, list[dict]],
|
||||||
fontsize: float,
|
fontsize: float,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Befüllt alle Template-Seiten: redacted _Field-Platzhalter und fügt echte Daten ein."""
|
"""Befüllt alle Template-Seiten: redacted _Field-Platzhalter und fügt echte Daten ein."""
|
||||||
@@ -162,10 +159,15 @@ def _fill_pdf_pages(
|
|||||||
if m.pdf_field is None:
|
if m.pdf_field is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
info = field_info.get(m.pdf_field)
|
occurrences = field_info.get(m.pdf_field, [])
|
||||||
if info is None:
|
if not occurrences:
|
||||||
continue # Feld nicht im Template gefunden
|
continue # Feld nicht im Template gefunden
|
||||||
|
|
||||||
|
value = _get_field_value(record, employee_name, m.source_field)
|
||||||
|
formatted = _format_value(value, m.format)
|
||||||
|
|
||||||
|
# Jedes Vorkommen des Feldes im PDF befüllen
|
||||||
|
for info in occurrences:
|
||||||
page_num = info["page_num"]
|
page_num = info["page_num"]
|
||||||
if page_num >= len(pages):
|
if page_num >= len(pages):
|
||||||
continue
|
continue
|
||||||
@@ -173,10 +175,7 @@ def _fill_pdf_pages(
|
|||||||
page = pages[page_num]
|
page = pages[page_num]
|
||||||
rect = info["rect"]
|
rect = info["rect"]
|
||||||
|
|
||||||
value = _get_field_value(record, employee_name, m.source_field)
|
# Platzhalter redacten
|
||||||
formatted = _format_value(value, m.format)
|
|
||||||
|
|
||||||
# Platzhalter immer redacten, auch wenn kein Wert zum Einsetzen da ist
|
|
||||||
page.add_redact_annot(rect)
|
page.add_redact_annot(rect)
|
||||||
page.apply_redactions()
|
page.apply_redactions()
|
||||||
|
|
||||||
@@ -185,18 +184,16 @@ def _fill_pdf_pages(
|
|||||||
|
|
||||||
# Mehrzeilige Felder: Zeilenweise per insert_text schreiben
|
# Mehrzeilige Felder: Zeilenweise per insert_text schreiben
|
||||||
if m.pdf_field in MULTILINE_FIELDS:
|
if m.pdf_field in MULTILINE_FIELDS:
|
||||||
# Doppelte Leerzeilen entfernen
|
# ... (unverändert)
|
||||||
clean_text = "\n".join(
|
clean_text = "\n".join(
|
||||||
line for line in formatted.split("\n") if line.strip()
|
line for line in formatted.split("\n") if line.strip()
|
||||||
)
|
)
|
||||||
if not clean_text:
|
if not clean_text:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# Zeilenweise mit automatischem Umbruch schreiben
|
|
||||||
lines = clean_text.split("\n")
|
lines = clean_text.split("\n")
|
||||||
line_spacing = fontsize * 1.2 # Zeilenabstand
|
line_spacing = fontsize * 1.2
|
||||||
|
|
||||||
# Zuerst alle Ausgabezeilen sammeln, um Gesamthöhe zu berechnen
|
|
||||||
output_lines: list[str] = []
|
output_lines: list[str] = []
|
||||||
for line in lines:
|
for line in lines:
|
||||||
max_width = TEXTBOX_WIDTH_PT
|
max_width = TEXTBOX_WIDTH_PT
|
||||||
@@ -213,9 +210,7 @@ def _fill_pdf_pages(
|
|||||||
if current_line:
|
if current_line:
|
||||||
output_lines.append(current_line)
|
output_lines.append(current_line)
|
||||||
|
|
||||||
# Textblock oben beginnend schreiben
|
|
||||||
y = rect.y0 + fontsize
|
y = rect.y0 + fontsize
|
||||||
|
|
||||||
for current_line in output_lines:
|
for current_line in output_lines:
|
||||||
page.insert_text(
|
page.insert_text(
|
||||||
(rect.x0, y), current_line,
|
(rect.x0, y), current_line,
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user