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
|
|
|
|
2016-03-02 23:47:30 +00:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
2016-03-03 03:21:35 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-03-14 08:31:07 +00:00
|
|
|
import qrcode
|
2015-06-04 11:57:59 +00:00
|
|
|
import textwrap
|
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-04-03 07:36:54 +00:00
|
|
|
from escpos.image import EscposImage
|
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
|
2016-03-03 02:37:43 +00:00
|
|
|
:type msg: bytes
|
2015-11-27 22:10:20 +00:00
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2016-04-13 11:27:51 +00:00
|
|
|
def image(self, img_source, high_density_vertical=True, high_density_horizontal=True, impl="bitImageRaster"):
|
2016-04-03 07:36:54 +00:00
|
|
|
""" Print an image
|
|
|
|
|
|
|
|
:param img_source: PIL image or filename to load: `jpg`, `gif`, `png` or `bmp`
|
|
|
|
|
|
|
|
"""
|
|
|
|
im = EscposImage(img_source)
|
|
|
|
|
|
|
|
if impl == "bitImageRaster":
|
|
|
|
# GS v 0, raster format bit image
|
|
|
|
density_byte = (0 if high_density_vertical else 1) + (0 if high_density_horizontal else 2)
|
2016-04-07 12:06:14 +00:00
|
|
|
header = GS + b"v0" + six.int2byte(density_byte) + self._int_low_high(im.width_bytes, 2) + self._int_low_high(im.height, 2)
|
2016-04-03 07:36:54 +00:00
|
|
|
self._raw(header + im.to_raster_format())
|
|
|
|
|
|
|
|
if impl == "graphics":
|
|
|
|
# GS ( L raster format graphics
|
2016-04-07 12:06:14 +00:00
|
|
|
img_header = self._int_low_high(im.width, 2) + self._int_low_high(im.height, 2)
|
|
|
|
tone = b'0'
|
|
|
|
colors = b'1'
|
2016-04-03 07:36:54 +00:00
|
|
|
ym = six.int2byte(1 if high_density_vertical else 2)
|
|
|
|
xm = six.int2byte(1 if high_density_horizontal else 2)
|
|
|
|
header = tone + xm + ym + colors + img_header
|
|
|
|
raster_data = im.to_raster_format()
|
2016-04-07 12:06:14 +00:00
|
|
|
self._image_send_graphics_data(b'0', b'p', header + raster_data)
|
|
|
|
self._image_send_graphics_data(b'0', b'2', b'')
|
2016-04-03 07:36:54 +00:00
|
|
|
|
|
|
|
if impl == "bitImageColumn":
|
|
|
|
# ESC *, column format bit image
|
2016-04-07 12:06:14 +00:00
|
|
|
density_byte = (1 if high_density_horizontal else 0) + (32 if high_density_vertical else 0)
|
2016-04-13 11:27:51 +00:00
|
|
|
header = ESC + b"*" + six.int2byte(density_byte) + self._int_low_high(im.width, 2)
|
|
|
|
outp = [ESC + b"3" + six.int2byte(16)] # Adjust line-feed size
|
2016-04-03 07:36:54 +00:00
|
|
|
for blob in im.to_column_format(high_density_vertical):
|
|
|
|
outp.append(header + blob + b"\n")
|
2016-04-13 11:27:51 +00:00
|
|
|
outp.append(ESC + b"2") # Reset line-feed size
|
2016-04-03 07:36:54 +00:00
|
|
|
self._raw(b''.join(outp))
|
|
|
|
|
|
|
|
def _image_send_graphics_data(self, m, fn, data):
|
2015-11-27 22:10:20 +00:00
|
|
|
"""
|
2016-04-03 07:36:54 +00:00
|
|
|
Wrapper for GS ( L, to calculate and send correct data length.
|
|
|
|
|
|
|
|
:param m: Modifier//variant for function. Usually '0'
|
|
|
|
:param fn: Function number to use, as byte
|
|
|
|
:param data: Data to send
|
|
|
|
"""
|
2016-04-07 12:06:14 +00:00
|
|
|
header = self._int_low_high(len(data) + 2, 2)
|
2016-04-03 07:36:54 +00:00
|
|
|
self._raw(GS + b'(L' + header + m + fn + data)
|
2015-06-10 01:21:52 +00:00
|
|
|
|
2016-03-19 13:48:29 +00:00
|
|
|
def qr(self, content, ec=QR_ECLEVEL_L, size=3, model=QR_MODEL_2, native=False):
|
2015-11-27 22:10:20 +00:00
|
|
|
""" Print QR Code for the provided string
|
2013-03-14 08:31:07 +00:00
|
|
|
|
2016-03-19 13:48:29 +00:00
|
|
|
:param content: The content of the code. Numeric data will be more efficiently compacted.
|
|
|
|
:param ec: Error-correction level to use. One of QR_ECLEVEL_L (default), QR_ECLEVEL_M, QR_ECLEVEL_Q or QR_ECLEVEL_H.
|
|
|
|
Higher error correction results in a less compact code.
|
|
|
|
:param size: Pixel size to use. Must be 1-16 (default 3)
|
|
|
|
:param model: QR code model to use. Must be one of QR_MODEL_1, QR_MODEL_2 (default) or QR_MICRO (not supported by all printers).
|
|
|
|
:param native: True to render the code on the printer, False to render the code as an image and send it to the printer (Default)
|
2015-11-27 22:10:20 +00:00
|
|
|
"""
|
2016-03-19 13:48:29 +00:00
|
|
|
# Basic validation
|
|
|
|
if ec not in [QR_ECLEVEL_L, QR_ECLEVEL_M, QR_ECLEVEL_H, QR_ECLEVEL_Q]:
|
|
|
|
raise ValueError("Invalid error correction level")
|
|
|
|
if not 1 <= size <= 16:
|
|
|
|
raise ValueError("Invalid block size (must be 1-16)")
|
|
|
|
if model not in [QR_MODEL_1, QR_MODEL_2, QR_MICRO]:
|
|
|
|
raise ValueError("Invalid QR model (must be one of QR_MODEL_1, QR_MODEL_2, QR_MICRO)")
|
|
|
|
if content == "":
|
|
|
|
# Handle edge case by printing nothing.
|
|
|
|
return
|
|
|
|
if not native:
|
|
|
|
# Map ESC/POS error correction levels to python 'qrcode' library constant and render to an image
|
|
|
|
if model != QR_MODEL_2:
|
2016-04-03 07:36:54 +00:00
|
|
|
raise ValueError("Invalid QR model for qrlib rendering (must be QR_MODEL_2)")
|
2016-03-19 13:48:29 +00:00
|
|
|
python_qr_ec = {
|
|
|
|
QR_ECLEVEL_H: qrcode.constants.ERROR_CORRECT_H,
|
|
|
|
QR_ECLEVEL_L: qrcode.constants.ERROR_CORRECT_L,
|
|
|
|
QR_ECLEVEL_M: qrcode.constants.ERROR_CORRECT_M,
|
|
|
|
QR_ECLEVEL_Q: qrcode.constants.ERROR_CORRECT_Q
|
|
|
|
}
|
|
|
|
qr_code = qrcode.QRCode(version=None, box_size=size, border=1, error_correction=python_qr_ec[ec])
|
|
|
|
qr_code.add_data(content)
|
|
|
|
qr_code.make(fit=True)
|
|
|
|
qr_img = qr_code.make_image()
|
|
|
|
im = qr_img._img.convert("RGB")
|
|
|
|
# Convert the RGB image in printable image
|
2016-04-03 07:36:54 +00:00
|
|
|
self.image(im)
|
2016-03-19 13:48:29 +00:00
|
|
|
return
|
|
|
|
# Native 2D code printing
|
2016-04-13 11:27:51 +00:00
|
|
|
cn = b'1' # Code type for QR code
|
2016-03-19 13:48:29 +00:00
|
|
|
# Select model: 1, 2 or micro.
|
2016-04-07 12:06:14 +00:00
|
|
|
self._send_2d_code_data(six.int2byte(65), cn, six.int2byte(48 + model) + six.int2byte(0))
|
2016-03-19 13:48:29 +00:00
|
|
|
# Set dot size.
|
2016-04-07 12:06:14 +00:00
|
|
|
self._send_2d_code_data(six.int2byte(67), cn, six.int2byte(size))
|
2016-03-19 13:48:29 +00:00
|
|
|
# Set error correction level: L, M, Q, or H
|
2016-04-07 12:06:14 +00:00
|
|
|
self._send_2d_code_data(six.int2byte(69), cn, six.int2byte(48 + ec))
|
2016-03-19 13:48:29 +00:00
|
|
|
# Send content & print
|
2016-04-07 12:06:14 +00:00
|
|
|
self._send_2d_code_data(six.int2byte(80), cn, content.encode('utf-8'), b'0')
|
|
|
|
self._send_2d_code_data(six.int2byte(81), cn, b'', b'0')
|
2016-03-19 13:48:29 +00:00
|
|
|
|
|
|
|
def _send_2d_code_data(self, fn, cn, data, m=b''):
|
|
|
|
""" Wrapper for GS ( k, to calculate and send correct data length.
|
|
|
|
|
|
|
|
:param fn: Function to use.
|
|
|
|
:param cn: Output code type. Affects available data.
|
|
|
|
:param data: Data to send.
|
|
|
|
:param m: Modifier/variant for function. Often '0' where used.
|
|
|
|
"""
|
|
|
|
if len(m) > 1 or len(cn) != 1 or len(fn) != 1:
|
|
|
|
raise ValueError("cn and fn must be one byte each.")
|
2016-04-07 12:06:14 +00:00
|
|
|
header = self._int_low_high(len(data) + len(m) + 2, 2)
|
2016-03-19 13:48:29 +00:00
|
|
|
self._raw(GS + b'(k' + header + cn + fn + m + data)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _int_low_high(inp_number, out_bytes):
|
|
|
|
""" Generate multiple bytes for a number: In lower and higher parts, or more parts as needed.
|
|
|
|
|
|
|
|
:param inp_number: Input number
|
|
|
|
:param out_bytes: The number of bytes to output (1 - 4).
|
|
|
|
"""
|
2016-04-07 12:06:14 +00:00
|
|
|
max_input = (256 << (out_bytes * 8) - 1)
|
2016-03-19 13:48:29 +00:00
|
|
|
if not 1 <= out_bytes <= 4:
|
|
|
|
raise ValueError("Can only output 1-4 byes")
|
|
|
|
if not 0 <= inp_number <= max_input:
|
|
|
|
raise ValueError("Number too large. Can only output up to {0} in {1} byes".format(max_input, out_bytes))
|
2016-04-07 12:06:14 +00:00
|
|
|
outp = b''
|
2016-03-19 13:48:29 +00:00
|
|
|
for _ in range(0, out_bytes):
|
|
|
|
outp += six.int2byte(inp_number % 256)
|
|
|
|
inp_number = inp_number // 256
|
|
|
|
return outp
|
2013-03-14 08:31:07 +00:00
|
|
|
|
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-03-07 04:06:05 +00:00
|
|
|
def barcode(self, code, bc, height=64, width=3, pos="BELOW", font="A", align_ct=True, function_type="A"):
|
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
|
2016-03-07 04:06:05 +00:00
|
|
|
barcodes may lead to unexpected printer behaviour. There are two forms of the barcode function. Type A is
|
|
|
|
default but has fewer barcodes, while type B has some more to choose from.
|
2016-01-14 14:50:58 +00:00
|
|
|
|
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
|
2016-03-07 04:06:05 +00:00
|
|
|
:param bc: barcode format, possible values are for type A are:
|
2016-01-05 16:30:40 +00:00
|
|
|
|
|
|
|
* UPC-A
|
|
|
|
* UPC-E
|
|
|
|
* EAN13
|
|
|
|
* EAN8
|
|
|
|
* CODE39
|
|
|
|
* ITF
|
|
|
|
* NW7
|
|
|
|
|
2016-03-07 04:06:05 +00:00
|
|
|
Possible values for type B:
|
|
|
|
|
|
|
|
* All types from function type A
|
|
|
|
* CODE93
|
|
|
|
* CODE128
|
|
|
|
* GS1-128
|
|
|
|
* GS1 DataBar Omnidirectional
|
|
|
|
* GS1 DataBar Truncated
|
|
|
|
* GS1 DataBar Limited
|
|
|
|
* GS1 DataBar Expanded
|
|
|
|
|
2016-01-05 16:30:40 +00:00
|
|
|
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-03-07 04:06:05 +00:00
|
|
|
:param function_type: Choose between ESCPOS function type A or B, depending on printer support and desired
|
|
|
|
barcode.
|
|
|
|
*default*: A
|
|
|
|
|
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:
|
2016-03-03 03:38:05 +00:00
|
|
|
self._raw(BARCODE_HEIGHT + six.int2byte(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:
|
2016-03-03 03:38:05 +00:00
|
|
|
self._raw(BARCODE_WIDTH + six.int2byte(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)
|
2016-03-07 04:06:05 +00:00
|
|
|
|
2016-03-08 17:17:47 +00:00
|
|
|
bc_types = BARCODE_TYPES[function_type.upper()]
|
2016-03-07 04:06:05 +00:00
|
|
|
if bc.upper() not in bc_types.keys():
|
|
|
|
# TODO: Raise a better error, or fix the message of this error type
|
|
|
|
raise BarcodeTypeError("Barcode type {bc} not valid for barcode function type {function_type}".format(
|
|
|
|
bc=bc,
|
|
|
|
function_type=function_type,
|
|
|
|
))
|
|
|
|
|
|
|
|
self._raw(bc_types[bc.upper()])
|
|
|
|
|
|
|
|
if function_type.upper() == "B":
|
2016-03-16 23:26:13 +00:00
|
|
|
self._raw(six.int2byte(len(code)))
|
2016-03-07 04:06:05 +00:00
|
|
|
|
2010-02-26 08:54:46 +00:00
|
|
|
# Print Code
|
|
|
|
if code:
|
2016-03-03 03:38:05 +00:00
|
|
|
self._raw(code.encode())
|
2010-02-26 08:54:46 +00:00
|
|
|
else:
|
2015-11-27 20:20:12 +00:00
|
|
|
raise BarcodeCodeError()
|
2015-06-04 11:57:59 +00:00
|
|
|
|
2016-03-08 00:20:04 +00:00
|
|
|
if function_type.upper() == "A":
|
2016-03-16 22:23:42 +00:00
|
|
|
self._raw(NUL)
|
2016-03-08 00:20:04 +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
|
|
|
|
2016-03-03 03:38:05 +00:00
|
|
|
.. todo:: rework this in order to proberly handle encoding
|
|
|
|
|
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:
|
2016-03-03 03:38:05 +00:00
|
|
|
self._raw(txt.encode())
|
2010-02-26 08:54:46 +00:00
|
|
|
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-04-02 13:26:00 +00:00
|
|
|
col_count = self.columns if columns is None else columns
|
|
|
|
self.text(textwrap.fill(txt, col_count))
|
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-02 00:34:21 +00:00
|
|
|
elif 1 <= width <= 8 and 1 <= height <= 8 and isinstance(width, int) and isinstance(height, int):
|
2016-03-03 03:21:35 +00:00
|
|
|
self._raw(TXT_SIZE + six.int2byte(TXT_WIDTH[width] + TXT_HEIGHT[height]))
|
2016-03-02 00:25:38 +00:00
|
|
|
else:
|
|
|
|
raise SetVariableError()
|
|
|
|
# Upside down
|
2016-03-01 18:05:52 +00:00
|
|
|
if flip == True:
|
|
|
|
self._raw(TXT_FLIP_ON)
|
|
|
|
else:
|
|
|
|
self._raw(TXT_FLIP_OFF)
|
2016-03-02 00:25:38 +00:00
|
|
|
# Smoothing
|
2016-03-01 18:05:52 +00:00
|
|
|
if smooth == True:
|
|
|
|
self._raw(TXT_SMOOTH_ON)
|
|
|
|
else:
|
|
|
|
self._raw(TXT_SMOOTH_OFF)
|
2016-03-02 00:25:38 +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
|
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-03-09 15:36:40 +00:00
|
|
|
|
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
|
2016-03-03 03:38:05 +00:00
|
|
|
self._raw(b"\n\n\n\n\n\n")
|
2010-02-26 08:54:46 +00:00
|
|
|
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
|
2016-04-02 14:02:04 +00:00
|
|
|
if not (1 <= pos <= 16):
|
2015-11-27 22:10:20 +00:00
|
|
|
raise TabPosError()
|
2014-05-21 05:15:54 +00:00
|
|
|
else:
|
2016-03-03 03:38:05 +00:00
|
|
|
self._raw(CTL_SET_HT + six.int2byte(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)
|
2016-03-10 15:01:35 +00:00
|
|
|
|
|
|
|
def panel_buttons(self, enable=True):
|
|
|
|
""" Controls the panel buttons on the printer (e.g. FEED)
|
|
|
|
|
|
|
|
When enable is set to False the panel buttons on the printer will be disabled. Calling the method with
|
|
|
|
enable=True or without argument will enable the panel buttons.
|
|
|
|
|
|
|
|
If panel buttons are enabled, the function of the panel button, such as feeding, will be executed upon pressing
|
|
|
|
the button. If the panel buttons are disabled, pressing them will not have any effect.
|
|
|
|
|
|
|
|
This command is effective until the printer is initialized, reset or power-cycled. The default is enabled panel
|
|
|
|
buttons.
|
|
|
|
|
|
|
|
Some panel buttons will always work, especially when printer is opened. See for more information the manual
|
|
|
|
of your printer and the escpos-command-reference.
|
|
|
|
|
|
|
|
:param enable: controls the panel buttons
|
|
|
|
:rtype: None
|
|
|
|
"""
|
|
|
|
if enable:
|
|
|
|
self._raw(PANEL_BUTTON_ON)
|
|
|
|
else:
|
|
|
|
self._raw(PANEL_BUTTON_OFF)
|