Untitled
unknown
plain_text
a year ago
1.4 kB
1
Indexable
Never
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) page_size_pixels = tuple(int(windll.gdi32.GetDeviceCaps(printer_dc.GetSafeHdc(), i) * page_size_mm[i] / 1000) for i in (0, 1)) # Specify the full path to the barcode image barcode_image_path = os.path.abspath(barcode_image) image = Image.open(barcode_image_path) image_width, image_height = image.size # Calculate the scaling factor to fit the image on the page scaling_factor = min(int(page_size_pixels[0] / image_width), int(page_size_pixels[1] / image_height)) # Calculate the position to center the image on the page x_pos = int((page_size_pixels[0] - scaling_factor * image_width) / 2) y_pos = int((page_size_pixels[1] - scaling_factor * image_height) / 2) # Start the print job h_printer = win32print.OpenPrinter(printer) h_job = win32print.StartDocPrinter(h_printer, 1, ('Barcode', None, 'RAW')) win32print.StartPagePrinter(h_printer) # Print the image dib = ImageWin.Dib(image) dib.draw(printer_dc.GetSafeHdc(), (x_pos, y_pos, scaling_factor * image_width, scaling_factor * image_height)) # End the print job win32print.EndPagePrinter(h_printer) win32print.EndDocPrinter(h_printer) win32print.ClosePrinter(h_printer)