Logo-Hoehe via image_dimensions() berechnen, Firmendaten korrekt darunter

This commit is contained in:
Erik Thiele
2026-07-02 15:27:52 +02:00
parent 3b6d2a96ba
commit 42962d34cb

35
app.py
View File

@@ -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('<HH', head[26:30])
return (w & 0x3FFF) * 2, (h & 0x3FFF) * 2
if head[12:16] == b'VP8L':
bits = struct.unpack('<I', head[21:25])[0]
return (bits & 0x3FFF) + 1, ((bits >> 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)