From 42962d34cb83eec96ecdf99bb6a1bf34e2b594b7 Mon Sep 17 00:00:00 2001 From: Erik Thiele Date: Thu, 2 Jul 2026 15:27:52 +0200 Subject: [PATCH] Logo-Hoehe via image_dimensions() berechnen, Firmendaten korrekt darunter --- app.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 8f7be33..80c67fb 100644 --- a/app.py +++ b/app.py @@ -6,6 +6,7 @@ Bietet eine Weboberflaeche zur Eingabe von Druckparametern, Live-Berechnung der Kosten, PDF-Rechnung und TXT-Export. """ import os +import struct import yaml import io from datetime import datetime @@ -15,6 +16,34 @@ from fpdf import FPDF app = Flask(__name__) app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024 CONFIG_FILE = 'druckkalkulation_config.yaml' + + +def image_dimensions(filepath): + """Liest Pixelbreite und -hoehe aus PNG/JPEG/WebP ohne externe Libs.""" + with open(filepath, 'rb') as f: + head = f.read(64) + if head[:8] == b'\x89PNG\r\n\x1a\n': + w, h = struct.unpack('>II', head[16:24]) + return w, h + if head[:2] == b'\xff\xd8': + i = 2 + while i < len(head) - 1: + if head[i] != 0xFF: + break + marker = head[i+1] + if marker in (0xC0, 0xC1, 0xC2): + h, w = struct.unpack('>HH', head[i+5:i+9]) + return w, h + length = struct.unpack('>H', head[i+2:i+4])[0] + i += 2 + length + if head[:4] == b'RIFF' and head[8:12] == b'WEBP': + if head[12:16] == b'VP8 ': + w, h = struct.unpack('> 14) & 0x3FFF) + 1 + return None, None LOGO_FILE = 'static/logo.png' DEFAULT_VALUES = { @@ -256,7 +285,11 @@ def invoice_pdf(): logo_path = os.path.join('static', logo_name) if logo_name else '' if logo_name and os.path.exists(logo_path): pdf.image(logo_path, x=155, y=8, w=40) - logo_bottom = pdf.get_y() + img_w, img_h = image_dimensions(logo_path) + if img_w and img_h: + logo_bottom = 8 + 40 * (img_h / img_w) + else: + logo_bottom = 8 + 20 else: pdf.set_font("Arial", "B", 18) pdf.set_xy(150, 15)