def printEscpos(): if request.method == "POST": # get json data from request requestBody = request.get_json() ipPrintert = request.args.get('ip', False) fileString = requestBody.get('image', False) # check required body request if fileString == '' or fileString == False: return make_response( jsonify(message='Required image', success=False), 422) if ipPrintert == '' or ipPrintert == False: return make_response( jsonify(message='Required ip in query', success=False), 422) try: # service convert base64 to image imgdata = service.base64ToImage(fileString) if imgdata == '' or imgdata == False: return make_response( jsonify(message='Convert base64 to image error', success=False), 422) # print to thermal escpos printer = Network(host=ipPrintert, port=9100, timeout=30) printer.image(imgdata) printer.cut() # printer.cashdraw('pin') return make_response( jsonify(success=True, message='Success print to thermal ' + ipPrintert), 200) except Exception as e: message = str(e) logging.exception(message) return make_response( jsonify(message='Failed print to thermal ' + ipPrintert, success=False, detail=message), 500)
class Printer: printer = None def __init__(self, ip): self.printer = Network(ip) def Test(self): return True def Print(self, text, convertesc, cutprint, selfont): if self.ConvertBool(convertesc): text = text.replace("\\n", "\n") text = text.replace("\\t", "\t") text = text.replace("\\\\", chr(92)) # text = text.replace("\\", "***") self.printer.set(font=selfont) self.printer.text(text) self.printer.set(font='a') if self.ConvertBool(cutprint): self.printer.cut() def PrintBoxTag(self, caseNumbers, Organisation, inDate, outDate, Contents, Serials): img = qrcode.make("ID" + str(caseNumbers), box_size=8) img.save("out.png") self.printer.set(align=u'center', font='a') self.printer.text("Hospitality Technology\n") self.printer.text("Hardware Department\n") self.printer.image("out.png") self.printer.set(align=u'left') caseNumberString = "" for caseNo in caseNumbers: caseNumberString += " " + caseNo + "\n" contentString = "" for hardware in Contents: contentString += " " + hardware + "\n" serialString = "" for serial in Serials: serialString += " " + serial + "\n" self.printer.text("Property : " + Organisation + "\n") self.printer.text("Received Date : " + inDate + "\n") self.printer.text("Released Date : " + outDate + "\n") self.printer.text("Case No :\n") self.printer.text(caseNumberString + "\n") self.printer.text("Contents :\n") self.printer.text(contentString + "\n") self.printer.text("Serials :\n") self.printer.text(serialString + "\n") self.printer.cut() def StressTest(self, line_count): chararr = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" ] for y in range(line_count): string = "" for x in range(42): string += chararr[randrange(0, len(chararr) - 1)] self.printer.text(string) self.printer.text("\n\n") self.printer.cut() def AdvancedStressTest(self, line_count, infont, alphabet, numbers, specialChars, CutAfterPrint): alphabet = self.ConvertBool(alphabet) numbers = self.ConvertBool(numbers) specialChars = self.ConvertBool(specialChars) CutAfterPrint = self.ConvertBool(CutAfterPrint) finalArray = [] alphabetArray = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ] numberArray = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] specialCharsArray = [ "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "=", "+", "\\", "/", "<", ">", ".", ",", '"', "'", ";", ":", "[", "]", "|" ] if numbers: finalArray.extend(numberArray) if alphabet: finalArray.extend(alphabetArray) if specialChars: finalArray.extend(specialCharsArray) self.printer.set(font=infont) for y in range(int(line_count)): string = "" for x in range(42): string += choice(finalArray) self.printer.text(string) if CutAfterPrint: self.printer.text("\n\n") self.printer.cut() self.printer.set(font='a') def ConvertBool(self, instr): if (instr.__eq__("false")): return False return True
def pos_print(order=None): if order is None: order = [] try: epson = Network("192.168.1.100") epson.image("/home/sysop/pos/rosie.png") bar = '0000' price = 0 for m in order: epson.set(height=2, width=2, align='center') epson.text('Your PIN\n') epson.set(font='a', height=3, width=3, align='center') epson.text(str(m[3]) + '\n') bar = str(m[3]) epson.set(font='a', height=2, width=2, align='left', text_type='u2') epson.text(' Order \n') epson.set(text_type='normal') epson.text(str(m[0])) if int(m[0]) > 1: epson.text(' Cups of Donuts') price = int(m[0]) * 4.76 epson.text(' $' + str(price) + '\n') price = int(m[0]) * 4.7619 else: epson.text(' Cup of Donuts') epson.text(' $4.76\n') price = 4.7619 n = str(m[1]) if int(n[0]) > 1: p = int(n[0]) - 1 epson.text(str(p) + ' ') epson.text('Mountain Dew') p2 = p * 1.9047 p = p * 1.90 price = price + p2 epson.text(' $' + str(p) + '0\n') if int(n[1]) > 0: epson.text(n[1] + ' ') epson.text('Root Beer') p = int(n[1]) * 1.90 p2 = int(n[1]) * 1.9047 price = price + p2 epson.text(' $' + str(p) + '0\n') if int(n[2]) > 0: epson.text(n[2] + ' ') epson.text('Pepsi') p = int(n[1]) * 1.90 p2 = int(n[1]) * 1.9047 price = price + p2 epson.text(' $' + str(p) + '0\n') if int(n[3]) > 0: epson.text(n[3] + ' ') epson.text('7 Up') p = int(n[1]) * 1.90 p2 = int(n[1]) * 1.9047 price = price + p2 epson.text(' $' + str(p) + '0\n') gst = price * .05 nice_gst = math.ceil(gst * 100) / 100 epson.text(' GST: $' + str(nice_gst) + '\n') print(nice_gst) price = price + gst tot = math.ceil(price * 100) / 100 epson.text(' Total: $' + str(tot) + '0\n') print(tot) epson.text('\n\n') epson.set(font='a', height=2, width=2, align='center') epson.text('THANK YOU\n') epson.set(font='a', height=1, width=1, align='center') epson.barcode(bar, 'CODE128', function_type="B") epson.cut() except Exception as ex: print(ex)
# -*- coding: cp1254 -*- from escpos.printer import Network p = Network("192.168.2.223") p.image('./images/bishop.png') p.text("MUTFAK\n") p.text(chr(27)) p.text(chr(45)) p.text(chr(50)) p.text(u'DONDURUCU\n') p.text(chr(27)) p.text(chr(33)) p.text(chr(17)) p.text('NEN 2016 \n') p.barcode("3057", "CODE39",80,3,"BELOW", "A") p.text('\n') p.barcode("3058", "CODE39",80,3) p.cut()
print 'Usage: thermalrule.py printer=true/false rule=[0-255] lines=80 interface=tcp/serial' exit() if printer: if interface == 'tcp': print 'Using TCP interface', host, tcpport Epson = Network(host=host, port=tcpport, timeout=60) time.sleep(2) # wait for network else: print 'Using serial interface', devfile, baud Epson = Serial(devfile=port, baudrate=baud, dsrdtr=True) ret = '\r\n' else: ret = '\n' Epson.image('rulemart.gif', impl='bitImageColumn') Epson.set(font='a') Epson.text('Rule : ' + str(rulenum) + '\n') Epson.text('Lines : ' + str(linesperpage) + '\n') Epson.text(datetime.datetime.now().strftime('Date : %Y/%m/%d %H:%M:%S\n')) output('\x1b\x21\x01', lf=False) # font 'b', 56 chars wide output('\x1b\x33\x20', lf=False) # line spacing output(ret * 3, lf=False) rule30(rule=rulenum) output(ret * 3, lf=False) Epson.barcode(code=datetime.datetime.now().strftime("%Y%m%d%H%M%S"), bc='CODE93', function_type='B') output(ret * 2, lf=False) Epson.set(align='center', font='a') Epson.text('Thank you! Please rule again.')