Inspired by Mike 42's Chrome raw print extension. A simple solution has been found for all window browsers. :)
Yeah, we all know that, but agents cost money, and free one has limits and annoying popups, we don't trust them.
Wait, are we talking about slient printing, or "window.print()", or "kiosk mode", no window printing dialog, preview, etc.etc. Just a click on the webpage, cashdraw pop open, ticket printed...
KwickPython is a short(<50 lines) python script. Support these printing features:
Javascript interface to KwickPython agent in port 9100
$.ajax({url:'http://127.0.0.1:9100/htbin/kp.py', success:function(list_printers){ console.log(list_printers) } });
$.ajax({url:'http://127.0.0.1:9100/htbin/kp.py', data:{p:'printer_name'}, success:function(status){ console.log(status) } });
$.ajax({url:'http://127.0.0.1:9100/htbin/kp.py', data:{p:'printer_name',d:'base64_encoded_data'}, success:function(bytes){ console.log(bytes) } });
To build KwickPython in MS Windows environment, you need both python and pywin32 installed. After python and pywin32 successfuly installed, follow these steps:
C:\Users\Owner>mkdir C:\KwickPython\htbin C:\Users\Owner>cd C:\KwickPyton C:\KwickPyton>python -m http.server --cgi 9100 Serving HTTP on 0.0.0.0 port 9100 (http://0.0.0.0:9100/) ...
C:\KwickPython\>python -m http.server --cgi 1234 Serving HTTP on 0.0.0.0 port 1234 (http://0.0.0.0:1234/) ...
Window can add any shared printers, and KwickPython will print to them. Just find the right printer name, and add it to your ajax code.
As you can see, the default access control is very open, "*", accept request from any sites. But you can tight it up.
print('Access-Control-Allow-Origin: *')Change to
print('Access-Control-Allow-Origin: http://yoursite.com')
Such as getting clients internal/local IP address, anything you can think of. But keep in mind, it might be harmful when your clients visit malicious sites.
#KwickPython [FREE TO USE, NO WARRANTY] #link back to https://kwickpos.com import cgi,os,sys,win32print,pywintypes print("Access-Control-Allow-Origin: *") print("Content-Type: text/plain\n") form = cgi.FieldStorage() p=form.getvalue('p') if p: data=form.getvalue('data') if data: import base64 raw_data=base64.b64decode(data) if raw_data: h = win32print.OpenPrinter(p) hJob = win32print.StartDocPrinter (h, 1, ("KwickPOS Ticket", None, "RAW")) win32print.StartPagePrinter (h) b=win32print.WritePrinter (h, raw_data) win32print.EndPagePrinter (h) win32print.EndDocPrinter (h) win32print.ClosePrinter (h) print(b) sys.exit() else: try: h = win32print.OpenPrinter(p) d = win32print.GetPrinter(h,2) win32print.ClosePrinter (h) a = d['Attributes'] hex=hex(a) if ( hex[-3] == '6' ): print(p+' Offline') sys.exit() s = d['Status'] if ( s == 1 ): print(p+' Paused') sys.exit() print(p+' Ready') except pywintypes.error: print(p+' No Such Printer') else: import json p={} lst = win32print.EnumPrinters(2) for f,d,n,c in lst: p[n]={} p[n]['d']=d p[n]['c']=c print(json.dumps(p))