diff --git a/ezgg_badge_generator.py b/ezgg_badge_generator.py index ef70003..3047694 100644 --- a/ezgg_badge_generator.py +++ b/ezgg_badge_generator.py @@ -13,6 +13,7 @@ async def main(): badge_generator = BadgeGeneratorService( svg_template=Path("template/template_dynamic_name_size.svg"), + html_template=Path("template/template_badge_180_rotated.html"), chrome_path=Path(r"C:\Program Files\Google\Chrome\Application\chrome.exe") ) for user_badge in badges: diff --git a/services/BadgeGeneratorService.py b/services/BadgeGeneratorService.py index 9a34270..eee460f 100644 --- a/services/BadgeGeneratorService.py +++ b/services/BadgeGeneratorService.py @@ -16,52 +16,59 @@ logger = logging.getLogger(__name__.split(".")[-1]) class BadgeGeneratorService: - - def __init__(self, svg_template: Path, chrome_path: Path): + def __init__(self, svg_template: Path, html_template: Path, chrome_path: Path): self.svg_template = svg_template + self.html_template = html_template self.chrome_path = chrome_path def generate_badge(self, name, seat_id, picture) -> None: - svg = self.generate_svg(name, seat_id, picture) - self.svg_to_pdf(svg, f"files/{name}.pdf") + svg = self._get_svg(name, seat_id, picture) + html = self._svg_to_html(svg) + self._html_to_pdf(html, f"output_badges/{name}.pdf") + logger.info(f"Created: {name}, {seat_id}, {str(picture)[:10]}") - def generate_svg(self, username: str, seat: str, image_base64: bytes) -> str: - with open(self.svg_template, "r", encoding="utf-8") as f: - svg = f.read() - svg = svg.replace(">test_username", f">{username}") - svg = svg.replace(">Platz: XYZ<", f">Platz: {seat}<") + def _get_svg(self, username: str, seat: str, image_base64: bytes) -> str: + env = Environment(loader=FileSystemLoader("template")) + template = env.get_template(self.svg_template.name) if image_base64: - encoded = base64.b64encode(image_base64).decode("utf-8") - svg = svg.replace('xlink:href="data:image/jpeg;base64,"', f'xlink:href="data:image/jpeg;base64,{encoded}"') - logger.info(f"Generate svg string: Username: {username}, Seat: {seat}, Picture: {str(image_base64)[:15]}") + image_base64 = base64.b64encode(image_base64).decode("utf-8") + else: + image_base64 = "Standard picture" + + svg = template.render( + picture=image_base64, + username=username, + seat_id=seat + ) return svg - def svg_to_pdf(self, svg_string: str, pdf_path: str): - - width = re.search(r'width="([^"]+)"', svg_string).group(1) - height = re.search(r'height="([^"]+)"', svg_string).group(1) + def _svg_to_html(self, svg: str) -> str: + width = re.search(r'width="([^"]+)"', svg).group(1) + height = re.search(r'height="([^"]+)"', svg).group(1) env = Environment(loader=FileSystemLoader("template")) - template = env.get_template("badge_pdf.html") + template = env.get_template(self.html_template.name) html = template.render( width=width, height=height, - svg_string=svg_string + svg=svg ) + return html - with tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode="w", encoding="utf-8") as f: - f.write(html) - html_path = f.name + def _html_to_pdf(self, html: str, pdf: str): + with tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode="w", encoding="utf-8") as file: + file.write(html) + html_path = file.name try: subprocess.run([ self.chrome_path, "--headless", "--disable-gpu", - f"--print-to-pdf={os.path.abspath(pdf_path)}", + f"--print-to-pdf={os.path.abspath(pdf)}", html_path ], check=True) finally: os.remove(html_path) - logger.info(f"Created: {pdf_path}") + diff --git a/template/Vorlage_LAN_Ausweis.svg b/template/Vorlage_LAN_Ausweis.svg deleted file mode 100644 index 157f600..0000000 --- a/template/Vorlage_LAN_Ausweis.svg +++ /dev/null @@ -1,200 +0,0 @@ - - - - - - - - - - - - - - - - - -LANtest_usernamePlatz: XYZich bin dein bier-geld diff --git a/template/badge_pdf.html b/template/template_badge_180_rotated.html similarity index 90% rename from template/badge_pdf.html rename to template/template_badge_180_rotated.html index 58093f0..cdc69a1 100644 --- a/template/badge_pdf.html +++ b/template/template_badge_180_rotated.html @@ -28,11 +28,11 @@ body {
-{{ svg_string | safe }} +{{ svg | safe }}
-{{ svg_string | safe }} +{{ svg | safe }}
diff --git a/template/template_dynamic_name_size.svg b/template/template_dynamic_name_size.svg deleted file mode 100644 index 7f6560a..0000000 --- a/template/template_dynamic_name_size.svg +++ /dev/null @@ -1,203 +0,0 @@ - - - - - - - - - - - - - - - - - -LANtest_usernamePlatz: XYZich bin dein bier-geld