KwickPython


Direct print to client local printer from javascript

Inspired by Mike 42's Chrome raw print extension. A simple solution has been found for all window browsers. :)

Running a client agent to accept/bridge/route print jobs from the webpage/javascript/ajax to local printer

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 FREE. And you can trust, because you build it. Download

KwickPython is a short(<50 lines) python script. Support these printing features:

  1. Get list of printers (Direct attached/usb/serial/parallel/lan, or network attched printers(shared))
  2. Check printers, Status Check/Paused, Online Check/Offline
  3. Printing jobs to selected printers(front, back, kitchen, bar, etc. printers).
*** And lot more features by expanding the KwickPython, but we stay with the basic for now.

Javascript interface to KwickPython agent in port 9100

KwickPython building steps

To build KwickPython in MS Windows environment, you need both python and pywin32 installed. After python and pywin32 successfuly installed, follow these steps:

  1. Open cmd console by Winkey-R, type "cmd" and Enter
  2. Make KwickPython cgi-bin directory: C:\KwickPython\htbin
  3. Save KwickPython script as: C:\KwickPython\htbin\kp.py
  4. Start the KwickPython: C:\KwickPython>python -m http.server --cgi 9100
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/) ...

Why Port 9100

Port 9100 is standard for most of netwok printer, you can set KwickPython to listen on 9100 or any other ports( or 1234). Just to make sure to update your ajax to the right port you set.
C:\KwickPython\>python -m http.server --cgi 1234 
Serving HTTP on 0.0.0.0 port 1234 (http://0.0.0.0:1234/) ...

RAW Printer Data

Use base64 to encode the printer data, most likely, it will be esc/pos formated binaries,or PCL, or some other fancy stuff.

Shared Printers

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.

KwickPython autostart

Now, we have this awsome KwickPython printing agent build and working, how can I make it auto start, or running without the console window?

KwickPython Security

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')

More Features

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 source

#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))

KwickPython support

Self suport or Google Search is the best. If you really need to contact us, the email is KwickPOS@gmail.com
Home © 2000 - 2017 KwickPOS.com