2010-02-26 08:54:46 +00:00
|
|
|
#!/usr/bin/python
|
2016-01-05 16:30:40 +00:00
|
|
|
""" Main class
|
|
|
|
|
|
|
|
This module contains the abstract base class :py:class:`Escpos`.
|
|
|
|
|
|
|
|
:author: `Manuel F Martinez <manpaz@bashlinux.com>`_ and others
|
|
|
|
:organization: Bashlinux and `python-escpos <https://github.com/python-escpos>`_
|
|
|
|
:copyright: Copyright (c) 2012 Bashlinux
|
|
|
|
: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
|
2015-06-04 11:57:59 +00:00
|
|
|
import textwrap
|
2015-06-10 01:21:52 +00:00
|
|
|
import binascii
|
2013-02-27 14:10:34 +00:00
|
|
|
import operator
|
2010-02-26 08:54:46 +00:00
|
|
|
|
2015-05-07 18:54:32 +00:00
|
|
|
from .constants import *
|
|
|
|
from .exceptions import *
|
2010-02-26 08:54:46 +00:00
|
|
|
|
2015-11-27 22:10:20 +00:00
|
|
|
from abc import ABCMeta, abstractmethod # abstract base class support
|
|
|
|
|
2016-01-08 02:34:14 +00:00
|
|
|
|
2015-11-27 20:24:47 +00:00
|
|
|
class Escpos(object):
|
2016-01-05 16:30:40 +00:00
|
|
|
""" ESC/POS Printer object
|
|
|
|
|
|
|
|
This class is the abstract base class for an esc/pos-printer. The printer implementations are children of this
|
|
|
|
class.
|
|
|
|
"""
|
2015-11-27 22:10:20 +00:00
|
|
|
__metaclass__ = ABCMeta
|
2015-11-27 20:20:12 +00:00
|
|
|
device = None
|
2010-02-26 08:54:46 +00:00
|
|
|
|
2015-06-04 11:57:59 +00:00
|
|
|
def __init__(self, columns=32):
|
2015-12-12 16:19:30 +00:00
|
|
|
""" Initialize ESCPOS Printer
|
|
|
|
|
|
|
|
:param columns: Text columns used by the printer. Defaults to 32."""
|
2015-06-04 11:57:59 +00:00
|
|
|
self.columns = columns
|
2010-02-26 08:54:46 +00:00
|
|
|
|
2015-11-27 22:10:20 +00:00
|
|
|
@abstractmethod
|
|
|
|
def _raw(self, msg):
|
|
|
|
""" Sends raw data to the printer
|
|
|
|
|
|
|
|
This function has to be individually implemented by the implementations.
|
2016-01-05 16:30:40 +00:00
|
|
|
|
2015-11-27 22:10:20 +00:00
|
|
|
:param msg: message string to be sent to the printer
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2015-11-27 20:20:12 +00:00
|
|
|
@staticmethod
|
|
|
|
def _check_image_size(size):
|
2015-11-27 22:10:20 +00:00
|
|
|
""" Check and fix the size of the image to 32 bits
|
|
|
|
|
|
|
|
:param size: size of the image
|
|
|
|
:returns: tuple of image borders
|
|
|
|
:rtype: (int, int)
|
|
|
|
"""
|
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:
|
2016-01-08 02:34:14 +00:00
|
|
|
return round(image_border / 2), round(image_border / 2)
|
2010-02-26 08:54:46 +00:00
|
|
|
else:
|
2016-01-08 02:34:14 +00:00
|
|
|
return round(image_border / 2), round((image_border / 2) + 1)
|
2010-02-26 08:54:46 +00:00
|
|
|
|
|
|
|
def _print_image(self, line, size):
|
2015-11-27 22:10:20 +00:00
|
|
|
""" Print formatted image
|
|
|
|
|
|
|
|
:param line:
|
|
|
|
:param size:
|
|
|
|
"""
|
2010-02-26 08:54:46 +00:00
|
|
|
i = 0
|
|
|
|
cont = 0
|
2015-11-27 20:38:59 +00:00
|
|
|
pbuffer = ""
|
2015-06-04 11:57:59 +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)
|
2015-12-12 16:19:30 +00:00
|
|
|
self._raw(binascii.unhexlify(pbuffer))
|
2015-11-27 20:38:59 +00:00
|
|
|
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-12-12 16:19:30 +00:00
|
|
|
self._raw(binascii.unhexlify(pbuffer))
|
2015-11-27 20:38:59 +00:00
|
|
|
pbuffer = ""
|
2010-02-26 08:54:46 +00:00
|
|
|
cont = 0
|
|
|
|
|
2013-03-14 08:31:07 +00:00
|
|
|
def _convert_image(self, im):
|
2015-11-27 22:10:20 +00:00
|
|
|
""" Parse image and prepare it to a printable format
|
|
|
|
|
|
|
|
:param im: image data
|
2016-01-05 16:30:40 +00:00
|
|
|
:raises: :py:exc:`~escpos.exceptions.ImageSizeError`
|
2015-11-27 22:10:20 +00:00
|
|
|
"""
|
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-06-04 11:57:59 +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):
|
2016-01-05 16:30:40 +00:00
|
|
|
""" Open and print an image file
|
|
|
|
|
|
|
|
Prints an image. The image is automatically adjusted in size in order to print it.
|
2010-02-26 08:54:46 +00:00
|
|
|
|
2016-01-14 00:00:53 +00:00
|
|
|
.. todo:: Seems to be broken. Write test that simply executes function with a dummy printer in order to
|
|
|
|
check for bugs like these in the future.
|
|
|
|
|
2016-01-05 16:30:40 +00:00
|
|
|
:param path_img: complete filename and path to image of type `jpg`, `gif`, `png` or `bmp`
|
2015-11-27 22:10:20 +00:00
|
|
|
"""
|
2013-03-14 08:31:07 +00:00
|
|
|
im_open = Image.open(path_img)
|
2015-08-22 19:43:09 +00:00
|
|
|
|
2015-08-27 21:45:15 +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)
|
|
|
|
|
2014-08-16 00:17:35 +00:00
|
|
|
def fullimage(self, img, max_height=860, width=512, histeq=True, bandsize=255):
|
2016-01-14 00:00:53 +00:00
|
|
|
""" Resizes and prints an arbitrarily sized image
|
|
|
|
|
|
|
|
.. todo:: Seems to be broken. Write test that simply executes function with a dummy printer in order to
|
|
|
|
check for bugs like these in the future.
|
|
|
|
"""
|
2013-02-27 14:10:34 +00:00
|
|
|
if isinstance(img, (Image, Image.Image)):
|
|
|
|
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
|
|
|
|
step = reduce(operator.add, h[b:b+256]) / 255
|
|
|
|
# create equalization lookup table
|
|
|
|
n = 0
|
|
|
|
for i in range(256):
|
|
|
|
lut.append(n / step)
|
|
|
|
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))
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2015-06-10 01:21:52 +00:00
|
|
|
def direct_image(self, image):
|
2016-01-05 16:30:40 +00:00
|
|
|
""" Send image to printer
|
|
|
|
|
|
|
|
:param image:
|
|
|
|
"""
|
2015-06-10 01:21:52 +00:00
|
|
|
mask = 0x80
|
|
|
|
i = 0
|
|
|
|
temp = 0
|
|
|
|
|
|
|
|
(width, height) = image.size
|
|
|
|
self._raw(S_RASTER_N)
|
|
|
|
headerX = int(width / 8)
|
|
|
|
headerY = height
|
|
|
|
buf = "%02X" % (headerX & 0xff)
|
|
|
|
buf += "%02X" % ((headerX >> 8) & 0xff)
|
|
|
|
buf += "%02X" % (headerY & 0xff)
|
|
|
|
buf += "%02X" % ((headerY >> 8) & 0xff)
|
|
|
|
#self._raw(binascii.unhexlify(buf))
|
|
|
|
for y in range(height):
|
|
|
|
for x in range(width):
|
2016-01-08 02:34:14 +00:00
|
|
|
value = image.getpixel((x, y))
|
|
|
|
value |= (value << 8)
|
2015-06-10 01:21:52 +00:00
|
|
|
if value == 0:
|
|
|
|
temp |= mask
|
|
|
|
|
2016-01-08 02:34:14 +00:00
|
|
|
mask >>= 1
|
2015-06-10 01:21:52 +00:00
|
|
|
|
|
|
|
i += 1
|
|
|
|
if i == 8:
|
2016-01-08 02:34:14 +00:00
|
|
|
buf += ("%02X" % temp)
|
2015-06-10 01:21:52 +00:00
|
|
|
mask = 0x80
|
|
|
|
i = 0
|
|
|
|
temp = 0
|
2015-06-26 00:12:30 +00:00
|
|
|
self._raw(binascii.unhexlify(bytes(buf, "ascii")))
|
2015-08-27 21:20:53 +00:00
|
|
|
self._raw('\n')
|
2015-06-10 01:21:52 +00:00
|
|
|
|
2015-11-27 20:20:12 +00:00
|
|
|
def qr(self, text):
|
2015-11-27 22:10:20 +00:00
|
|
|
""" Print QR Code for the provided string
|
2013-03-14 08:31:07 +00:00
|
|
|
|
2016-01-05 16:30:40 +00:00
|
|
|
Prints a QR-code. The size has been adjusted to version 4, so it is small enough to be
|
|
|
|
printed but also big enough to be read by a smartphone.
|
|
|
|
|
2015-11-27 22:10:20 +00:00
|
|
|
:param text: text to generate a QR-Code from
|
|
|
|
"""
|
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-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):
|
2015-11-27 22:10:20 +00:00
|
|
|
""" Set Character Code Table
|
|
|
|
|
2016-01-05 16:30:40 +00:00
|
|
|
Sends the control sequence from :py:mod:`escpos.constants` to the printer
|
|
|
|
with :py:meth:`escpos.printer.'implementation'._raw()`.
|
2013-03-14 08:31:07 +00:00
|
|
|
|
2015-11-27 22:10:20 +00:00
|
|
|
:param code: Name of CharCode
|
2016-01-05 16:30:40 +00:00
|
|
|
:raises: :py:exc:`~escpos.exceptions.CharCodeError`
|
2015-11-27 22:10:20 +00:00
|
|
|
"""
|
2014-05-21 05:15:54 +00:00
|
|
|
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)
|
2015-11-27 22:10:20 +00:00
|
|
|
# elif code.upper() == "LATVIAN": # this is not listed in the constants
|
|
|
|
# self._raw(CHARCODE_PC755)
|
2014-05-21 05:15:54 +00:00
|
|
|
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
|
|
|
|
2016-01-14 13:51:26 +00:00
|
|
|
def barcode(self, code, bc, height=64, width=3, pos="BELOW", font="A", align_ct=True):
|
2015-11-27 22:10:20 +00:00
|
|
|
""" Print Barcode
|
|
|
|
|
2016-01-14 14:50:58 +00:00
|
|
|
This method allows to print barcodes. The rendering of the barcode is done by the printer and therefore has to
|
|
|
|
be supported by the unit. Currently you have to check manually whether your barcode text is correct. Uncorrect
|
|
|
|
barcodes may lead to unexpected printer behaviour.
|
|
|
|
|
2016-01-14 13:51:26 +00:00
|
|
|
.. todo:: Add a method to check barcode codes. Alternatively or as an addition write explanations about each
|
2016-01-14 14:50:58 +00:00
|
|
|
barcode-type. Research whether the check digits can be computed autmatically.
|
|
|
|
|
|
|
|
Use the parameters `height` and `width` for adjusting of the barcode size. Please take notice that the barcode
|
|
|
|
will not be printed if it is outside of the printable area. (Which should be impossible with this method, so
|
|
|
|
this information is probably more useful for debugging purposes.)
|
|
|
|
|
2016-01-14 00:00:53 +00:00
|
|
|
.. todo:: On TM-T88II width from 1 to 6 is accepted. Try to acquire command reference and correct the code.
|
2016-01-14 13:04:35 +00:00
|
|
|
.. todo:: Supplying pos does not have an effect for every barcode type. Check and document for which types this
|
|
|
|
is true.
|
2016-01-14 00:00:53 +00:00
|
|
|
|
2016-01-14 14:50:58 +00:00
|
|
|
If you do not want to center the barcode you can call the method with `align_ct=False`, which will disable
|
|
|
|
automatic centering. Please note that when you use center alignment, then the alignment of text will be changed
|
|
|
|
automatically to centered. You have to manually restore the alignment if necessary.
|
|
|
|
|
|
|
|
.. todo:: If further barcode-types are needed they could be rendered transparently as an image. (This could also
|
|
|
|
be of help if the printer does not support types that others do.)
|
|
|
|
|
2016-01-05 16:30:40 +00:00
|
|
|
:param code: alphanumeric data to be printed as bar code
|
|
|
|
:param bc: barcode format, possible values are:
|
|
|
|
|
|
|
|
* UPC-A
|
|
|
|
* UPC-E
|
|
|
|
* EAN13
|
|
|
|
* EAN8
|
|
|
|
* CODE39
|
|
|
|
* ITF
|
|
|
|
* NW7
|
|
|
|
|
|
|
|
If none is specified, the method raises :py:exc:`~escpos.exceptions.BarcodeTypeError`.
|
2016-01-14 00:00:53 +00:00
|
|
|
:param height: barcode height, has to be between 1 and 255
|
2014-05-22 04:45:57 +00:00
|
|
|
*default*: 64
|
2016-01-14 13:51:26 +00:00
|
|
|
:type height: int
|
2016-01-14 00:00:53 +00:00
|
|
|
:param width: barcode width, has to be between 2 and 6
|
|
|
|
*default*: 3
|
2016-01-14 13:51:26 +00:00
|
|
|
:type width: int
|
|
|
|
:param pos: where to place the text relative to the barcode, *default*: BELOW
|
2016-01-05 16:30:40 +00:00
|
|
|
|
|
|
|
* ABOVE
|
|
|
|
* BELOW
|
|
|
|
* BOTH
|
|
|
|
* OFF
|
|
|
|
|
|
|
|
:param font: select font (see ESC/POS-documentation, the device often has two fonts), *default*: A
|
|
|
|
|
|
|
|
* A
|
|
|
|
* B
|
|
|
|
|
2016-01-14 13:51:26 +00:00
|
|
|
:param align_ct: If this parameter is True the barcode will be centered. Otherwise no alignment command will be
|
|
|
|
issued.
|
|
|
|
:type align_ct: bool
|
|
|
|
|
2016-01-05 16:30:40 +00:00
|
|
|
:raises: :py:exc:`~escpos.exceptions.BarcodeSizeError`,
|
|
|
|
:py:exc:`~escpos.exceptions.BarcodeTypeError`,
|
|
|
|
:py:exc:`~escpos.exceptions.BarcodeCodeError`
|
2015-11-27 22:10:20 +00:00
|
|
|
"""
|
2010-02-26 08:54:46 +00:00
|
|
|
# Align Bar Code()
|
2016-01-14 13:51:26 +00:00
|
|
|
if align_ct:
|
|
|
|
self._raw(TXT_ALIGN_CT)
|
2010-02-26 08:54:46 +00:00
|
|
|
# Height
|
2014-05-22 04:45:57 +00:00
|
|
|
if 1 <= height <= 255:
|
|
|
|
self._raw(BARCODE_HEIGHT + chr(height))
|
2010-02-26 08:54:46 +00:00
|
|
|
else:
|
2016-01-08 02:43:33 +00:00
|
|
|
raise BarcodeSizeError("height = {height}".format(height=height))
|
2010-02-26 08:54:46 +00:00
|
|
|
# Width
|
2014-05-22 04:45:57 +00:00
|
|
|
if 2 <= width <= 6:
|
|
|
|
self._raw(BARCODE_WIDTH + chr(width))
|
2010-02-26 08:54:46 +00:00
|
|
|
else:
|
2016-01-08 02:43:33 +00:00
|
|
|
raise BarcodeSizeError("width = {width}".format(width=width))
|
2010-02-26 08:54:46 +00:00
|
|
|
# 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-06-04 11:57:59 +00:00
|
|
|
else: # DEFAULT POSITION: BELOW
|
2010-02-26 08:54:46 +00:00
|
|
|
self._raw(BARCODE_TXT_BLW)
|
2015-06-04 11:57:59 +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)
|
2014-05-22 04:48:24 +00:00
|
|
|
elif bc.upper() in ("NW7", "CODABAR"):
|
2010-02-26 08:54:46 +00:00
|
|
|
self._raw(BARCODE_NW7)
|
|
|
|
else:
|
2014-05-22 04:47:08 +00:00
|
|
|
raise BarcodeTypeError(bc)
|
2010-02-26 08:54:46 +00:00
|
|
|
# Print Code
|
|
|
|
if code:
|
|
|
|
self._raw(code)
|
|
|
|
else:
|
2015-11-27 20:20:12 +00:00
|
|
|
raise BarcodeCodeError()
|
2015-06-04 11:57:59 +00:00
|
|
|
|
2010-02-26 08:54:46 +00:00
|
|
|
def text(self, txt):
|
2015-11-27 22:10:20 +00:00
|
|
|
""" Print alpha-numeric text
|
|
|
|
|
|
|
|
The text has to be encoded in the currently selected codepage.
|
2016-01-05 16:30:40 +00:00
|
|
|
|
2015-11-27 22:10:20 +00:00
|
|
|
:param txt: text to be printed
|
2016-01-05 16:30:40 +00:00
|
|
|
:raises: :py:exc:`~escpos.exceptions.TextError`
|
2015-11-27 22:10:20 +00:00
|
|
|
"""
|
2010-02-26 08:54:46 +00:00
|
|
|
if txt:
|
|
|
|
self._raw(txt)
|
|
|
|
else:
|
2016-01-05 16:30:40 +00:00
|
|
|
# TODO: why is it problematic to print an empty string?
|
2010-02-26 08:54:46 +00:00
|
|
|
raise TextError()
|
|
|
|
|
2015-06-04 11:57:59 +00:00
|
|
|
def block_text(self, txt, columns=None):
|
2016-01-05 16:30:40 +00:00
|
|
|
""" Text is printed wrapped to specified columns
|
|
|
|
|
|
|
|
:param txt: text to be printed
|
|
|
|
:param columns: amount of columns
|
|
|
|
:return: None
|
|
|
|
"""
|
2016-01-08 02:34:14 +00:00
|
|
|
colCount = self.columns if columns is None else columns
|
2015-06-04 11:57:59 +00:00
|
|
|
self.text(textwrap.fill(txt, colCount))
|
2010-02-26 08:54:46 +00:00
|
|
|
|
2016-03-01 18:05:52 +00:00
|
|
|
def set(self, align='left', font='a', text_type='normal', width=1, height=1, density=9, invert=False, smooth=False, flip=False):
|
2015-11-27 22:10:20 +00:00
|
|
|
""" Set text properties by sending them to the printer
|
|
|
|
|
2016-01-05 16:30:40 +00:00
|
|
|
:param align: horizontal position for text, possible values are:
|
|
|
|
|
|
|
|
* CENTER
|
|
|
|
* LEFT
|
|
|
|
* RIGHT
|
|
|
|
|
|
|
|
*default*: LEFT
|
|
|
|
:param font: font type, possible values are A or B, *default*: A
|
|
|
|
:param text_type: text type, possible values are:
|
|
|
|
|
|
|
|
* B for bold
|
|
|
|
* U for underlined
|
|
|
|
* U2 for underlined, version 2
|
|
|
|
* BU for bold and underlined
|
|
|
|
* BU2 for bold and underlined, version 2
|
|
|
|
* NORMAL for normal text
|
|
|
|
|
|
|
|
*default*: NORMAL
|
2016-03-01 18:05:52 +00:00
|
|
|
:param width: text width multiplier, decimal range 1-8, *default*: 1
|
|
|
|
:param height: text height multiplier, decimal range 1-8, *default*: 1
|
2016-01-05 16:30:40 +00:00
|
|
|
:param density: print density, value from 0-8, if something else is supplied the density remains unchanged
|
2016-02-29 18:04:00 +00:00
|
|
|
:param invert: True enables white on black printing, *default*: False
|
2016-03-01 18:05:52 +00:00
|
|
|
:param smooth: True enables text smoothing. Effective on 4x4 size text and larger, *default*: False
|
|
|
|
:param flip: True enables upside-down printing, *default*: False
|
2016-02-29 18:04:00 +00:00
|
|
|
:type invert: bool
|
2015-11-27 22:10:20 +00:00
|
|
|
"""
|
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)
|
2016-03-01 18:05:52 +00:00
|
|
|
elif height == 2 and width == 1:
|
2013-05-30 15:47:30 +00:00
|
|
|
self._raw(TXT_NORMAL)
|
|
|
|
self._raw(TXT_2HEIGHT)
|
2016-03-01 18:05:52 +00:00
|
|
|
elif width == 2 and height == 1:
|
2014-05-21 05:15:54 +00:00
|
|
|
self._raw(TXT_NORMAL)
|
2013-05-30 15:47:30 +00:00
|
|
|
self._raw(TXT_2WIDTH)
|
2016-03-01 18:05:52 +00:00
|
|
|
elif width == 1 and height == 1:
|
2014-05-21 05:15:54 +00:00
|
|
|
self._raw(TXT_NORMAL)
|
2016-03-01 18:05:52 +00:00
|
|
|
else:
|
|
|
|
self._raw(TXT_SIZE + chr(TXT_WIDTH[width] + TXT_HEIGHT[height]))
|
2010-02-26 08:54:46 +00:00
|
|
|
# Type
|
2016-03-01 18:05:52 +00:00
|
|
|
if flip == True:
|
|
|
|
self._raw(TXT_FLIP_ON)
|
|
|
|
else:
|
|
|
|
self._raw(TXT_FLIP_OFF)
|
|
|
|
if smooth == True:
|
|
|
|
self._raw(TXT_SMOOTH_ON)
|
|
|
|
else:
|
|
|
|
self._raw(TXT_SMOOTH_OFF)
|
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
|
2016-02-29 18:04:00 +00:00
|
|
|
# Invert Printing
|
|
|
|
if invert:
|
|
|
|
self._raw(TXT_INVERT_ON)
|
|
|
|
else:
|
|
|
|
self._raw(TXT_INVERT_OFF)
|
2010-02-26 08:54:46 +00:00
|
|
|
|
|
|
|
def cut(self, mode=''):
|
2016-01-13 15:06:18 +00:00
|
|
|
""" Cut paper.
|
|
|
|
|
|
|
|
Without any arguments the paper will be cut completely. With 'mode=PART' a partial cut will
|
|
|
|
be attempted. Note however, that not all models can do a partial cut. See the documentation of
|
|
|
|
your printer for details.
|
2016-01-14 00:00:53 +00:00
|
|
|
.. todo:: Check this function on TM-T88II.
|
2015-11-27 22:10:20 +00:00
|
|
|
|
|
|
|
:param mode: set to 'PART' for a partial cut
|
|
|
|
"""
|
2010-02-26 08:54:46 +00:00
|
|
|
# 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):
|
2015-11-27 22:10:20 +00:00
|
|
|
""" Send pulse to kick the cash drawer
|
|
|
|
|
2016-01-05 16:30:40 +00:00
|
|
|
Kick cash drawer on pin 2 or pin 5 according to parameter.
|
|
|
|
|
|
|
|
:param pin: pin number, 2 or 5
|
|
|
|
:raises: :py:exc:`~escpos.exceptions.CashDrawerError`
|
2015-11-27 22:10:20 +00:00
|
|
|
"""
|
2010-02-26 08:54:46 +00:00
|
|
|
if pin == 2:
|
|
|
|
self._raw(CD_KICK_2)
|
|
|
|
elif pin == 5:
|
|
|
|
self._raw(CD_KICK_5)
|
|
|
|
else:
|
|
|
|
raise CashDrawerError()
|
|
|
|
|
|
|
|
def hw(self, hw):
|
2015-11-27 22:10:20 +00:00
|
|
|
""" Hardware operations
|
|
|
|
|
2016-01-05 16:30:40 +00:00
|
|
|
:param hw: hardware action, may be:
|
|
|
|
|
|
|
|
* INIT
|
|
|
|
* SELECT
|
|
|
|
* RESET
|
2015-11-27 22:10:20 +00:00
|
|
|
"""
|
2010-02-26 08:54:46 +00:00
|
|
|
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):
|
2015-11-27 22:10:20 +00:00
|
|
|
""" Feed control sequences
|
|
|
|
|
2016-01-05 16:30:40 +00:00
|
|
|
:param ctl: string for the following control sequences:
|
|
|
|
|
|
|
|
* LF *for Line Feed*
|
|
|
|
* FF *for Form Feed*
|
|
|
|
* CR *for Carriage Return*
|
|
|
|
* HT *for Horizontal Tab*
|
|
|
|
* VT *for Vertical Tab*
|
|
|
|
|
|
|
|
:param pos: integer between 1 and 16, controls the horizontal tab position
|
|
|
|
:raises: :py:exc:`~escpos.exceptions.TabPosError`
|
2015-11-27 22:10:20 +00:00
|
|
|
"""
|
2014-05-21 05:15:54 +00:00
|
|
|
# Set tab positions
|
|
|
|
if pos < 1 or pos > 16:
|
2015-11-27 22:10:20 +00:00
|
|
|
raise TabPosError()
|
2014-05-21 05:15:54 +00:00
|
|
|
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)
|