add jinja2 to work with templates
This commit is contained in:
parent
0b33342509
commit
9ff7c34eae
@ -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:
|
||||
|
||||
@ -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</tspan>", f">{username}</tspan>")
|
||||
svg = svg.replace(">Platz: XYZ</tspan><", f">Platz: {seat}</tspan><")
|
||||
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}")
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 59 KiB |
@ -28,11 +28,11 @@ body {
|
||||
<body>
|
||||
|
||||
<div class="page">
|
||||
{{ svg_string | safe }}
|
||||
{{ svg | safe }}
|
||||
</div>
|
||||
|
||||
<div class="page rotated">
|
||||
{{ svg_string | safe }}
|
||||
{{ svg | safe }}
|
||||
</div>
|
||||
|
||||
</body>
|
||||
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 59 KiB |
Loading…
Reference in New Issue
Block a user