diff --git a/app.py b/app.py index 78a749a..6d45723 100644 --- a/app.py +++ b/app.py @@ -12,6 +12,41 @@ from datetime import datetime from flask import Flask, render_template, request, jsonify, redirect, url_for, send_file from fpdf import FPDF +def image_dimensions(filepath): + """Liest Pixelmasse aus PNG/JPEG/WebP ohne externe Libs.""" # noqa: E501 + with open(filepath, 'rb') as f: + head = f.read(24) + if head[:8] == b'\x89PNG\r\n\x1a\n': + import struct + return struct.unpack('>II', head[16:24]) + if head[:2] == b'\xff\xd8': + import struct + with open(filepath, 'rb') as f: + data = f.read(8192) + i = 2 + while i < len(data) - 1: + if data[i] != 0xFF: + i += 1 + continue + marker = data[i+1] + if marker in (0xC0, 0xC1, 0xC2): + h, w = struct.unpack('>HH', data[i+5:i+9]) + return w, h + length = struct.unpack('>H', data[i+2:i+4])[0] + i += 2 + length + if head[:4] == b'RIFF' and head[8:12] == b'WEBP': + import struct + with open(filepath, 'rb') as f: + data = f.read(64) + if data[12:16] == b'VP8 ' and len(data) >= 30: + w, h = struct.unpack('= 25: + bits = struct.unpack('> 14) & 0x3FFF) + 1 + return None, None + + app = Flask(__name__) app.config['MAX_CONTENT_LENGTH'] = 5 * 1024 * 1024 CONFIG_FILE = 'druckkalkulation_config.yaml' @@ -256,8 +291,12 @@ def invoice_pdf(): logo_name = config.get('logo', '') 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 = 50 + pdf.image(logo_path, x=155, y=5, w=40) + img_w, img_h = image_dimensions(logo_path) + if img_w and img_h: + logo_bottom = 5 + 40 * (img_h / img_w) + 3 + else: + logo_bottom = 5 + 25 + 3 else: pdf.set_font("Arial", "B", 18) pdf.set_xy(150, 15)