commit d11d0b1b38a102e6358ce336ae7bf26bab7da753 Author: tcprod Date: Fri Mar 6 01:29:54 2026 +0100 add functions to create a printable user badge diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..40cb5fc --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.venv +test.pdf +.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/template/Vorlage_LAN_Ausweis.svg b/template/Vorlage_LAN_Ausweis.svg new file mode 100644 index 0000000..157f600 --- /dev/null +++ b/template/Vorlage_LAN_Ausweis.svg @@ -0,0 +1,200 @@ + + + + + + + + + + + + + + + + + +LANtest_usernamePlatz: XYZich bin dein bier-geld diff --git a/template/Vorlage_LAN_Ausweis_mit_Daten.svg b/template/Vorlage_LAN_Ausweis_mit_Daten.svg new file mode 100644 index 0000000..3a6466c --- /dev/null +++ b/template/Vorlage_LAN_Ausweis_mit_Daten.svg @@ -0,0 +1,200 @@ + + + + + + + + + + + + + + + + + +LANtest_namePlatz: c05ich bin dein bier-geld diff --git a/template/test_profile_picture.png b/template/test_profile_picture.png new file mode 100644 index 0000000..3ac3657 Binary files /dev/null and b/template/test_profile_picture.png differ diff --git a/user_badge_creator.py b/user_badge_creator.py new file mode 100644 index 0000000..21af5f0 --- /dev/null +++ b/user_badge_creator.py @@ -0,0 +1,104 @@ +import os +import re +import base64 +import subprocess +import tempfile +from pathlib import Path + +SVG_TEMPLATE = Path("template/Vorlage_LAN_Ausweis.svg") +CHROME_PATH = Path(r"C:\Program Files\Google\Chrome\Application\chrome.exe") + + +def get_image_as_base64(image_path: str) -> str: + with open(image_path, "rb") as image_file: + encoded_bytes = base64.b64encode(image_file.read()) + encoded_string = encoded_bytes.decode("utf-8") + return f'xlink:href="data:image/jpeg;base64,{encoded_string}"' + + +def get_user_svg_string(username: str, seat: str, user_image: str) -> str: + with open(SVG_TEMPLATE, "r", encoding="utf-8") as svg_file: + svg_string = svg_file.read() + + svg_string = svg_string.replace(">test_username", f">{username}") + svg_string = svg_string.replace(">Platz: XYZ<", f">Platz: {seat}<") + svg_string = svg_string.replace('xlink:href="data:image/jpeg;base64,"', user_image) + + """ + with open("test.svg", "w", encoding="utf-8") as f: + f.write(svg_content) + """ + + return svg_string + + +def create_pdf_from_svg(svg_string: str, pdf_path: str): + width = re.search(r'width="([^"]+)"', svg_string) + height = re.search(r'height="([^"]+)"', svg_string) + + width = width.group(1) + height = height.group(1) + + html = f""" + + + + + + +
+{svg_string} +
+ +
+{svg_string} +
+ + + +""" + + with tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode="w", encoding="utf-8") as f: + f.write(html) + html_path = f.name + + try: + subprocess.run([ + CHROME_PATH, + "--headless", + "--disable-gpu", + f"--print-to-pdf={os.path.abspath(pdf_path)}", + html_path + ], check=True) + finally: + os.remove(html_path) + + +if __name__ == "__main__": + img_base64 = get_image_as_base64("template/test_profile_picture.png") + user_svg_string = get_user_svg_string("Tcprod.", "C01", img_base64) + create_pdf_from_svg(user_svg_string, "test.pdf")