2013-08-22 15:16:57 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
'''
|
|
|
|
@author: Manuel F Martinez <manpaz@bashlinux.com>
|
|
|
|
@organization: Bashlinux
|
|
|
|
@copyright: Copyright (c) 2012 Bashlinux
|
|
|
|
@license: GPL
|
|
|
|
'''
|
2014-06-03 10:36:33 +00:00
|
|
|
import struct
|
2013-02-27 12:42:32 +00:00
|
|
|
import os
|
2014-03-13 17:58:55 +00:00
|
|
|
import time
|
|
|
|
import qrcode
|
2013-02-27 14:10:34 +00:00
|
|
|
import operator
|
2014-06-03 10:36:33 +00:00
|
|
|
from PIL import Image, ImageOps
|
2013-08-22 15:16:57 +00:00
|
|
|
|
2014-05-31 14:36:00 +00:00
|
|
|
from escpos.utils import *
|
|
|
|
from escpos.constants import *
|
|
|
|
from escpos.exceptions import *
|
2013-08-22 15:16:57 +00:00
|
|
|
|
2014-05-31 14:36:00 +00:00
|
|
|
class Escpos(object):
|
2013-08-22 15:16:57 +00:00
|
|
|
""" ESC/POS Printer object """
|
|
|
|
device = None
|
|
|
|
|
|
|
|
def _check_image_size(self, size):
|
|
|
|
""" Check and fix the size of the image to 32 bits """
|
|
|
|
if size % 32 == 0:
|
|
|
|
return (0, 0)
|
|
|
|
else:
|
|
|
|
image_border = 32 - (size % 32)
|
|
|
|
if (image_border % 2) == 0:
|
2014-05-31 14:05:46 +00:00
|
|
|
return (image_border // 2, image_border // 2)
|
2013-08-22 15:16:57 +00:00
|
|
|
else:
|
2014-06-03 10:36:33 +00:00
|
|
|
return (image_border // 2, image_border // 2 + 1)
|
2013-08-22 15:16:57 +00:00
|
|
|
|
2014-06-03 10:36:33 +00:00
|
|
|
def _print_image(self, imagedata, n_rows, col_bytes):
|
2013-08-22 15:16:57 +00:00
|
|
|
""" Print formatted image """
|
|
|
|
i = 0
|
|
|
|
cont = 0
|
|
|
|
buffer = ""
|
2014-03-13 17:51:14 +00:00
|
|
|
|
2013-08-22 15:16:57 +00:00
|
|
|
self._raw(S_RASTER_N)
|
2014-06-03 10:36:33 +00:00
|
|
|
buffer = struct.pack('<HH', col_bytes, n_rows)
|
|
|
|
self._raw(buffer)
|
|
|
|
self._raw(imagedata)
|
2013-08-22 15:16:57 +00:00
|
|
|
|
2014-08-16 00:17:35 +00:00
|
|
|
def fullimage(self, img, max_height=860, width=512, histeq=True, bandsize=255):
|
2013-02-27 14:10:34 +00:00
|
|
|
""" Resizes and prints an arbitrarily sized image """
|
2013-02-27 22:17:56 +00:00
|
|
|
if isinstance(img, Image.Image):
|
2013-02-27 14:10:34 +00:00
|
|
|
im = img.convert("RGB")
|
|
|
|
else:
|
|
|
|
im = Image.open(img).convert("RGB")
|
|
|
|
|
|
|
|
if histeq:
|
|
|
|
# Histogram equaliztion
|
|
|
|
h = im.histogram()
|
|
|
|
lut = []
|
|
|
|
for b in range(0, len(h), 256):
|
|
|
|
# step size
|
2014-05-31 14:05:46 +00:00
|
|
|
step = reduce(operator.add, h[b:b+256]) // 255
|
2013-02-27 14:10:34 +00:00
|
|
|
# create equalization lookup table
|
|
|
|
n = 0
|
|
|
|
for i in range(256):
|
2014-05-31 14:05:46 +00:00
|
|
|
lut.append(n // step)
|
2013-02-27 14:10:34 +00:00
|
|
|
n = n + h[i+b]
|
|
|
|
im = im.point(lut)
|
|
|
|
|
2014-05-22 05:08:58 +00:00
|
|
|
if width:
|
|
|
|
ratio = float(width) / im.size[0]
|
|
|
|
newheight = int(ratio * im.size[1])
|
2013-02-27 14:10:34 +00:00
|
|
|
|
2014-05-22 05:08:58 +00:00
|
|
|
# Resize the image
|
|
|
|
im = im.resize((width, newheight), Image.ANTIALIAS)
|
|
|
|
|
|
|
|
if max_height and im.size[1] > max_height:
|
2013-02-27 14:10:34 +00:00
|
|
|
im = im.crop((0, 0, im.size[0], max_height))
|
2013-08-22 15:16:57 +00:00
|
|
|
|
2013-02-27 14:10:34 +00:00
|
|
|
# Divide into bands
|
|
|
|
current = 0
|
|
|
|
while current < im.size[1]:
|
2014-05-22 05:08:58 +00:00
|
|
|
self.image(im.crop((0, current, width or im.size[0],
|
|
|
|
min(im.size[1], current + bandsize))))
|
2013-02-27 14:10:34 +00:00
|
|
|
current += bandsize
|
|
|
|
|
2014-06-03 10:36:33 +00:00
|
|
|
def image(self, im):
|
2013-08-22 15:16:57 +00:00
|
|
|
""" Parse image and prepare it to a printable format """
|
|
|
|
pixels = []
|
|
|
|
pix_line = ""
|
|
|
|
im_left = ""
|
|
|
|
im_right = ""
|
|
|
|
switch = 0
|
|
|
|
img_size = [ 0, 0 ]
|
|
|
|
|
2014-06-03 10:36:33 +00:00
|
|
|
if not isinstance(im, Image.Image):
|
|
|
|
im = Image.open(im)
|
2013-08-22 15:16:57 +00:00
|
|
|
|
2014-06-03 10:36:33 +00:00
|
|
|
im = im.convert("L")
|
|
|
|
im = ImageOps.invert(im)
|
|
|
|
im = im.convert("1")
|
2013-08-22 15:16:57 +00:00
|
|
|
|
2014-06-03 10:36:33 +00:00
|
|
|
if im.size[0] > 640:
|
|
|
|
print("WARNING: Image is wider than 640 and could be truncated at print time ")
|
|
|
|
if im.size[1] > 640:
|
2013-08-22 15:16:57 +00:00
|
|
|
raise ImageSizeError()
|
2014-06-03 10:36:33 +00:00
|
|
|
|
|
|
|
orig_width, height = im.size
|
|
|
|
width = ((orig_width + 31) // 32) * 32
|
|
|
|
new_image = Image.new("1", (width, height))
|
|
|
|
new_image.paste(im, (0, 0, orig_width, height))
|
2013-08-22 15:16:57 +00:00
|
|
|
|
2014-06-03 10:36:33 +00:00
|
|
|
the_bytes = new_image.tobytes()
|
|
|
|
self._print_image(the_bytes, n_rows=height, col_bytes=width//8)
|
2013-08-22 15:16:57 +00:00
|
|
|
|
2014-03-13 17:58:55 +00:00
|
|
|
def qr(self, text):
|
2013-08-22 15:16:57 +00:00
|
|
|
""" Print QR Code for the provided string """
|
2014-08-16 13:28:20 +00:00
|
|
|
qr_code = qrcode.QRCode(version=4, box_size=4, border=1, error_correction=qrcode.constants.ERROR_CORRECT_H)
|
2013-08-22 15:16:57 +00:00
|
|
|
qr_code.add_data(text)
|
|
|
|
qr_code.make(fit=True)
|
|
|
|
qr_img = qr_code.make_image()
|
|
|
|
# Convert the RGB image in printable image
|
2014-06-03 10:36:33 +00:00
|
|
|
im = qr_img._img.convert("1")
|
|
|
|
width = im.size[0]
|
|
|
|
height = im.size[1]
|
|
|
|
while width * 2 <= 640:
|
|
|
|
width *= 2
|
|
|
|
height *= 2
|
|
|
|
|
|
|
|
im = im.resize((width, height))
|
2014-03-13 17:58:55 +00:00
|
|
|
self.image(im)
|
2014-05-22 05:41:02 +00:00
|
|
|
self.text('\n')
|
2013-08-22 15:16:57 +00:00
|
|
|
|
2014-05-22 04:45:57 +00:00
|
|
|
def barcode(self, code, bc, height, width, pos, font):
|
2013-08-22 15:16:57 +00:00
|
|
|
""" Print Barcode """
|
|
|
|
# Align Bar Code()
|
|
|
|
self._raw(TXT_ALIGN_CT)
|
|
|
|
# Height
|
2014-05-22 04:45:57 +00:00
|
|
|
if 1 <= height <= 255:
|
|
|
|
self._raw(BARCODE_HEIGHT + chr(height))
|
2013-08-22 15:16:57 +00:00
|
|
|
else:
|
2014-05-22 04:47:08 +00:00
|
|
|
raise BarcodeSizeError("height = %s" % height)
|
2013-08-22 15:16:57 +00:00
|
|
|
# Width
|
2014-05-22 04:45:57 +00:00
|
|
|
if 2 <= width <= 6:
|
|
|
|
self._raw(BARCODE_WIDTH + chr(width))
|
2013-08-22 15:16:57 +00:00
|
|
|
else:
|
2014-05-22 04:47:08 +00:00
|
|
|
raise BarcodeSizeError("width = %s" % width)
|
2013-08-22 15:16:57 +00:00
|
|
|
# Font
|
|
|
|
if font.upper() == "B":
|
|
|
|
self._raw(BARCODE_FONT_B)
|
|
|
|
else: # DEFAULT FONT: A
|
|
|
|
self._raw(BARCODE_FONT_A)
|
|
|
|
# Position
|
|
|
|
if pos.upper() == "OFF":
|
|
|
|
self._raw(BARCODE_TXT_OFF)
|
|
|
|
elif pos.upper() == "BOTH":
|
|
|
|
self._raw(BARCODE_TXT_BTH)
|
|
|
|
elif pos.upper() == "ABOVE":
|
|
|
|
self._raw(BARCODE_TXT_ABV)
|
2014-03-13 17:51:14 +00:00
|
|
|
else: # DEFAULT POSITION: BELOW
|
2013-08-22 15:16:57 +00:00
|
|
|
self._raw(BARCODE_TXT_BLW)
|
2014-03-13 17:51:14 +00:00
|
|
|
# Type
|
2013-08-22 15:16:57 +00:00
|
|
|
if bc.upper() == "UPC-A":
|
|
|
|
self._raw(BARCODE_UPC_A)
|
|
|
|
elif bc.upper() == "UPC-E":
|
|
|
|
self._raw(BARCODE_UPC_E)
|
|
|
|
elif bc.upper() == "EAN13":
|
|
|
|
self._raw(BARCODE_EAN13)
|
|
|
|
elif bc.upper() == "EAN8":
|
|
|
|
self._raw(BARCODE_EAN8)
|
|
|
|
elif bc.upper() == "CODE39":
|
|
|
|
self._raw(BARCODE_CODE39)
|
|
|
|
elif bc.upper() == "ITF":
|
|
|
|
self._raw(BARCODE_ITF)
|
2014-05-22 04:48:24 +00:00
|
|
|
elif bc.upper() in ("NW7", "CODABAR"):
|
2013-08-22 15:16:57 +00:00
|
|
|
self._raw(BARCODE_NW7)
|
|
|
|
else:
|
2014-05-22 04:47:08 +00:00
|
|
|
raise BarcodeTypeError(bc)
|
2013-08-22 15:16:57 +00:00
|
|
|
# Print Code
|
|
|
|
if code:
|
|
|
|
self._raw(code)
|
|
|
|
else:
|
|
|
|
raise exception.BarcodeCodeError()
|
|
|
|
|
|
|
|
def text(self, txt):
|
|
|
|
""" Print alpha-numeric text """
|
|
|
|
if txt:
|
2014-03-13 18:05:41 +00:00
|
|
|
self._raw(txt.encode('cp936'))
|
2013-08-22 15:16:57 +00:00
|
|
|
else:
|
|
|
|
raise TextError()
|
|
|
|
|
|
|
|
def set(self, align='left', font='a', type='normal', width=1, height=1):
|
|
|
|
""" Set text properties """
|
|
|
|
# Width
|
2014-03-14 16:17:32 +00:00
|
|
|
if height != 2 and width != 2: # DEFAULT SIZE: NORMAL
|
2013-08-22 15:16:57 +00:00
|
|
|
self._raw(TXT_NORMAL)
|
2014-03-14 16:17:32 +00:00
|
|
|
|
|
|
|
if height == 2:
|
2013-08-22 15:16:57 +00:00
|
|
|
self._raw(TXT_2HEIGHT)
|
2014-03-14 16:17:32 +00:00
|
|
|
if width == 2:
|
2013-08-22 15:16:57 +00:00
|
|
|
self._raw(TXT_2WIDTH)
|
2014-03-14 16:17:32 +00:00
|
|
|
|
|
|
|
if height == 2 and width == 2:
|
|
|
|
self._raw(TXT_4SQUARE)
|
|
|
|
|
2013-08-22 15:16:57 +00:00
|
|
|
# Type
|
|
|
|
if type.upper() == "B":
|
|
|
|
self._raw(TXT_BOLD_ON)
|
|
|
|
self._raw(TXT_UNDERL_OFF)
|
|
|
|
elif type.upper() == "U":
|
|
|
|
self._raw(TXT_BOLD_OFF)
|
|
|
|
self._raw(TXT_UNDERL_ON)
|
|
|
|
elif type.upper() == "U2":
|
|
|
|
self._raw(TXT_BOLD_OFF)
|
|
|
|
self._raw(TXT_UNDERL2_ON)
|
|
|
|
self._raw(TXT_ITALIC_OFF)
|
|
|
|
elif type.upper() == "BU":
|
|
|
|
self._raw(TXT_BOLD_ON)
|
|
|
|
self._raw(TXT_UNDERL_ON)
|
|
|
|
elif type.upper() == "BU2":
|
|
|
|
self._raw(TXT_BOLD_ON)
|
|
|
|
self._raw(TXT_UNDERL2_ON)
|
|
|
|
elif type.upper == "NORMAL":
|
|
|
|
self._raw(TXT_BOLD_OFF)
|
|
|
|
self._raw(TXT_UNDERL_OFF)
|
|
|
|
# Font
|
|
|
|
if font.upper() == "B":
|
|
|
|
self._raw(TXT_FONT_B)
|
|
|
|
else: # DEFAULT FONT: A
|
|
|
|
self._raw(TXT_FONT_A)
|
|
|
|
# Align
|
|
|
|
if align.upper() == "CENTER":
|
|
|
|
self._raw(TXT_ALIGN_CT)
|
|
|
|
elif align.upper() == "RIGHT":
|
|
|
|
self._raw(TXT_ALIGN_RT)
|
|
|
|
elif align.upper() == "LEFT":
|
|
|
|
self._raw(TXT_ALIGN_LT)
|
|
|
|
|
|
|
|
def cut(self, mode=''):
|
|
|
|
""" Cut paper """
|
|
|
|
# Fix the size between last line and cut
|
|
|
|
# TODO: handle this with a line feed
|
|
|
|
self._raw("\n\n\n\n\n\n")
|
|
|
|
if mode.upper() == "PART":
|
|
|
|
self._raw(PAPER_PART_CUT)
|
|
|
|
else: # DEFAULT MODE: FULL CUT
|
|
|
|
self._raw(PAPER_FULL_CUT)
|
|
|
|
|
|
|
|
def cashdraw(self, pin):
|
|
|
|
""" Send pulse to kick the cash drawer """
|
|
|
|
if pin == 2:
|
|
|
|
self._raw(CD_KICK_2)
|
|
|
|
elif pin == 5:
|
|
|
|
self._raw(CD_KICK_5)
|
|
|
|
else:
|
|
|
|
raise CashDrawerError()
|
|
|
|
|
|
|
|
def hw(self, hw):
|
|
|
|
""" Hardware operations """
|
|
|
|
if hw.upper() == "INIT":
|
|
|
|
self._raw(HW_INIT)
|
|
|
|
elif hw.upper() == "SELECT":
|
|
|
|
self._raw(HW_SELECT)
|
|
|
|
elif hw.upper() == "RESET":
|
|
|
|
self._raw(HW_RESET)
|
|
|
|
else: # DEFAULT: DOES NOTHING
|
|
|
|
pass
|
|
|
|
|
|
|
|
def control(self, ctl):
|
|
|
|
""" Feed control sequences """
|
|
|
|
if ctl.upper() == "LF":
|
|
|
|
self._raw(CTL_LF)
|
|
|
|
elif ctl.upper() == "FF":
|
|
|
|
self._raw(CTL_FF)
|
|
|
|
elif ctl.upper() == "CR":
|
|
|
|
self._raw(CTL_CR)
|
|
|
|
elif ctl.upper() == "HT":
|
|
|
|
self._raw(CTL_HT)
|
|
|
|
elif ctl.upper() == "VT":
|
|
|
|
self._raw(CTL_VT)
|