ZPL how-to
Logos and images in ZPL (^GF, ~DG, ^XG)
ZPL prints images as black and white dots. ^GF carries the dots inside the label; ~DG stores them on the printer once, and ^XG prints them from there. Both take the dots as hex, and this page shows how to make that from a PNG.
An image in the label with ^GF
^GFA,b,c,d,data draws an image at the ^FO before it. This one is a frame 16 dots wide and 8 tall, with a block in the middle:
^XA
^FO40,40^GFA,16,16,2,
FFFF
C003
C003
C3C3
C3C3
C003
C003
FFFF
^FS
^XZ- Each hex digit is 4 dots, 1 for black.
FFFFis a row of 16 black dots,C003two black dots at each end. dis the bytes in a row: 2 here, two hex digits each. A row's width is rounded up to whole bytes, so a 100 dot wide image takes 13.cis the bytes in the whole image, 2 × 8 = 16, and Zebra's reference saysbshould match it forA, hex data.- Line breaks in the data are ignored, so one row a line is easier to read.
- In Labelary it covered x 40 to 55, y 40 to 47, 64 black dots, every dot where the hex put it.
Turn a PNG into ^GF
This Python script, with the Pillow library, prints the ^GF for logo.png. Save it as png2zpl.py and run python png2zpl.py. It writes logo.zpl itself, as a redirect in Windows PowerShell 5.1 would save it as UTF-16:
from PIL import Image
img = Image.open('logo.png').convert('RGBA')
white = Image.new('RGBA', img.size, 'white')
img = Image.alpha_composite(white, img).convert('L')
width, height = img.size
row_bytes = (width + 7) // 8
data = bytearray(row_bytes * height)
for y in range(height):
for x in range(width):
if img.getpixel((x, y)) < 128:
data[y * row_bytes + x // 8] |= 0x80 >> (x % 8)
total = len(data)
open('logo.zpl', 'w').write(f'^GFA,{total},{total},{row_bytes},{data.hex().upper()}\n')- It puts the image on white first, so a transparent background prints white, and prints any pixel darker than half as a black dot.
- Run with Pillow 13.0.0 on a 100 by 50 pixel PNG, it printed a
^GFof 650 bytes, 13 a row: 1,316 characters. Labelary drew each of the 1,158 black dots where the script set them, and no others. - Grey is a choice between black and white. The logo's dark grey bar printed solid, all 400 dots; its light grey bar printed nothing. Make the logo black and white before you convert it, at the size it will print: one pixel to a dot.
Labelary's API converts images too:
curl --silent --form file=@logo.png --output labelary-logo.zpl https://api.labelary.com/v1/graphicsFor the same PNG it answered a label of 409 characters, the same 650 bytes in the compressed form below, and it drew the same dots. It sends your image to Labelary, as the preview guide covers.
Less data: compression
Hex doubles the size: 650 bytes of image are 1,300 characters. Zebra's reference describes a shorter form, for ~DG and ~DB:
GtoYrepeat the next digit 1 to 19 times,gtoz20 to 400 times.JFisFFFF.- A comma fills the rest of the row with 0s,
!with 1s, and a colon repeats the row before.
^XA
^FO40,40^GFA,16,16,2,
JF
CH03
:
C3C3
:
CH03
:
JF
^FS
^XZIn ^GF, Labelary drew this the same as the frame above, dot for dot. A row of FF, drew as FF00.
Zebra's reference also gives two Base64 forms, :B64:data:crc and, compressed first, :Z64:data:crc, where crc is 4 hex digits checked by the printer: a mismatch counts as an aborted download. Labelary drew the frame the same in both, and also with a crc of 0000. A preview does not check it; a printer does.
How big an image can be
- Zebra's reference gives
bandca range of 1 to 99,999 bytes, and says about of range is set to the nearest limit. - A whole 4 × 2 inch label at 203 dpi is 102 bytes a row by 406 rows, 41,412 bytes. A whole 4 × 6 inch label is 124,236, over the limit.
- Labelary drew a 124,236 byte
^GFin full, with no warning, so a preview will not show the problem. Split a big image into two^GFfields, one under the other: two halves of 62,118 bytes drew the same. - Labelary has its own limits: 2,000 pixels for an image and 1 MB for a request, its API page says, and 200 KB for an image file sent to its graphics API, which answers a bigger one with a 400.
Store it once with ~DG, print it with ^XG
A logo on every label can be sent once and stored on the printer. ~DG takes the same hex, outside ^XA and ^XZ; ^XG prints it, and can scale it by whole numbers:
~DGR:FRAME.GRF,16,2,
FFFF
C003
C003
C3C3
C3C3
C003
C003
FFFF
^XA
^FO40,40^XGR:FRAME.GRF,1,1^FS
^FO100,40^XGR:FRAME.GRF,2,2^FS
^FO200,40^IMR:FRAME.GRF^FS
^XZR:FRAME.GRFis where and under what name: Zebra's examples useR:for the printer's memory (DRAM), names are 1 to 8 characters, and the extension is.GRF. Sending the same name again replaces it.16,2are the total bytes and the bytes a row, as in^GF.- At 1,1 it covered x 40 to 55, y 40 to 47, dot for dot as the frame. At 2,2, x 100 to 131, y 40 to 55: twice as wide and twice as tall.
^IM, the same without scaling, covered x 200 to 215, y 40 to 47. - A second label in the same request printed it again without sending it.
^IDR:FRAME.GRFdeletes it. Sent before the label, Labelary drew nothing:ERROR: Requested 1st label but ZPL generated no labels.- A preview only knows what is in the request. To preview a label that uses a stored graphic, send the
~DGwith it.
A logo on a zeepler label
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 "resetPrinter": true5 },6 "data": {7 "item": "Rack 12",8 "code": "R12-B03"9 },10 "templates": [11 {12 "type": "Text",13 "dataPath": "item",14 "coords": [40, 40],15 "font": "0,50"16 },17 {18 "type": "BarCode",19 "dataPath": "code",20 "coords": [40, 110],21 "height": 10022 }23 ]24}And the ZPL that comes back, ready to send to the printer:
^XA^MCY^XZ
^XA
^SZ2
^FWN,0^PON^PMN^LRN
^MMT
^MNY
^LL406
^PW812
^LH0,0
^CF0,50^FO40,40^FDRack 12^FS
^BY2,2,59^FO40,110^BCN,100,N,N,N,A^FDR12-B03^FS
^XZzeepler's templates are Text, BarCode and QRCode, with no image, and zeepler sends ^ in data as a character, as the boxes and lines guide shows, so a ^GF cannot come in through data. Add it to the ZPL you get back, before the last ^XZ:
logo = open('logo.zpl').read().strip()
zpl = open('label.zpl').read()
end = zpl.rindex('^XZ')
zpl = zpl[:end] + '^FO680,40' + logo + '^FS\n' + zpl[end:]
open('label.zpl', 'w').write(zpl)logo.zplis the script's output above, andlabel.zplthe response. The response starts with a short label of its own,^XA^MCY^XZ, which is why the script looks for the last^XZ.- In Labelary the logo's dots covered x 682 to 774, y 42 to 87, each where the script set it, and the text and barcode the same 13,808 dots as without it.
- The request sets
"resetPrinter": true, so the label starts with^FWN,0^PON^PMN^LRN.
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.
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.
- 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.
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.