Skip to main content

ZPL how-to

Code 128 barcodes in ZPL

A Code 128 barcode in ZPL has no width setting. Its width comes from the module width in ^BY and from how many characters it encodes, and its height and the text under it are set in ^BC.

In plain ZPL

zpl
^XA
^BY3
^FO40,40^BCN,100,Y,N,N^FDHB-M8-40-Z^FS
^XZ
  • ^BY3 sets the module width, the narrowest bar, to 3 dots. It runs from 1 to 10 and stays set for every barcode after it, until the next ^BY. Its second value, the wide-to-narrow ratio, has no effect on Code 128.
  • ^BCN,100,Y,N,N: Code 128, normal orientation, 100 dots tall, with the human-readable text printed under the bars (Y, the default), not above them (N), and no UCC check digit (N). Leave the height out and it comes from the third value of ^BY, as in ^BY3,,100, which is 10 dots if nothing set it.

Working out the width

Every Code 128 character is 11 modules wide. A barcode is a start character, the data, a check character and a 13-module stop: 11 × (characters + 2) + 13 modules. Multiply by the module width to get dots.

  • HB-M8-40-Z is 10 characters, so 145 modules: 290 dots wide at ^BY2 and 435 at ^BY3, about 2.1 inches at 203 dpi.
  • Leave at least 10 modules of blank space on each side, so a scanner can find where the barcode starts and ends.
  • A 4 inch label is 812 dots wide at 203 dpi. A long value at ^BY3 runs off the edge, and the printer prints what fits, with no error. Lower the module width before you shorten the data.

Subsets A, B and C

Code 128 has three character sets. Subset A has capitals, digits and control characters, subset B has capitals, lower case and digits, and subset C packs two digits into each character, so a run of digits takes half the width.

zpl
^XA
^BY2
^FO40,40^BCN,100,N,N,N,A^FD12345678^FS
^XZ
  • The A at the end of ^BC is automatic mode: the printer picks the subsets, and switches to C for runs of digits. These 8 digits print 158 dots wide at ^BY2.
  • Without it, the barcode starts in subset B and stays there: the same 8 digits print 246 dots wide.
  • To choose a subset yourself, leave the A off ^BC and start the field data with >9 for A, >: for B or >; for C. In automatic mode they are not start codes: they print as characters.
  • Give subset C an even number of digits. With an odd number, the last digit can be dropped, with no error.
  • Without the A, a > in your data starts an invocation code. Write it as >< to print a >. Automatic mode prints it as it is.

The same label with zeepler

Send this body to POST https://api.zeepler.com/v1/zeeplify with your API key in the x-api-key header. It is a working request, not an illustration: on every change, our tests run this same file through the API's code and compare the result with the ZPL below.

json
1{
2 "config": {
3 "size": "4x2"
4 },
5 "data": {
6 "sku": "HB-M8-40-Z"
7 },
8 "templates": [
9 {
10 "type": "BarCode",
11 "dataPath": "sku",
12 "coords": [40, 40]
13 },
14 {
15 "type": "Text",
16 "dataPath": "sku",
17 "coords": [40, 110],
18 "font": "0,30"
19 }
20 ]
21}

And the ZPL that comes back, ready to send to the printer:

zpl
^XA^MCY^XZ
    ^XA
    ^SZ2
    ^MMT
    ^MNY
    ^LL406
    ^PW812
    ^LH0,0
^BY2,2,59^FO40,40^BCN,,N,N,N,A^FDHB-M8-40-Z^FS
^CF0,30^FO40,110^FDHB-M8-40-Z^FS
^XZ

zeepler's BarCode element is Code 128 in automatic mode, so runs of digits are packed into subset C for you. Its module width is fixed at 2 dots, and its height is 59 unless you set height in dots. It prints no text under the bars: the request adds a Text element with the same value instead. If you need a wider barcode or the printer's own text line, use the plain ZPL above.

A ^, ~ or _ in the value is escaped for you, as the guide on printing them explains.

Sending it

  • Call the API from your own server or integration engine, not from a web page. An API key in a browser is a key anyone can copy.
  • A successful call answers 200 with the ZPL as plain text. A request the API cannot turn into a label answers 400 with every problem listed by field, and is not billed.
  • Send the ZPL to a Zebra or other ZPL printer at 203 dpi. At 300 dpi the same label prints at two-thirds of the size.

The API reference has every field and status code.

Before you go live

  • Scan the barcode with your own scanners, at the module width you plan to use.
  • Text does not wrap. A value longer than the space you left runs off the edge of the label, so set maxLength on any field whose length you do not control.
  • Coordinates are in dots from the top-left corner. A preview shows the layout; then print one label, measure it, and adjust, because a preview cannot show what your printer does with it.
  • Scan every barcode and QR code on a printed label before relying on it.

Related guides

  • QR code size in ZPL: How ^BQ magnification sets the size of a QR code, and how to work out the size in dots.
  • Printing ^, ~ and _ in ZPL: Why a caret or tilde in your data cuts the field short, and how ^FH prints them.
  • Font size in ZPL: Setting text height with ^A and ^CF, converting points to dots, and why ^CF carries over.
  • Send ZPL to a Zebra printer from C#: Send ZPL from .NET to a network printer on port 9100, or to a USB printer through the Windows spooler, without the driver redrawing it.
  • Accented characters in ZPL (^CI28): Print é, ç, ô and € with ^CI28, switch the printer back with ^CI0, and what to do on older firmware.
  • Send ZPL to a Zebra printer from Python: Send ZPL from Python to a network printer on port 9100, or to a USB printer through the Windows spooler, without the driver redrawing it.
  • Change a barcode to a QR code in ZPL: Swap ^BC for ^BQ, start the field data with QA, so the first characters scan, and reset the ^BY left over from the barcode.
  • Send ZPL to a Zebra printer from Node.js: Send ZPL from Node.js to a network printer on port 9100 or to a USB printer through a Windows printer share, and print from a web page through your server.
  • Label size and origin in ZPL (^PW, ^LL, ^LH): Set the label size in dots with ^PW and ^LL, move 0,0 with ^LH, and stop the last label's settings moving this one.
  • Templates in ZPL: store a format with ^DF, fill it with ^XF: Store a label layout on the printer with ^DF and ^FN, fill it with ^XF, and what prints when a field or the format is missing.
  • Several labels in one ZPL job: Send several different labels in one job, stop each one inheriting the last one's font and barcode settings, and number a run with ^SN and ^PQ.
  • EAN-13 and Code 93 barcodes in ZPL: Print EAN-13 with ^BE and Code 93 with ^BA, and the data each one changes: a replaced check digit, and characters Code 93 cannot hold.
  • Print copies of a ZPL label with ^PQ: Print several copies of one label with ^PQ, why ^PQ after ^XZ prints only one, and each serial number twice with ^SN.
  • Center text in ZPL with ^FB: Center text of any length with ^FB, why centered text sits a little left, and text too long for the block.
  • Center or right-align a barcode in ZPL: Right-align a barcode with the third value of ^FO, center one by working out its width, and why ^FB does not move it.
  • Wrap text and new lines in ZPL: Wrap long text with ^FB, start a new line with \&, and stop ^FB writing over its last line or cutting text off.
  • Rotate text and barcodes in ZPL: Turn text and barcodes with N, R, I and B, where ^FO and ^FT put a turned field, ^FW and ^POI, and why both stay set.
  • Preview ZPL without a printer: See a label before printing it with Labelary's viewer or API, what zeepler's playground shows, and what no preview can tell you.
  • Boxes, lines, underline and bold in ZPL (^GB): Draw boxes and lines with ^GB, print white on black with ^FR, underline text with a line, and make text bolder.
  • Logos and images in ZPL (^GF, ~DG, ^XG): Print a logo with ^GF, turn a PNG into hex, keep it under the size limit, and store it once with ~DG to print with ^XG.
  • GS1-128 barcodes in ZPL, with the check digit: Print GS1-128 with ^BC and FNC1, work out the mod 10 check digit of a GTIN or SSCC, separate fields that vary in length, and what the printer adds itself.
  • 300 dpi printers, and the same ZPL on 203 and 300 dpi: Why a 203 dpi label prints smaller at 300 dpi, scaling it by 1.5 and what does not scale exactly, ^MU in millimetres, and reading the printer's resolution.
  • Comments in ZPL: ^FX: Write a comment with ^FX, why a caret inside one runs as a command, comments outside ^XA and ^XZ, and why // is not a comment.
  • Emulate a Zebra printer for testing: A stand-in printer on port 9100 in a few lines of Python or Node.js, pointing your send code at it, checking what it caught in Labelary, and what a stand-in cannot test.
  • Convert ZPL to PDF or PNG: Labelary's API turns ZPL into a PDF, one label to a page, or a PNG of one label: curl, Python and Node.js, its limits, and what it means to send it your labels.
  • Convert a PDF to ZPL: Draw each page of the PDF as dots at the printer's resolution and send them as ^GF: a Python script, sizing, grey, the ^GF size limit, and why the label prints as a picture.
  • Convert EPL to ZPL: Translate an EPL2 label to ZPL by hand: each common EPL command and its ZPL equivalent, one label translated line by line, what does not carry over, and switching a printer's language.
  • Convert HTML to ZPL: Two ways from an HTML label to ZPL: draw the page in a headless browser and send the dots as ^GF (a Node.js script), or write the label as ZPL fields, with which HTML and CSS ideas carry over and which do not.

Help with an integration

Email info@zeepler.com. You will get a reply within 2 business days. The support policy covers response times and version changes.