Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
4.2 kB
2
Indexable
Never
import io
import code128
from PIL import Image, ImageDraw, ImageFont, ImageWin
import os
import win32print
import win32ui
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)

def get_printers():
    printers = [printer[2] for printer in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL)]
    return printers

def user_select_printer(printers):
    if not printers:
        print("No printers found.")
        return None

    print("List of printers:")
    for i, printer in enumerate(printers, start=1):
        print(f"{i}. {printer}")

    selected_index = int(input("Enter the number of the printer you'd like to use: "))
    return printers[selected_index - 1]

def get_product_code():
    product_code = input("Enter the product code: ")
    return product_code.upper()

def generate_custom_code128(product_code, dpi=203, paper_size=(1, 2.125)):
    basethickness = 8
    baseheight = 0.6
    barcode = code128.image(product_code, thickness=basethickness, height=int(baseheight * dpi))
    paper_width = int(paper_size[0] * dpi)
    paper_height = int(paper_size[1] * dpi)
    image = Image.new('1', (paper_width, paper_height), 1)
    barcode = barcode.rotate(90, expand=True)
    barcode_width, barcode_height = barcode.size

    while (barcode_height > paper_height):
        basethickness = basethickness - 1
        barcode = code128.image(product_code, thickness=basethickness, height=int(baseheight * dpi))
        barcode = barcode.rotate(90, expand=True)
        barcode_width, barcode_height = barcode.size

    print("paper size: "+str(paper_width)+', '+str(paper_height))
    print("barcode size: "+str(barcode_width)+', '+str(barcode_height))
    x_pos = int(((paper_width - barcode_width) / 2) - 35)
    y_pos = int((paper_height - barcode_height) / 2)
    image.paste(barcode, (x_pos, y_pos - 35))
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype("arial.ttf", 50)
    text_width, text_height = draw.textsize(product_code, font=font)
    print("text size: "+str(text_width)+', '+str(text_height))
    text_x = int(((paper_height - text_width) / 2) - 50)
    text_y = y_pos - text_height - 2
    rotated_text = Image.new('1', (text_width, text_height), 1)
    text_draw = ImageDraw.Draw(rotated_text)
    text_draw.text((0, 0), product_code, font=font, fill=0)
    rotated_text = rotated_text.rotate(90, expand=True)
    image.paste(rotated_text, (220, text_x))
    print("PASTING TEXT AT 220, "+str(text_x))
    output = io.BytesIO()
    image.save(output, format="PNG")
    output.seek(0)
    return output



def generate_barcode(product_code):
    output_filename = 'temp_barcode'
    barcode_image = generate_custom_code128(product_code)
    with open(output_filename + '.png', 'wb') as f:
        f.write(barcode_image.getvalue())
    return output_filename

def print_barcode(printer, barcode_image):
    # Get the printer DC
    printer_dc = win32ui.CreateDC()
    printer_dc.CreatePrinterDC(printer)
    # Get the page size and image size in pixels
    page_size_mm = (25.4, 54)
    dpi = 203  # Set the DPI value according to your printer's specification
    page_size_pixels = tuple(int(dpi * page_size_mm[i] / 25.4) for i in (0, 1))
    image = Image.open(barcode_image+".png")
    image_width, image_height = image.size
    # Calculate the position to center the image on the page
    x_pos = int((page_size_pixels[0] - image_width) / 2)
    y_pos = int((page_size_pixels[1] - image_height) / 2)
    # Start the print job
    printer_dc.StartDoc("barcode.png")
    printer_dc.StartPage()
    # Print the image
    dib = ImageWin.Dib(image)
    dib.draw(printer_dc.GetSafeHdc(), (x_pos, y_pos, image_width, image_height))
    # End the print job
    printer_dc.EndPage()
    printer_dc.EndDoc()
    printer_dc.DeleteDC()


def main():
    printers = get_printers()
    selected_printer = user_select_printer(printers)

    if selected_printer is None:
        return

    while True:
        product_code = get_product_code()
        barcode_image = generate_barcode(product_code)
        print_barcode(selected_printer, barcode_image)

if __name__ == "__main__":
    main()