Skip to content

Instantly share code, notes, and snippets.

@buptxge
Created April 7, 2016 08:38
Show Gist options
  • Select an option

  • Save buptxge/2fc61a3f914645cf8ae2c9a258ca06c9 to your computer and use it in GitHub Desktop.

Select an option

Save buptxge/2fc61a3f914645cf8ae2c9a258ca06c9 to your computer and use it in GitHub Desktop.
Print image using win32print and PIL
import win32print
import win32ui
from PIL import Image, ImageWin
PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111
printer_name = win32print.GetDefaultPrinter ()
file_name = "new.jpg"
hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC (printer_name)
printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT)
bmp = Image.open (file_name)
if bmp.size[0] < bmp.size[1]:
bmp = bmp.rotate (90)
hDC.StartDoc (file_name)
hDC.StartPage ()
dib = ImageWin.Dib (bmp)
dib.draw (hDC.GetHandleOutput (), (0,0,printer_size[0],printer_size[1]))
hDC.EndPage ()
hDC.EndDoc ()
hDC.DeleteDC ()
@MARIAM-O-IDEGWU
Copy link
Copy Markdown

Hello please help out. I am developing an app using tkinter python. Users are to fill the registration form and add their passport.
I want users to be able to print out a hard copy of their registration form with all their registration details including the passport when done with a button click.
Please Urgent.
My Email mariamidegwu@ymail.com
Thanks

@Jonak-Adipta-Kalita
Copy link
Copy Markdown

Thanks for this!!

@rohanlolm
Copy link
Copy Markdown

You legend. Thankyou !

@zgy520
Copy link
Copy Markdown

zgy520 commented Mar 19, 2022

hi, why the printed file is black background? how to solve it

@bsljvela
Copy link
Copy Markdown

Thank you!!

@Recusants
Copy link
Copy Markdown

Please i need your help: i have this code, it's printing fine but it's not cutting the paper:

try:
    printer_name = "EPSON T20III"
    raw_data = bytes(recipt.text, "utf-8")

    drivers = win32print.EnumPrinterDrivers(None, None, 2)
    hPrinter = win32print.OpenPrinter(printer_name)
    printer_info = win32print.GetPrinter(hPrinter, 2)
    for driver in drivers:
        if driver["Name"] == printer_info["pDriverName"]:
            printer_driver = driver
   
    raw_type = "XPS_PASS" if printer_driver["Version"] == 4 else "RAW"
    try:
        hJob = win32print.StartDocPrinter(hPrinter, 1, ("test of raw data", None, raw_type))
        try:
            win32print.StartPagePrinter(hPrinter)
            win32print.WritePrinter(hPrinter, raw_data)
            win32print.EndPagePrinter(hPrinter)
    
        except Exception as e:
            message.warning(request, f"Error! {e}")
        finally:
            win32print.EndDocPrinter(hPrinter)  
    except Exception as e:
        message.warning(request, f'Error! {e}')
    finally:
        win32print.ClosePrinter(hPrinter)
except Exception as e:
    message.warning(request, f"Error printing! {e}")

here is the problem in full detail:

@Edwar9218
Copy link
Copy Markdown

Por favor, necesito su ayuda: tengo este código, se está imprimiendo bien pero no está cortando el papel:

try:
    printer_name = "EPSON T20III"
    raw_data = bytes(recipt.text, "utf-8")

    drivers = win32print.EnumPrinterDrivers(None, None, 2)
    hPrinter = win32print.OpenPrinter(printer_name)
    printer_info = win32print.GetPrinter(hPrinter, 2)
    for driver in drivers:
        if driver["Name"] == printer_info["pDriverName"]:
            printer_driver = driver
   
    raw_type = "XPS_PASS" if printer_driver["Version"] == 4 else "RAW"
    try:
        hJob = win32print.StartDocPrinter(hPrinter, 1, ("test of raw data", None, raw_type))
        try:
            win32print.StartPagePrinter(hPrinter)
            win32print.WritePrinter(hPrinter, raw_data)
            win32print.EndPagePrinter(hPrinter)
    
        except Exception as e:
            message.warning(request, f"Error! {e}")
        finally:
            win32print.EndDocPrinter(hPrinter)  
    except Exception as e:
        message.warning(request, f'Error! {e}')
    finally:
        win32print.ClosePrinter(hPrinter)
except Exception as e:
    message.warning(request, f"Error printing! {e}")

Aquí está el problema con todo detalle:

win32print.WritePrinter(hPrinter, raw_data + b'\n\x1D\x56\x42\x10')
solucion b'\n\x1D\x56\x42\x10')

@KJCAUS
Copy link
Copy Markdown

KJCAUS commented Feb 20, 2024

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment