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(
|
badge_generator = BadgeGeneratorService(
|
||||||
svg_template=Path("template/template_dynamic_name_size.svg"),
|
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")
|
chrome_path=Path(r"C:\Program Files\Google\Chrome\Application\chrome.exe")
|
||||||
)
|
)
|
||||||
for user_badge in badges:
|
for user_badge in badges:
|
||||||
|
|||||||
@ -16,52 +16,59 @@ logger = logging.getLogger(__name__.split(".")[-1])
|
|||||||
|
|
||||||
|
|
||||||
class BadgeGeneratorService:
|
class BadgeGeneratorService:
|
||||||
|
def __init__(self, svg_template: Path, html_template: Path, chrome_path: Path):
|
||||||
def __init__(self, svg_template: Path, chrome_path: Path):
|
|
||||||
self.svg_template = svg_template
|
self.svg_template = svg_template
|
||||||
|
self.html_template = html_template
|
||||||
self.chrome_path = chrome_path
|
self.chrome_path = chrome_path
|
||||||
|
|
||||||
def generate_badge(self, name, seat_id, picture) -> None:
|
def generate_badge(self, name, seat_id, picture) -> None:
|
||||||
svg = self.generate_svg(name, seat_id, picture)
|
svg = self._get_svg(name, seat_id, picture)
|
||||||
self.svg_to_pdf(svg, f"files/{name}.pdf")
|
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:
|
def _get_svg(self, username: str, seat: str, image_base64: bytes) -> str:
|
||||||
with open(self.svg_template, "r", encoding="utf-8") as f:
|
env = Environment(loader=FileSystemLoader("template"))
|
||||||
svg = f.read()
|
template = env.get_template(self.svg_template.name)
|
||||||
svg = svg.replace(">test_username</tspan>", f">{username}</tspan>")
|
|
||||||
svg = svg.replace(">Platz: XYZ</tspan><", f">Platz: {seat}</tspan><")
|
|
||||||
if image_base64:
|
if image_base64:
|
||||||
encoded = base64.b64encode(image_base64).decode("utf-8")
|
image_base64 = base64.b64encode(image_base64).decode("utf-8")
|
||||||
svg = svg.replace('xlink:href="data:image/jpeg;base64,"', f'xlink:href="data:image/jpeg;base64,{encoded}"')
|
else:
|
||||||
logger.info(f"Generate svg string: Username: {username}, Seat: {seat}, Picture: {str(image_base64)[:15]}")
|
image_base64 = "Standard picture"
|
||||||
|
|
||||||
|
svg = template.render(
|
||||||
|
picture=image_base64,
|
||||||
|
username=username,
|
||||||
|
seat_id=seat
|
||||||
|
)
|
||||||
return svg
|
return svg
|
||||||
|
|
||||||
def svg_to_pdf(self, svg_string: str, pdf_path: str):
|
def _svg_to_html(self, svg: str) -> str:
|
||||||
|
width = re.search(r'width="([^"]+)"', svg).group(1)
|
||||||
width = re.search(r'width="([^"]+)"', svg_string).group(1)
|
height = re.search(r'height="([^"]+)"', svg).group(1)
|
||||||
height = re.search(r'height="([^"]+)"', svg_string).group(1)
|
|
||||||
|
|
||||||
env = Environment(loader=FileSystemLoader("template"))
|
env = Environment(loader=FileSystemLoader("template"))
|
||||||
template = env.get_template("badge_pdf.html")
|
template = env.get_template(self.html_template.name)
|
||||||
|
|
||||||
html = template.render(
|
html = template.render(
|
||||||
width=width,
|
width=width,
|
||||||
height=height,
|
height=height,
|
||||||
svg_string=svg_string
|
svg=svg
|
||||||
)
|
)
|
||||||
|
return html
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode="w", encoding="utf-8") as f:
|
def _html_to_pdf(self, html: str, pdf: str):
|
||||||
f.write(html)
|
with tempfile.NamedTemporaryFile(delete=False, suffix=".html", mode="w", encoding="utf-8") as file:
|
||||||
html_path = f.name
|
file.write(html)
|
||||||
|
html_path = file.name
|
||||||
|
|
||||||
try:
|
try:
|
||||||
subprocess.run([
|
subprocess.run([
|
||||||
self.chrome_path,
|
self.chrome_path,
|
||||||
"--headless",
|
"--headless",
|
||||||
"--disable-gpu",
|
"--disable-gpu",
|
||||||
f"--print-to-pdf={os.path.abspath(pdf_path)}",
|
f"--print-to-pdf={os.path.abspath(pdf)}",
|
||||||
html_path
|
html_path
|
||||||
], check=True)
|
], check=True)
|
||||||
finally:
|
finally:
|
||||||
os.remove(html_path)
|
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>
|
<body>
|
||||||
|
|
||||||
<div class="page">
|
<div class="page">
|
||||||
{{ svg_string | safe }}
|
{{ svg | safe }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="page rotated">
|
<div class="page rotated">
|
||||||
{{ svg_string | safe }}
|
{{ svg | safe }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</body>
|
</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