Skip to main content

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

zpl
^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,40 starts the block at the label's left edge, and ^FB812,1,0,C makes 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.
  • L and R in place of C align to the block's left or right edge. J justifies every line but the last.
  • ^FT works 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 \&:

zpl
^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

In 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:

zpl
^XA
^CF0,50
^FO0,40^FB812,2,0,C^FDWinter jacket with a detachable hood,\&size M\&^FS
^XZ

In 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 ^FO x from an old layout, such as ^FO40 with 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:
    zpl
    ^XA
    ^CF0,50
    ^FO40,40^FB732,1,0,C^FDWinter jacket, size M\&^FS
    ^XZ
    This printed exactly where the first label's item did, with 195 dots on the left and 196 on the right.
  • 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.
    zpl
    ^XA
    ^CF0,50
    ^FO0,40^FB812,1,0,C^FDWinter jacket with a detachable hood, size M\&^FS
    ^XZ
    Allow two lines, and the second follows below, also centered:
    zpl
    ^XA
    ^CF0,50
    ^FO0,40^FB812,2,0,C^FDWinter jacket with a detachable hood, size M\&^FS
    ^XZ
    Here 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 by \&, 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:
    zpl
    ^XA
    ^FO250,20^A0B,50,50^FB360,1,0,C^FD$4.99\&^FS
    ^XZ
    In Labelary the price was centered in the 360 dots below y 20, with 127 dots above it and 126 below.

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 "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:

zpl
^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
^XZ

Set 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 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.