st-ten-1/src/components/brother_label_printer.py

44 lines
1.6 KiB
Python
Raw Normal View History

2024-03-12 12:48:00 +00:00
import logging
import os
from datetime import datetime
from PIL import Image, ImageDraw, ImageFont
2024-03-14 12:15:08 +00:00
import qrcode
2024-03-12 12:48:00 +00:00
from src.components.component import Component
class BrotherLabelPrinter(Component):
def __int__(self):
self.log = logging.getLogger()
2024-03-14 12:15:08 +00:00
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)
2024-03-12 12:48:00 +00:00
try:
2024-03-14 12:15:08 +00:00
barcode1_img = qr.make_image(fill_color="black", back_color="white")
2024-03-12 12:48:00 +00:00
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)
2024-03-14 12:15:08 +00:00
font = ImageFont.truetype("config/label_templates/Fonts/FreeSansBold.ttf", 40)
font2 = ImageFont.truetype("config/label_templates/Fonts/FreeSansBold.ttf", 28)
2024-03-12 12:48:00 +00:00
draw.text((280, 50), "ETICHETTA INTEROPERAZIONALE", (0, 0, 0), font=font)
2024-03-14 12:15:08 +00:00
draw.text((280, 150), barcode, (0, 0, 0), font=font2)
label_img.save("config/label_templates/temp/tmp.png")
2024-03-12 12:48:00 +00:00
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"