2010-02-26 08:54:46 +00:00
|
|
|
#!/usr/bin/python
|
2014-05-21 05:15:54 +00:00
|
|
|
"""
|
2010-02-26 08:54:46 +00:00
|
|
|
@author: Manuel F Martinez <manpaz@bashlinux.com>
|
|
|
|
@organization: Bashlinux
|
2012-09-24 04:24:45 +00:00
|
|
|
@copyright: Copyright (c) 2012 Bashlinux
|
2015-06-10 23:28:27 +00:00
|
|
|
@license: GNU GPL v3
|
2014-05-21 05:15:54 +00:00
|
|
|
"""
|
2010-02-26 08:54:46 +00:00
|
|
|
|
2014-02-24 05:10:34 +00:00
|
|
|
try:
|
|
|
|
import Image
|
|
|
|
except ImportError:
|
|
|
|
from PIL import Image
|
|
|
|
|
2013-03-14 08:31:07 +00:00
|
|
|
import qrcode
|
2010-02-26 08:54:46 +00:00
|
|
|
|
|
|
|
from constants import *
|
|
|
|
from exceptions import *
|
|
|
|
|
2015-11-27 20:20:12 +00:00
|
|
|
|
2015-11-27 20:24:47 +00:00
|
|
|
class Escpos(object):
|
2010-02-26 08:54:46 +00:00
|
|
|
""" ESC/POS Printer object """
|
2015-11-27 20:20:12 +00:00
|
|
|
device = None
|
2010-02-26 08:54:46 +00:00
|
|
|
|
2015-11-27 20:20:12 +00:00
|
|
|
@staticmethod
|
|
|
|
def _check_image_size(size):
|
2012-09-24 04:24:45 +00:00
|
|
|
""" Check and fix the size of the image to 32 bits """
|
2010-02-26 08:54:46 +00:00
|
|
|
if size % 32 == 0:
|
2015-11-27 20:20:12 +00:00
|
|
|
return 0, 0
|
2010-02-26 08:54:46 +00:00
|
|
|
else:
|
|
|
|
image_border = 32 - (size % 32)
|
|
|
|
if (image_border % 2) == 0:
|
2015-11-27 20:20:12 +00:00
|
|
|
return image_border / 2, image_border / 2
|
2010-02-26 08:54:46 +00:00
|
|
|
else:
|
2015-11-27 20:20:12 +00:00
|
|
|
return image_border / 2, (image_border / 2) + 1
|
2010-02-26 08:54:46 +00:00
|
|
|
|
|
|
|
def _print_image(self, line, size):
|
2012-09-24 04:24:45 +00:00
|
|
|
""" Print formatted image """
|
2010-02-26 08:54:46 +00:00
|
|
|
i = 0
|
|
|
|
cont = 0
|
2015-11-27 20:38:59 +00:00
|
|
|
pbuffer = ""
|
2015-11-27 20:20:12 +00:00
|
|
|
|
2010-02-26 08:54:46 +00:00
|
|
|
self._raw(S_RASTER_N)
|
2015-11-27 20:38:59 +00:00
|
|
|
pbuffer = "%02X%02X%02X%02X" % (((size[0]/size[1])/8), 0, size[1] & 0xff, size[1] >> 8)
|
|
|
|
self._raw(pbuffer.decode('hex'))
|
|
|
|
pbuffer = ""
|
2010-02-26 08:54:46 +00:00
|
|
|
|
|
|
|
while i < len(line):
|
2015-11-27 20:20:12 +00:00
|
|
|
hex_string = int(line[i:i+8], 2)
|
2015-11-27 20:38:59 +00:00
|
|
|
pbuffer += "%02X" % hex_string
|
2010-02-26 08:54:46 +00:00
|
|
|
i += 8
|
|
|
|
cont += 1
|
|
|
|
if cont % 4 == 0:
|
2015-11-27 20:38:59 +00:00
|
|
|
self._raw(pbuffer.decode("hex"))
|
|
|
|
pbuffer = ""
|
2010-02-26 08:54:46 +00:00
|
|
|
cont = 0
|
|
|
|
|
2013-03-14 08:31:07 +00:00
|
|
|
def _convert_image(self, im):
|
2012-09-24 04:24:45 +00:00
|
|
|
""" Parse image and prepare it to a printable format """
|
2015-11-27 20:20:12 +00:00
|
|
|
pixels = []
|
2010-02-26 08:54:46 +00:00
|
|
|
pix_line = ""
|
2015-11-27 20:20:12 +00:00
|
|
|
im_left = ""
|
2010-02-26 08:54:46 +00:00
|
|
|
im_right = ""
|
2015-11-27 20:20:12 +00:00
|
|
|
switch = 0
|
|
|
|
img_size = [0, 0]
|
2010-02-26 08:54:46 +00:00
|
|
|
|
|
|
|
if im.size[0] > 512:
|
2015-11-27 20:20:12 +00:00
|
|
|
print ("WARNING: Image is wider than 512 and could be truncated at print time ")
|
2015-01-31 23:46:15 +00:00
|
|
|
if im.size[1] > 0xffff:
|
2010-02-26 08:54:46 +00:00
|
|
|
raise ImageSizeError()
|
|
|
|
|
|
|
|
im_border = self._check_image_size(im.size[0])
|
|
|
|
for i in range(im_border[0]):
|
|
|
|
im_left += "0"
|
|
|
|
for i in range(im_border[1]):
|
|
|
|
im_right += "0"
|
|
|
|
|
|
|
|
for y in range(im.size[1]):
|
|
|
|
img_size[1] += 1
|
|
|
|
pix_line += im_left
|
|
|
|
img_size[0] += im_border[0]
|
|
|
|
for x in range(im.size[0]):
|
|
|
|
img_size[0] += 1
|
|
|
|
RGB = im.getpixel((x, y))
|
|
|
|
im_color = (RGB[0] + RGB[1] + RGB[2])
|
|
|
|
im_pattern = "1X0"
|
|
|
|
pattern_len = len(im_pattern)
|
2015-11-27 20:20:12 +00:00
|
|
|
switch = (switch - 1) * (-1)
|
2010-02-26 08:54:46 +00:00
|
|
|
for x in range(pattern_len):
|
|
|
|
if im_color <= (255 * 3 / pattern_len * (x+1)):
|
|
|
|
if im_pattern[x] == "X":
|
|
|
|
pix_line += "%d" % switch
|
|
|
|
else:
|
|
|
|
pix_line += im_pattern[x]
|
|
|
|
break
|
2015-11-27 20:22:27 +00:00
|
|
|
elif (255 * 3 / pattern_len * pattern_len) < im_color <= (255 * 3):
|
2010-02-26 08:54:46 +00:00
|
|
|
pix_line += im_pattern[-1]
|
2015-11-27 20:20:12 +00:00
|
|
|
break
|
2010-02-26 08:54:46 +00:00
|
|
|
pix_line += im_right
|
|
|
|
img_size[0] += im_border[1]
|
|
|
|
|
|
|
|
self._print_image(pix_line, img_size)
|
|
|
|
|
2015-11-27 20:20:12 +00:00
|
|
|
def image(self, path_img):
|
2013-03-14 08:31:07 +00:00
|
|
|
""" Open image file """
|
|
|
|
im_open = Image.open(path_img)
|
2015-08-22 19:43:09 +00:00
|
|
|
|
2015-11-27 20:20:12 +00:00
|
|
|
# Remove the alpha channel on transparent images
|
|
|
|
if im_open.mode == 'RGBA':
|
|
|
|
im_open.load()
|
|
|
|
im = Image.new("RGB", im_open.size, (255, 255, 255))
|
|
|
|
im.paste(im_open, mask=im_open.split()[3])
|
|
|
|
else:
|
|
|
|
im = im_open.convert("RGB")
|
2015-08-22 19:43:09 +00:00
|
|
|
|
2013-03-14 08:34:48 +00:00
|
|
|
# Convert the RGB image in printable image
|
2013-03-14 08:31:07 +00:00
|
|
|
self._convert_image(im)
|
|
|
|
|
2015-11-27 20:20:12 +00:00
|
|
|
def qr(self, text):
|
2013-03-14 08:31:07 +00:00
|
|
|
""" Print QR Code for the provided string """
|
|
|
|
qr_code = qrcode.QRCode(version=4, box_size=4, border=1)
|
2013-03-14 08:34:48 +00:00
|
|
|
qr_code.add_data(text)
|
|
|
|
qr_code.make(fit=True)
|
|
|
|
qr_img = qr_code.make_image()
|
2013-03-14 08:31:07 +00:00
|
|
|
im = qr_img._img.convert("RGB")
|
2015-08-22 19:43:09 +00:00
|
|
|
|
2013-03-14 08:34:48 +00:00
|
|
|
# Convert the RGB image in printable image
|
2013-03-14 08:31:07 +00:00
|
|
|
self._convert_image(im)
|
|
|
|
|
2015-11-27 20:20:12 +00:00
|
|
|
def charcode(self, code):
|
2014-05-21 05:15:54 +00:00
|
|
|
""" Set Character Code Table """
|
|
|
|
if code.upper() == "USA":
|
|
|
|
self._raw(CHARCODE_PC437)
|
|
|
|
elif code.upper() == "JIS":
|
|
|
|
self._raw(CHARCODE_JIS)
|
|
|
|
elif code.upper() == "MULTILINGUAL":
|
|
|
|
self._raw(CHARCODE_PC850)
|
|
|
|
elif code.upper() == "PORTUGUESE":
|
|
|
|
self._raw(CHARCODE_PC860)
|
|
|
|
elif code.upper() == "CA_FRENCH":
|
|
|
|
self._raw(CHARCODE_PC863)
|
|
|
|
elif code.upper() == "NORDIC":
|
|
|
|
self._raw(CHARCODE_PC865)
|
|
|
|
elif code.upper() == "WEST_EUROPE":
|
|
|
|
self._raw(CHARCODE_WEU)
|
|
|
|
elif code.upper() == "GREEK":
|
|
|
|
self._raw(CHARCODE_GREEK)
|
|
|
|
elif code.upper() == "HEBREW":
|
|
|
|
self._raw(CHARCODE_HEBREW)
|
|
|
|
elif code.upper() == "LATVIAN":
|
|
|
|
self._raw(CHARCODE_PC755)
|
|
|
|
elif code.upper() == "WPC1252":
|
|
|
|
self._raw(CHARCODE_PC1252)
|
|
|
|
elif code.upper() == "CIRILLIC2":
|
|
|
|
self._raw(CHARCODE_PC866)
|
|
|
|
elif code.upper() == "LATIN2":
|
|
|
|
self._raw(CHARCODE_PC852)
|
|
|
|
elif code.upper() == "EURO":
|
|
|
|
self._raw(CHARCODE_PC858)
|
|
|
|
elif code.upper() == "THAI42":
|
|
|
|
self._raw(CHARCODE_THAI42)
|
|
|
|
elif code.upper() == "THAI11":
|
|
|
|
self._raw(CHARCODE_THAI11)
|
|
|
|
elif code.upper() == "THAI13":
|
|
|
|
self._raw(CHARCODE_THAI13)
|
|
|
|
elif code.upper() == "THAI14":
|
|
|
|
self._raw(CHARCODE_THAI14)
|
|
|
|
elif code.upper() == "THAI16":
|
|
|
|
self._raw(CHARCODE_THAI16)
|
|
|
|
elif code.upper() == "THAI17":
|
|
|
|
self._raw(CHARCODE_THAI17)
|
|
|
|
elif code.upper() == "THAI18":
|
|
|
|
self._raw(CHARCODE_THAI18)
|
|
|
|
else:
|
2015-04-20 09:58:04 +00:00
|
|
|
raise CharCodeError()
|
2014-05-21 05:15:54 +00:00
|
|
|
|
2010-02-26 08:54:46 +00:00
|
|
|
def barcode(self, code, bc, width, height, pos, font):
|
|
|
|
""" Print Barcode """
|
|
|
|
# Align Bar Code()
|
|
|
|
self._raw(TXT_ALIGN_CT)
|
|
|
|
# Height
|
2015-11-27 20:20:12 +00:00
|
|
|
if height >= 2 or height <= 6:
|
2010-02-26 08:54:46 +00:00
|
|
|
self._raw(BARCODE_HEIGHT)
|
|
|
|
else:
|
|
|
|
raise BarcodeSizeError()
|
|
|
|
# Width
|
2015-11-27 20:20:12 +00:00
|
|
|
if width >= 1 or width <= 255:
|
2010-02-26 08:54:46 +00:00
|
|
|
self._raw(BARCODE_WIDTH)
|
|
|
|
else:
|
|
|
|
raise BarcodeSizeError()
|
|
|
|
# Font
|
|
|
|
if font.upper() == "B":
|
|
|
|
self._raw(BARCODE_FONT_B)
|
2015-11-27 20:20:12 +00:00
|
|
|
else: # DEFAULT FONT: A
|
2010-02-26 08:54:46 +00:00
|
|
|
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)
|
2015-11-27 20:20:12 +00:00
|
|
|
else: # DEFAULT POSITION: BELOW
|
2010-02-26 08:54:46 +00:00
|
|
|
self._raw(BARCODE_TXT_BLW)
|
2015-11-27 20:20:12 +00:00
|
|
|
# Type
|
2010-02-26 08:54:46 +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)
|
|
|
|
elif bc.upper() == "NW7":
|
|
|
|
self._raw(BARCODE_NW7)
|
|
|
|
else:
|
|
|
|
raise BarcodeTypeError()
|
|
|
|
# Print Code
|
|
|
|
if code:
|
|
|
|
self._raw(code)
|
|
|
|
else:
|
2015-11-27 20:20:12 +00:00
|
|
|
raise BarcodeCodeError()
|
2010-02-26 08:54:46 +00:00
|
|
|
|
|
|
|
def text(self, txt):
|
|
|
|
""" Print alpha-numeric text """
|
|
|
|
if txt:
|
|
|
|
self._raw(txt)
|
|
|
|
else:
|
|
|
|
raise TextError()
|
|
|
|
|
2015-11-27 20:38:59 +00:00
|
|
|
def set(self, align='left', font='a', text_type='normal', width=1, height=1, density=9):
|
2010-02-26 08:54:46 +00:00
|
|
|
""" Set text properties """
|
2013-05-30 15:47:30 +00:00
|
|
|
# Width
|
2014-05-21 05:15:54 +00:00
|
|
|
if height == 2 and width == 2:
|
2014-05-21 05:43:40 +00:00
|
|
|
self._raw(TXT_NORMAL)
|
|
|
|
self._raw(TXT_4SQUARE)
|
2014-05-21 05:15:54 +00:00
|
|
|
elif height == 2 and width != 2:
|
2013-05-30 15:47:30 +00:00
|
|
|
self._raw(TXT_NORMAL)
|
|
|
|
self._raw(TXT_2HEIGHT)
|
2014-05-21 05:15:54 +00:00
|
|
|
elif width == 2 and height != 2:
|
|
|
|
self._raw(TXT_NORMAL)
|
2013-05-30 15:47:30 +00:00
|
|
|
self._raw(TXT_2WIDTH)
|
2015-11-27 20:20:12 +00:00
|
|
|
else: # DEFAULT SIZE: NORMAL
|
2014-05-21 05:15:54 +00:00
|
|
|
self._raw(TXT_NORMAL)
|
2010-02-26 08:54:46 +00:00
|
|
|
# Type
|
2015-11-27 20:38:59 +00:00
|
|
|
if text_type.upper() == "B":
|
2010-02-26 08:54:46 +00:00
|
|
|
self._raw(TXT_BOLD_ON)
|
|
|
|
self._raw(TXT_UNDERL_OFF)
|
2015-11-27 20:38:59 +00:00
|
|
|
elif text_type.upper() == "U":
|
2010-02-26 08:54:46 +00:00
|
|
|
self._raw(TXT_BOLD_OFF)
|
|
|
|
self._raw(TXT_UNDERL_ON)
|
2015-11-27 20:38:59 +00:00
|
|
|
elif text_type.upper() == "U2":
|
2010-02-26 08:54:46 +00:00
|
|
|
self._raw(TXT_BOLD_OFF)
|
|
|
|
self._raw(TXT_UNDERL2_ON)
|
2015-11-27 20:38:59 +00:00
|
|
|
elif text_type.upper() == "BU":
|
2010-02-26 08:54:46 +00:00
|
|
|
self._raw(TXT_BOLD_ON)
|
|
|
|
self._raw(TXT_UNDERL_ON)
|
2015-11-27 20:38:59 +00:00
|
|
|
elif text_type.upper() == "BU2":
|
2010-02-26 08:54:46 +00:00
|
|
|
self._raw(TXT_BOLD_ON)
|
|
|
|
self._raw(TXT_UNDERL2_ON)
|
2015-11-27 20:38:59 +00:00
|
|
|
elif text_type.upper == "NORMAL":
|
2010-02-26 08:54:46 +00:00
|
|
|
self._raw(TXT_BOLD_OFF)
|
|
|
|
self._raw(TXT_UNDERL_OFF)
|
2013-05-30 15:47:30 +00:00
|
|
|
# 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)
|
2014-05-21 05:31:49 +00:00
|
|
|
# Density
|
|
|
|
if density == 0:
|
|
|
|
self._raw(PD_N50)
|
|
|
|
elif density == 1:
|
|
|
|
self._raw(PD_N37)
|
|
|
|
elif density == 2:
|
|
|
|
self._raw(PD_N25)
|
|
|
|
elif density == 3:
|
|
|
|
self._raw(PD_N12)
|
|
|
|
elif density == 4:
|
|
|
|
self._raw(PD_0)
|
|
|
|
elif density == 5:
|
|
|
|
self._raw(PD_P12)
|
|
|
|
elif density == 6:
|
|
|
|
self._raw(PD_P25)
|
|
|
|
elif density == 7:
|
|
|
|
self._raw(PD_P37)
|
|
|
|
elif density == 8:
|
|
|
|
self._raw(PD_P50)
|
2015-11-27 20:20:12 +00:00
|
|
|
else: # DEFAULT: DOES NOTHING
|
2014-05-21 05:31:49 +00:00
|
|
|
pass
|
2010-02-26 08:54:46 +00:00
|
|
|
|
|
|
|
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)
|
2015-11-27 20:20:12 +00:00
|
|
|
else: # DEFAULT MODE: FULL CUT
|
2010-02-26 08:54:46 +00:00
|
|
|
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)
|
2015-11-27 20:20:12 +00:00
|
|
|
else: # DEFAULT: DOES NOTHING
|
2010-02-26 08:54:46 +00:00
|
|
|
pass
|
|
|
|
|
2014-05-21 05:15:54 +00:00
|
|
|
def control(self, ctl, pos=4):
|
2010-02-26 08:54:46 +00:00
|
|
|
""" Feed control sequences """
|
2014-05-21 05:15:54 +00:00
|
|
|
# Set tab positions
|
|
|
|
if pos < 1 or pos > 16:
|
|
|
|
raise TabError()
|
|
|
|
else:
|
2015-11-27 20:20:12 +00:00
|
|
|
self._raw("".join([CTL_SET_HT, hex(pos)]))
|
2014-05-21 05:15:54 +00:00
|
|
|
# Set position
|
2010-02-26 08:54:46 +00:00
|
|
|
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":
|
2012-11-15 17:20:16 +00:00
|
|
|
self._raw(CTL_VT)
|