Printing Barcodes

Last Reviewed:

2023-08-10

Many printers implement barcode printing natively. These hardware rendered barcodes are fast but the supported formats are limited by the printer itself and different between models. However, almost all printers support printing images, so barcode rendering can be performed externally by software and then sent to the printer as an image. As a drawback, this operation is much slower and the user needs to know and choose the image implementation method supported by the printer’s command-set.

barcode-method

Since version 3.0, the barcode method unifies the previous barcode (hardware) and soft_barcode (software) methods. It is able to choose automatically the best printer implementation for barcode printing based on the capabilities of the printer and the type of barcode desired. To achieve this, it relies on the information contained in the escpos-printer-db profiles. The chosen profile needs to match the capabilities of the printer as closely as possible.

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

Print barcode.

Automatic hardware|software barcode renderer according to the printer capabilities.

Defaults to hardware barcode and its format types if supported. Automatically switches to software barcode renderer if hardware does not support a barcode type that is supported by software. (e.g. JAN, ISSN, etc.).

Set force_software=True to force the software renderer according to the profile. Set force_software=graphics|bitImageColumn|bitImageRaster to specify a renderer.

Ignores caps, special chars and whitespaces in barcode type names. So “EAN13”, “ean-13”, “Ean_13”, “EAN 13” are all accepted.

Parameters:
  • code – alphanumeric data to be printed as bar code (payload).

  • bc – barcode format type (EAN13, CODE128, JAN, etc.).

  • height (int) – barcode module height (in printer dots), has to be between 1 and 255. default: 64

  • width (int) – barcode module width (in printer dots), has to be between 2 and 6. default: 3

  • pos (str) – text position (ABOVE, BELOW, BOTH, OFF) relative to the barcode (ignored in software renderer). default: BELOW

  • font (str) – select font A or B (ignored in software renderer). default: A

  • align_ct (bool) – If True, center the barcode. default: True

  • function_type – ESCPOS function type A or B. None to guess it from profile (ignored in software renderer). default: None

  • check (bool) – If True, checks that the code meets the requirements of the barcode type. default: True

  • force_software (Union[bool, str]) – If True, force the use of software barcode renderer from profile. If “graphics”, “bitImageColumn” or “bitImageRaster”, force the use of specific renderer.

Raises:

BarcodeCodeError, BarcodeTypeError

Return type:

None

Note

Get all supported formats at:

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.