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
^XA
^FO0,40^FB812,1,0,C^BY2^BCN,100,Y,N,N^FDHB-M8-40-Z^FS
^XZIn 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
^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.^FTtakes 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.
^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
^BY3the 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.
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:
^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
^XZSet 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 incoords."right"ends the bars x dots from the label's right edge: at x 40, this barcode would print where the^FO772,40,1sample 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
200with the ZPL as plain text. A request the API cannot turn into a label answers400with 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
maxLengthon 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
- 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.
- Code 128 barcodes in ZPL: Set a Code 128 barcode's width and height with ^BY and ^BC, work out its width in dots, and pick subsets A, B and C.
- 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.
- 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.
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.