From 89fae6cfa11b1371947d39450ac7195f04b365ae Mon Sep 17 00:00:00 2001 From: Edo Date: Mon, 18 Mar 2024 11:02:01 +0100 Subject: [PATCH] st ten 10 printing ok --- src/components/brother_label_printer.py | 52 ++++++++++++++++++------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/src/components/brother_label_printer.py b/src/components/brother_label_printer.py index 2829cca..49db337 100644 --- a/src/components/brother_label_printer.py +++ b/src/components/brother_label_printer.py @@ -1,8 +1,8 @@ import logging import os -from datetime import datetime - -from PIL import Image, ImageDraw, ImageFont +import win32print +import win32ui +from PIL import Image, ImageDraw, ImageFont, ImageOps, ImageWin import qrcode from src.components.component import Component @@ -12,6 +12,9 @@ class BrotherLabelPrinter(Component): self.log = logging.getLogger() def print_label(self, barcode): + printer_name = win32print.GetDefaultPrinter() + file_name = "./tmp/tmp.png" + qr = qrcode.QRCode( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, @@ -22,22 +25,41 @@ class BrotherLabelPrinter(Component): 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)) + barcode_img = qr.make_image(fill_color="black", back_color="white") + barcode_img = barcode_img.resize((250, 380)) + print_width = 720 + print_height = 380 + label_img = Image.new("RGB", (print_width, print_height), color=(255, 255, 255)) + label_img.paste(barcode_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, 50), "ETICHETTA \nINTEROPERAZIONALE", (0, 0, 0), font=font) draw.text((280, 150), 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") + + # Rotazione dell'immagine di 90 gradi in senso antiorario + rotated_label_img = label_img.rotate(90, expand=True) + + # Salvataggio dell'immagine + if not os.path.exists("./tmp/"): + os.makedirs("./tmp/") + rotated_label_img.save(file_name) + + # Stampa dell'immagine + hdc = win32ui.CreateDC() + hdc.CreatePrinterDC(printer_name) + + hdc.StartDoc(file_name) + hdc.StartPage() + + dib = ImageWin.Dib(rotated_label_img) + dib.draw(hdc.GetHandleOutput(), (0, 0, print_height, print_width)) # Inverto larghezza e altezza + + hdc.EndPage() + hdc.EndDoc() + return barcode except Exception as e: - self.log.error("Error printing product label\n\nError:\n" + str(e)) - return "PRINT ERROR" - + self.log.error("Errore durante la stampa dell'etichetta\n\nErrore:\n" + str(e)) + return "ERRORE STAMPA"