Printing Barcodes

Last Reviewed

2016-07-31

Most ESC/POS-printers implement barcode-printing. The barcode-commandset is implemented in the barcode-method. For a list of compatible barcodes you should check the manual of your printer. As a rule of thumb: even older Epson-models support most 1D-barcodes. To be sure just try some implementations and have a look at the notices below.

barcode-method

The barcode-method is rather low-level and orients itself on the implementation of ESC/POS. In the future this class could be supplemented by a high-level class that helps the user generating the payload.

Escpos.barcode(code, bc, height=64, width=3, pos='BELOW', font='A', align_ct=True, function_type=None, check=True)[source]

Print Barcode

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. By default, this method will check whether your barcode text is correct, that is the characters and lengths are supported by ESCPOS. Call the method with check=False to disable the check, but note that uncorrect 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.

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.)

Todo

On TM-T88II width from 1 to 6 is accepted. Try to acquire command reference and correct the code.

Todo

Supplying pos does not have an effect for every barcode type. Check and document for which types this is true.

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.)

Parameters
  • code – alphanumeric data to be printed as bar code

  • bc

    barcode format, possible values are for type A are:

    • UPC-A

    • UPC-E

    • EAN13

    • EAN8

    • CODE39

    • ITF

    • NW7

    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

    If none is specified, the method raises BarcodeTypeError.

  • height (int) – barcode height, has to be between 1 and 255 default: 64

  • width (int) – barcode width, has to be between 2 and 6 default: 3

  • pos

    where to place the text relative to the barcode, default: BELOW

    • ABOVE

    • BELOW

    • BOTH

    • OFF

  • font

    select font (see ESC/POS-documentation, the device often has two fonts), default: A

    • A

    • B

  • align_ct (bool) – If this parameter is True the barcode will be centered. Otherwise no alignment command will be issued.

  • function_type – Choose between ESCPOS function type A or B, depending on printer support and desired barcode. If not given, the printer will attempt to automatically choose the correct function based on the current profile. default: A

  • check – If this parameter is True, the barcode format will be checked to ensure it meets the bc requirements as definged in the ESC/POS documentation. See check_barcode() for more information. default: True.

Raises

BarcodeSizeError, BarcodeTypeError, BarcodeCodeError

CODE128

Code128 barcodes need a certain format. For now the user has to make sure that the payload is correct. For alphanumeric CODE128 you have to preface your payload with {B.

from escpos.printer import Dummy, Serial
p = Serial()
# print CODE128 012ABCDabcd
p.barcode("{B012ABCDabcd", "CODE128", function_type="B")

A very good description on CODE128 is also on Wikipedia.