Skip to main content

ZPL how-to

Center or right-align a barcode in ZPL

^FB centers text, not barcodes. To right-align a barcode, give ^FO its right edge and a 1. To center one, work out how wide it prints and start it half the leftover space in.

^FB does not move a barcode

zpl
^XA
^FO0,40^FB812,1,0,C^BY2^BCN,100,Y,N,N^FDHB-M8-40-Z^FS
^XZ

In Labelary the bars printed from x 0, at the left edge, as if the ^FB were not there. A field block lays out text; a barcode keeps its own width. Center text in ZPL with ^FB covers text.

Right-align: the third value of ^FO

zpl
^XA
^BY2^FO772,40,1^BCN,100,Y,N,N^FDHB-M8-40-Z^FS
^XZ
  • ^FO772,40,1: x is now the right edge, and 1 means right-aligned. The bars ended at x 771, 40 dots in from the edge of an 812-dot label, whatever the data's length.
  • ^FT takes the same third value, and printed in the same place.
  • Zebra's reference says only firmware V60.14.x, V50.14.x or later supports it. On an older printer, work out the x as for centering, below, with the space on the left instead of halved.
  • The other value, 2, is automatic: it follows the script's direction, for right-to-left text. Use 0 or 1.

Center: work out the x

A Code 128 barcode is 145 modules wide for HB-M8-40-Z's 10 characters, and each module is as wide as ^BY says: 290 dots at ^BY2. Take that from the label's width and halve it: (812 − 290) ÷ 2 = 261.

zpl
^XA
^BY2^FO261,40^BCN,100,Y,N,N^FDHB-M8-40-Z^FS
^XZ
  • In Labelary the bars had 261 dots on the left and 261 on the right, and the printer centered the text under them on the bars: it ran from x 347 to 463.
  • At ^BY3 the same code is 435 dots wide, so x 188: it printed with 188 dots on the left and 189 on the right.
  • Code 128 barcodes in ZPL explains counting the characters. The samples here have no A at the end of ^BC, so they stay in subset B and every character, digit or not, counts as one. In automatic mode (^BCN,100,Y,N,N,A) a run of digits packs two to a character, and the barcode is narrower. For other barcodes, measure the width once on a preview.

The same barcode 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 "align": "center"
14 }
15 ]
16}

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^FO261,40^BCN,,N,N,N,A^FDHB-M8-40-Z^FS
^XZ

Set a BarCode element's align to "center" or "right", and zeepler works out the width as above. Code 128 is counted as the printer's automatic mode packs the value, with runs of 4 or more digits in subset C. Code 93 is counted with its full-ASCII pairs, and EAN-13 is 285 dots. This request centers HB-M8-40-Z: zeepler sent it at x 261, and the bars printed with 261 dots on the left and 261 on the right.

  • "center" does not use the x in coords. "right" ends the bars x dots from the label's right edge: at x 40, this barcode would print where the ^FO772,40,1 sample did.
  • A barcode wider than the space starts at x 0.
  • An EAN-13's first digit prints 34 dots left of its bars, and a scanner needs 21 blank dots right of them, so with "right" keep x at least 21.

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

  • 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. Print one label, measure it, and adjust: there is no preview.
  • Scan every barcode and QR code on a printed label before relying on it.

Related guides

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.