Skip to main content

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:

zpl
^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. FFFF is a row of 16 black dots, C003 two black dots at each end.
  • d is 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.
  • c is the bytes in the whole image, 2 × 8 = 16, and Zebra's reference says b should match it for A, 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:

python
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 ^GF of 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:

bash
curl --silent --form file=@logo.png --output labelary-logo.zpl https://api.labelary.com/v1/graphics

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

  • G to Y repeat the next digit 1 to 19 times, g to z 20 to 400 times. JF is FFFF.
  • A comma fills the rest of the row with 0s, ! with 1s, and a colon repeats the row before.
zpl
^XA
^FO40,40^GFA,16,16,2,
JF
CH03
:
C3C3
:
CH03
:
JF
^FS
^XZ

In ^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 b and c a range of 1 to 99,999 bytes, and says a b out 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 ^GF in full, with no warning, so a preview will not show the problem. Split a big image into two ^GF fields, 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:

zpl
~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
^XZ
  • R:FRAME.GRF is where and under what name: Zebra's examples use R: 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,2 are 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.GRF deletes 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 ~DG with 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.

json
1{
2 "config": {
3 "size": "4x2",
4 "resetPrinter": true
5 },
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": 100
22 }
23 ]
24}

And the ZPL that comes back, ready to send to the printer:

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

zeepler'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:

python
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.zpl is the script's output above, and label.zpl the 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 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.

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.