ZPL how-to
Center text in ZPL with ^FB
ZPL places text from its left edge, so text of changing length cannot be centered by picking an x. ^FB makes a block as wide as the label and centers the text inside it, whatever its length.
Center with ^FB
^XA
^CF0,50
^FO0,40^FB812,1,0,C^FDWinter jacket, size M\&^FS
^CF0,100
^FO0,110^FB812,1,0,C^FD$4.99\&^FS
^XZ^FO0,40starts the block at the label's left edge, and^FB812,1,0,Cmakes it 812 dots wide, the whole of a 4-inch label at 203 dpi, one line tall, with no extra line spacing, and centered.\&at the end of the data is a line break. It is there for the reason in the next section.- In Labelary, both lines were centered whatever their length: the item had 195 dots on the left and 196 on the right, and the price 298 dots on the left and 298 on the right.
LandRin place ofCalign to the block's left or right edge.Jjustifies every line but the last.^FTworks too. Zebra's reference puts its y at the baseline of the block's last line, and the block grows upward from there. In Labelary this printed across exactly as the item above, with 195 dots on the left and 196 on the right:zpl^XA ^CF0,50 ^FT0,85^FB812,1,0,C^FDWinter jacket, size M\&^FS ^XZ
Why centered text sits to the left
The same label without the two \&:
^XA
^CF0,50
^FO0,40^FB812,1,0,C^FDWinter jacket, size M^FS
^CF0,100
^FO0,110^FB812,1,0,C^FD$4.99^FS
^XZIn Labelary the item then had 187 dots on the left and 204 on the right, and the price 284 dots on the left and 313 on the right: 9 dots left of center at 50 dots tall, and 15 at 100. Ending the data with \& centered it again. Lines that ^FB wraps by itself are off in the same way (below), so if the last few dots matter, break long text with \& where you want it:
^XA
^CF0,50
^FO0,40^FB812,2,0,C^FDWinter jacket with a detachable hood,\&size M\&^FS
^XZIn Labelary its first line had 26 dots on the left and 28 on the right, and the second 343 dots on the left and 344 on the right.
This was checked in Labelary only. A printer may differ, so print one label and measure before relying on it.
What goes wrong
- The block starts where the text did. Changing only the
^FOx from an old layout, such as^FO40with an 812-dot block, moved the text 40 dots right of center in Labelary, and the block runs 40 dots off the label. For a margin, take twice the margin off the width:This printed exactly where the first label's item did, with 195 dots on the left and 196 on the right.zpl^XA ^CF0,50 ^FO40,40^FB732,1,0,C^FDWinter jacket, size M\&^FS ^XZ - Text too long for one line prints over itself. With one line allowed, the rest of the text printed on top of the same line. Zebra's reference: text beyond the last line overwrites it.Allow two lines, and the second follows below, also centered:zpl
^XA ^CF0,50 ^FO0,40^FB812,1,0,C^FDWinter jacket with a detachable hood, size M\&^FS ^XZHere the wrapped first line had 18 dots on the left and 36 on the right, the half-character offset again, and the last line, ended byzpl^XA ^CF0,50 ^FO0,40^FB812,2,0,C^FDWinter jacket with a detachable hood, size M\&^FS ^XZ\&, 343 dots on the left and 344 on the right. Zebra's reference adds that a word too long for a line is split with a hyphen at the edge. - A width of 0, or none. Zebra's reference says the text does not print. In Labelary every character printed on top of the others at the left edge, between x 1 and 39.
- Rotated text. The width runs along the text, so for
^A0B(read from the bottom up) it is a height:In Labelary the price was centered in the 360 dots below y 20, with 127 dots above it and 126 below.zpl^XA ^FO250,20^A0B,50,50^FB360,1,0,C^FD$4.99\&^FS ^XZ
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.
1{2 "config": {3 "size": "4x2"4 },5 "data": {6 "item": "Winter jacket, size M",7 "price": "$4.99"8 },9 "templates": [10 {11 "type": "Text",12 "dataPath": "item",13 "coords": [40, 40],14 "font": "0,50",15 "align": "center"16 },17 {18 "type": "Text",19 "dataPath": "price",20 "coords": [40, 110],21 "font": "0,100",22 "align": "center"23 }24 ]25}And the ZPL that comes back, ready to send to the printer:
^XA^MCY^XZ
^XA
^SZ2
^MMT
^MNY
^LL406
^PW812
^LH0,0
^CF0,50^FO40,40^FB732,1,0,C,0^FDWinter jacket, size M\&^FS
^CF0,100^FO40,110^FB732,1,0,C,0^FD$4.99\&^FS
^XZSet a Text element's align to "center". zeepler sends it in a one-line ^FB block from x to the label width minus x, so the x in coords is the margin on both sides, and ends it with \&, as above. This request centers both lines with a margin of 40. The item had 195 dots on the left and 196 on the right, and the price 298 dots on the left and 298 on the right: where the plain ZPL put them.
"right"puts the text against the block's right edge, x dots in from the label's. x must be under half the label width.- A backslash in the value prints as one: zeepler doubles it inside the block, so a
\&is not a line break. - Text too long for the block prints over itself, as above: zeepler gives it one line, so keep it short enough, or cut it with
maxLength. Font size in ZPL covers how the font sets its size.
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 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.
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.