admin-dashboard-backend/groupTasks/groups/shx-intern-filament-roll-label/script.py

85 lines
2.6 KiB
Python

import json
import subprocess
import sys
import os
from datetime import datetime
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')))
from libs.utils import utils
json_object = json.loads(sys.argv[1])
filament_type_id = json_object["filament_type_id"]
if filament_type_id is None:
print("Missing required parameters")
sys.exit(1)
filament_type_id = filament_type_id["value"]
def createHighDpiPng(sourceHtml, outputPng):
# Calculate scaled dimensions
scale_factor = 1
width = int(1020 * scale_factor) # Original width in pixels multiplied by the scale factor
height = int(400 * scale_factor) # Original height in pixels multiplied by the scale factor
command = [
"google-chrome-stable",
"--headless",
"--no-sandbox",
"--disable-gpu",
"--screenshot=" + outputPng,
"--window-size={},{}".format(width, height), # Set window size to scaled dimensions
"--force-device-scale-factor={}".format(scale_factor), # Set device scale factor
sourceHtml,
]
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
_, stderr = process.communicate()
if process.returncode != 0:
print("Error creating PNG")
print(stderr.decode()) # Decoding the stderr for better readability
sys.exit(1)
if __name__ == "__main__":
utils.move_files_back_from_old_files()
# replace placeholders in index.html
f_type_id = utils.extract_filament_id(filament_type_id)
data = {
"invex_id": "",
"shx_filament_id": f_type_id
}
utils.create_qrcode(json.dumps(data), "./qrcode.png", "#fff")
with open('../../groupsData/google-sheet-filaments.json', 'r', encoding='utf-8') as json_file:
data = json.load(json_file)
for filament in data["filaments"]:
if filament["id"] == f_type_id:
with open("index.html", "r") as file:
indexhtml = file.read()
indexhtml = indexhtml.replace("{{FILAMENT_ID}}", filament["id"])
indexhtml = indexhtml.replace("{{FILAMENT_NAME_COLOR}}", filament["name_color"])
indexhtml = indexhtml.replace("{{FILAMENT_MATERIAL}}", filament["material"])
indexhtml = indexhtml.replace("{{FILAMENT_MANUFACTURER}}", filament["manufacturer"])
now = datetime.now()
formatted_date = now.strftime("%d.%m.%Y")
indexhtml = indexhtml.replace("{{DATE}}", formatted_date)
with open("index.html", "w") as file:
file.write(indexhtml)
break
createHighDpiPng("index.html", "filament-roll-label.png")
utils.clear_workspace(["index.html", "qrcode.png"])