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=u'BELOW', font=u'A', align_ct=True, function_type=u'A')

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. Currently you have to check manually whether your barcode text is correct. 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.

Todo

Add a method to check barcode codes. Alternatively or as an addition write explanations about each 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.)

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. default: A
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.