65 lines
3.1 KiB
Python
65 lines
3.1 KiB
Python
|
|
import logging
|
||
|
|
import os
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
from PIL import Image, ImageDraw, ImageFont
|
||
|
|
from pylibdmtx.pylibdmtx import encode
|
||
|
|
|
||
|
|
from src.components.component import Component
|
||
|
|
|
||
|
|
|
||
|
|
class BrotherLabelPrinter(Component):
|
||
|
|
def __int__(self):
|
||
|
|
self.log = logging.getLogger()
|
||
|
|
|
||
|
|
def print_label(self, operator, lvl_barcode, qlt_barcode):
|
||
|
|
barcode = "ST-TEN-10_" + lvl_barcode + "_" + qlt_barcode
|
||
|
|
encoded = encode(barcode.encode("utf-8"))
|
||
|
|
|
||
|
|
try:
|
||
|
|
barcode1_img = Image.frombytes('RGB', (encoded.width, encoded.height), encoded.pixels)
|
||
|
|
barcode1_img = barcode1_img.resize((250, 250))
|
||
|
|
print_w = 720
|
||
|
|
print_h = 330
|
||
|
|
label_img = Image.new("RGB", (print_w, print_h), color=(255, 255, 255))
|
||
|
|
label_img.paste(barcode1_img, box=(20, 20))
|
||
|
|
draw = ImageDraw.Draw(label_img)
|
||
|
|
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 40)
|
||
|
|
font2 = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 28)
|
||
|
|
draw.text((280, 50), "ETICHETTA INTEROPERAZIONALE", (0, 0, 0), font=font)
|
||
|
|
draw.text((280, 150), operator, (0, 0, 0), font=font2)
|
||
|
|
draw.text((280, 200), qlt_barcode, (0, 0, 0), font=font2)
|
||
|
|
draw.text((280, 250), lvl_barcode, (0, 0, 0), font=font2)
|
||
|
|
label_img.save("/tmp/tmp.png")
|
||
|
|
os.system(f"lp -o page-bottom=0 -o page-left=0 -o page-right=0 -o page-top=0 /tmp/tmp.png")
|
||
|
|
return barcode
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
self.log.error("Error printing product label\n\nError:\n" + str(e))
|
||
|
|
return "PRINT ERROR"
|
||
|
|
|
||
|
|
def print_fail(self, operator, lvl_barcode, qlt_barcode):
|
||
|
|
fail_barcode = "KO60_" + lvl_barcode + "_" + qlt_barcode + "_KO60"
|
||
|
|
try:
|
||
|
|
print_w = 720
|
||
|
|
print_h = 330
|
||
|
|
label_img = Image.new("RGB", (print_w, print_h), color=(255, 255, 255))
|
||
|
|
draw = ImageDraw.Draw(label_img)
|
||
|
|
font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 40)
|
||
|
|
font2 = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", 28)
|
||
|
|
date_time = datetime.now().strftime("%d/%m/%Y %H:%M:%S")
|
||
|
|
machine_name = self.machine_config["machine"]["name"].upper()
|
||
|
|
draw.text((30, 30), f"XXXXXX SCARTO {machine_name} XXXXXX", (0, 0, 0), font=font)
|
||
|
|
draw.text((30, 80), qlt_barcode, (0, 0, 0), font=font2)
|
||
|
|
draw.text((30, 130), lvl_barcode, (0, 0, 0), font=font2)
|
||
|
|
draw.text((30, 180), operator, (0, 0, 0), font=font2)
|
||
|
|
draw.text((30, 230), date_time, (0, 0, 0), font=font2)
|
||
|
|
draw.text((30, 270), f"XXXXXX SCARTO {machine_name} XXXXXX", (0, 0, 0), font=font)
|
||
|
|
label_img.save("/tmp/tmp.png")
|
||
|
|
os.system(f"lp -o page-bottom=0 -o page-left=0 -o page-right=0 -o page-top=0 /tmp/tmp.png")
|
||
|
|
return fail_barcode
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
self.log.error("Error printing product label\n\nError:\n" + str(e))
|
||
|
|
return "PRINT ERROR"
|