import logging import os from datetime import datetime from PIL import Image, ImageDraw, ImageFont import qrcode from src.components.component import Component class BrotherLabelPrinter(Component): def __int__(self): self.log = logging.getLogger() def print_label(self, barcode): qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, ) qr.add_data(barcode) qr.make(fit=True) try: barcode1_img = qr.make_image(fill_color="black", back_color="white") 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("config/label_templates/Fonts/FreeSansBold.ttf", 40) font2 = ImageFont.truetype("config/label_templates/Fonts/FreeSansBold.ttf", 28) draw.text((280, 50), "ETICHETTA INTEROPERAZIONALE", (0, 0, 0), font=font) draw.text((280, 150), barcode, (0, 0, 0), font=font2) label_img.save("config/label_templates/temp/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"