add functions to create a printable user badge
This commit is contained in:
commit
d11d0b1b38
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
.venv
|
||||||
|
test.pdf
|
||||||
|
.idea
|
||||||
200
template/Vorlage_LAN_Ausweis.svg
Normal file
200
template/Vorlage_LAN_Ausweis.svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 59 KiB |
200
template/Vorlage_LAN_Ausweis_mit_Daten.svg
Normal file
200
template/Vorlage_LAN_Ausweis_mit_Daten.svg
Normal file
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 191 KiB |
BIN
template/test_profile_picture.png
Normal file
BIN
template/test_profile_picture.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 99 KiB |
104
user_badge_creator.py
Normal file
104
user_badge_creator.py
Normal file
@ -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</tspan>", f">{username}</tspan>")
|
||||||
|
svg_string = svg_string.replace(">Platz: XYZ</tspan><", f">Platz: {seat}</tspan><")
|
||||||
|
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"""
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<style>
|
||||||
|
@page {{
|
||||||
|
size: {width} {height};
|
||||||
|
margin: 0;
|
||||||
|
}}
|
||||||
|
|
||||||
|
body {{
|
||||||
|
margin: 0;
|
||||||
|
}}
|
||||||
|
|
||||||
|
.page {{
|
||||||
|
width: {width};
|
||||||
|
height: {height};
|
||||||
|
page-break-after: always;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}}
|
||||||
|
|
||||||
|
.rotated {{
|
||||||
|
transform: rotate(180deg) scaleX(-1);
|
||||||
|
transform-origin: center;
|
||||||
|
}}
|
||||||
|
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<div class="page">
|
||||||
|
{svg_string}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="page rotated">
|
||||||
|
{svg_string}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
"""
|
||||||
|
|
||||||
|
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")
|
||||||
Loading…
Reference in New Issue
Block a user