Skip to content
Daniel Gonçalves edited this page Jul 8, 2020 · 4 revisions

In PyESCPOS it is expected that a printer implementation is a subclass of escpos.impl.epson.GenericESCPOS that holds the public interface, that is, all the printing methods which will eventually be called by the client application code.

Instantiating a Printer Model

Suppose you have a Epson TM-T20 printer, connected to your computer's serial port. First you create a connection and then pass that connection to the constructor method of your printer model.

from escpos.conn.serial import SerialConnection
from escpos.impl.epson import TMT20

# create a serial connection on COM1
conn = SerialConnection.create('COM1:9600,8,1,N,RTSCTS')

# constructs an instance for Epson TM-T20 implementation
p = TMT20(conn)
p.init()
p.text('Hello ESC/POS!')

Print Methods

These are all "public" print methods (p is the printer implementation instance).

Method Description
p.init() Initialize printer. The exact effect depends on the printer model.
p.lf(lines=1) Line feed. The lines arguments tells how many line feed should be sent. Default is 1.
p.textout(text) Write text without issuing a line feed. The text will be first encoded to the encoding specified when the printer is instantiated, which defaults to UTF-8.
p.text(text) Write text followed by a line feed.
p.text_center(text) Shortcut method for print centered text.
p.justify_left() Set left justification. All subsequent text should be left aligned.
p.justify_center() Set centered justification. All subsequent text should be centered.
p.justify_right() Set right justification. All subsequent text should be right aligned.
p.set_code_page(n) Set printer's code page.
p.set_font(font) Set font to one of available fonts (see escpos.impl.epson.AVAILABLE_FONTS).
p.set_mode(font=FONT_A, emphasized=False, underline=False, expanded=False) Set various font attributes at once. See set_font().
p.set_text_size(width, height) Set font size to width and height. These arguments should be integers ranging from 0 to 7 whose meaning is the magnification of the text in either direction (0 for 1x -- normal text, 1 for 2x -- width and/or height, and so on until 7 for maximum 8x magnification).
p.set_expanded(flag) Switch expanded text mode. Argument flag must be a boolean (True on, False off).
p.set_condensed(flag) Switch condensed text mode. Argument flag must be a boolean (True on, False off).
p.set_emphasized(flag) Switch emphasized text mode. Argument flag must be a boolean (True on, False off).
p.set_double_strike(flag) Switch double strike mode. Argument flag must be a boolean (True on, False off).
p.cut(partial=True, feed=0) Trigger printer cutter to perform a partial (default) or full paper cut. Argument feed must be a value from 0 to 255 (inclusive).
p.kick_drawer(port=0, **kwargs) Kick drawer connected to the given port. Refer to Dealing with Cash Drawers below for more details.
p.ean8(data, **kwargs) Print given data rendered as JAN-8/EAN-8 barcode symbology.
p.ean13(data, **kwargs) Print given data rendered as JAN-13/EAN-13 barcode symbology.
p.code128(data, **kwargs) Print given data rendered as Code 128 barcode symbology. You can pass a keyword argument named codeset for the Code 128 subtype, which defaults to escpos.barcode.CODE128_A.
p.qrcode(data, **kwargs) Print given data rendered as QRCode. Refer to Printing QRCodes below for more details.

Dealing with Cash Drawers

Cash drawers are identified according to the port in which they are connected. This relation between drawers and ports does not exists in the ESC/POS specification and it was just a design decision to normalize cash drawers handling. From the client application perspective, drawers are simply connected to ports 0, 1, 2, and so on.

If printer physically does not have this feature (does not have cash drawer ports) then no exception should be raised, so it is safe to call this method without any previous checks. On the other hand, if printer does have this feature but client application pass in an invalid port number, an escpos.exceptions.CashDrawerException will be raised.

Printing QRCodes

The qrcode() method also accepts the following keyword arguments:

  • Keyword argument qrcode_ecc_level sets the error correction level. Defaults to escpos.barcode.QRCODE_ERROR_CORRECTION_L.
  • Keyword argument qrcode_module_size sets the module size. Defaults to escpos.barcode.QRCODE_MODULE_SIZE_4.

For example:

from escpos import barcode
from escpos.conn.network import NetworkConnection
from escpos.impl.epson import TMT20

p = TMT20(NetworkConnection.create('192.168.1.100:9100'))
p.qrcode(
        'https://github.com/base4sistemas',
        qrcode_ecc_level=barcode.QRCODE_ERROR_CORRECTION_M,
        qrcode_module_size=barcode.QRCODE_MODULE_SIZE_8
    )

Clone this wiki locally