import os from reportlab.lib.pagesizes import A4 from reportlab.lib import colors from reportlab.pdfbase import pdfmetrics from reportlab.pdfbase.ttfonts import TTFont DEFAULT_MARGIN = 28 DEFAULT_HEADER_H = 86 DEFAULT_FOOTER_H = 72 def register_unicode_fonts(): if "Vera" in pdfmetrics.getRegisteredFontNames(): return fonts_dir = os.path.join(os.path.dirname(__import__("reportlab").__file__), "fonts") pdfmetrics.registerFont(TTFont("Vera", os.path.join(fonts_dir, "Vera.ttf"))) pdfmetrics.registerFont(TTFont("Vera-Bold", os.path.join(fonts_dir, "VeraBd.ttf"))) pdfmetrics.registerFont(TTFont("Vera-Italic", os.path.join(fonts_dir, "VeraIt.ttf"))) def draw_standard_header_footer( pdf, *, page_num, client_name, manufacturer, model, serial, upgrade_hours, chassis_hours, mileage, work_order_number, generated_date, report_title="Izvještaj servisera", page_size=A4, margin=DEFAULT_MARGIN, header_h=DEFAULT_HEADER_H, footer_h=DEFAULT_FOOTER_H, ): w, h = page_size pdf.setStrokeColor(colors.black) pdf.setLineWidth(0.7) pdf.rect(margin, h - header_h, w - 2 * margin, header_h - 4, stroke=1, fill=0) pdf.setFont("Vera-Bold", 11) pdf.drawString(margin + 6, h - 22, str(client_name or "-")[:44]) pdf.drawCentredString(w / 2, h - 22, report_title) pdf.setFont("Vera", 9) pdf.drawRightString(w - margin - 6, h - 22, f"Stranica {page_num}") pdf.setLineWidth(0.5) pdf.line(margin, h - 36, w - margin, h - 36) labels = [ "Model:", "Serijski broj:", "Radni sati nadogradnje:", "Radni sati podvozja:", "Km:", "Broj naloga:", ] values = [ (manufacturer, model), serial, upgrade_hours, chassis_hours, mileage, work_order_number, ] col_w = (w - 2 * margin) / len(labels) pdf.setFont("Vera", 7.2) for i, (lbl, val) in enumerate(zip(labels, values)): x = margin + i * col_w + 3 pdf.drawString(x, h - 49, lbl) value_text = str(val or "-") value_font_size = 9 if i == 0 and isinstance(val, tuple): manufacturer_text = str(val[0] or "-")[:24] model_text = str(val[1] or "-")[:24] pdf.setFont("Vera-Bold", 8.2) pdf.drawString(x, h - 59, manufacturer_text) pdf.setFont("Vera", 7.6) pdf.drawString(x, h - 68, model_text) elif i == len(labels) - 1: value_font_size = 7 max_value_width = col_w - 8 while ( pdf.stringWidth(value_text, "Vera-Bold", value_font_size) > max_value_width and value_font_size > 5.5 ): value_font_size -= 0.3 if pdf.stringWidth(value_text, "Vera-Bold", value_font_size) > max_value_width: trimmed = value_text while ( len(trimmed) > 3 and pdf.stringWidth(f"{trimmed}...", "Vera-Bold", value_font_size) > max_value_width ): trimmed = trimmed[:-1] value_text = f"{trimmed}..." else: value_text = value_text[:24] if not (i == 0 and isinstance(val, tuple)): pdf.setFont("Vera-Bold", value_font_size) pdf.drawString(x, h - 61, value_text) pdf.setFont("Vera", 7.2) if i > 0: pdf.line(margin + i * col_w, h - 36, margin + i * col_w, h - header_h + 4) footer_y = footer_h pdf.setLineWidth(0.5) pdf.rect(margin, footer_y + 42, w - 2 * margin, 18, stroke=1, fill=0) sig_labels = [f"Datum: {generated_date}", "Potpis servisera", "Pečat i potpis klijenta *1", "Pregledao"] sig_w = (w - 2 * margin) / 4 pdf.setFont("Vera", 7.2) for i, lbl in enumerate(sig_labels): x = margin + i * sig_w pdf.drawString(x + 4, footer_y + 45, lbl) if i > 0: pdf.line(x, footer_y + 42, x, footer_y + 60) pdf.setFont("Vera", 6.5) pdf.drawString(margin, footer_y + 26, "*1- potpisom klijent potvrđuje da je suglasan s podacima u radnom nalogu") pdf.drawCentredString( w / 2, footer_y + 14, "Ovlašteni servis LIEBHERR Werk-Ehingen GmbH, LIEBHERR-Werk Nenzing GmbH, Liebherr-MCCTech Rostock GmbH", )