admin-dashboard-backend/groupTasks/groups/shx-intern-product-bag-label/script.py

84 lines
2.7 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])
product_type_id = json_object["product_type_id"]
if product_type_id is None:
print("Missing required parameters")
sys.exit(1)
product_type_id = product_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(1020 * 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
p_type_id = utils.extract_product_id(product_type_id)
data = {
"shx_id": p_type_id
}
utils.create_qrcode(json.dumps(data), "./qrcode.png", "#fff")
with open('../../groupsData/google-sheet-products.json', 'r', encoding='utf-8') as json_file:
data = json.load(json_file)
for product in data["products"]:
if product["id"] == p_type_id:
with open("index.html", "r") as file:
indexhtml = file.read()
indexhtml = indexhtml.replace("{{PRODUCT_ID}}", "#" + product["id"])
indexhtml = indexhtml.replace("{{PRODUCT_NAME}}", product["name"].replace("_", " "))
indexhtml = indexhtml.replace("{{PRODUCT_VARIANT}}", product["product_variant"])
indexhtml = indexhtml.replace("{{PRODUCT_CHARACTERISTIC_LINE_1}}", product["product_characteristic_line_1"])
indexhtml = indexhtml.replace("{{PRODUCT_CHARACTERISTIC_LINE_2}}", product["product_characteristic_line_2"])
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", "product-bag-label.png")
utils.clear_workspace(["index.html", "qrcode.png"])