Skip to main content

ZPL how-to

Convert a PDF to ZPL

ZPL describes a label as commands, so a PDF has to be turned into them. One way, the one the Stack Overflow answer takes, is to draw each page as black and white dots at the printer's resolution and send the dots as a ^GF image. It prints, but as a picture: its text and barcodes become dots. If you make the PDF yourself, writing the label in ZPL instead is smaller and its barcodes land on whole dots.

Draw each page as dots, then send them as ^GF

This Python script uses pypdfium2, which draws PDF pages with the PDFium library, and Pillow. Install them:

bash
python -m pip install pypdfium2 pillow

Save the script as pdf2zpl.py:

python
import sys
import pypdfium2 as pdfium
from PIL import Image

DPI = 203             # the printer's dots per inch
WIDTH, HEIGHT = 4, 6  # the label, in inches
LIMIT = 99999         # the most bytes one ^GF can hold

# Whole dots, rounded down: a part of a dot is not on the label.
dots_w, dots_h = int(WIDTH * DPI), int(HEIGHT * DPI)
row_bytes = (dots_w + 7) // 8
rows_per_field = LIMIT // row_bytes

labels = []
for page in pdfium.PdfDocument(sys.argv[1]):
    width, height = page.get_size()  # in points, 72 to the inch
    scale = min(dots_w / width, dots_h / height)
    image = page.render(scale=scale, grayscale=True).to_pil().convert('L')
    label = Image.new('L', (dots_w, dots_h), 'white')
    label.paste(image, (0, 0))
    # A black dot for each pixel darker than half, 8 dots to a byte.
    ink = label.point(lambda v: 255 if v < 128 else 0).convert('1', dither=Image.Dither.NONE)
    data = ink.tobytes()
    zpl = [f'^XA^PW{dots_w}^LL{dots_h}^LH0,0']
    for top in range(0, dots_h, rows_per_field):
        part = data[top * row_bytes:(top + rows_per_field) * row_bytes]
        zpl.append(f'^FO0,{top}^GFA,{len(part)},{len(part)},{row_bytes},{part.hex().upper()}^FS')
    zpl.append('^XZ')
    labels.append('\n'.join(zpl))

open(sys.argv[2], 'w').write('\n'.join(labels) + '\n')

Then give it the PDF and the file to write:

bash
python pdf2zpl.py label.pdf label.zpl
  • Each page of the PDF becomes one label, from ^XA to ^XZ.
  • A pixel darker than half becomes a black dot, the same rule as the logos and images guide's PNG script, and the same dots: tobytes packs them 8 to a byte, a row at a time, as ^GF wants them.
  • ^PW and ^LL set the label to the size the script drew, and ^LH0,0 puts its top left corner at the label's. The label size guide covers all three.
  • It writes the file itself, as the logos guide's script does: a redirect in Windows PowerShell 5.1 would save it as UTF-16.

What it made of our test PDF

We made a 2 page PDF of 2,892 bytes, 4 × 6 inches, with sample content only: a border, two lines of Helvetica, a rule, a light grey band with "Keep dry" on it, a Code 128 barcode and a line of 6 point text. We ran the script with Python 3.14 on Linux, pypdfium2 5.15.2 and Pillow 13.0.0.

  • Each page drew at 812 by 1218 pixels: 4 × 6 inches at 203 dpi, one pixel to a dot.
  • It wrote 2 labels. In Labelary, every dot of each was where the script set it, and no others: 70,308 and 71,434 black dots.
  • zxing-cpp read both barcodes from Labelary's drawings: B01-0004 and B01-0005.
  • The text stayed readable down to the 6 point line, in Labelary's drawing. That is not a printed label: print one and look at it before you rely on small text.

Sizing it to the label

  • Set DPI to the printer's resolution and WIDTH, HEIGHT to the label in inches. A 4 × 6 inch label at 203 dpi is 812 × 1218 dots. For a 300 dpi printer, set DPI = 304.8: its head has 12 dots per mm, as the 300 dpi guide explains, and a 4 × 6 label comes out 1219 × 1828 dots.
  • The script rounds the label down to whole dots, as zeepler does. A 2.25 × 1.25 inch label at 203 dpi is 456 × 253 dots.
  • A PDF measures pages in points, 72 to the inch. Ours were 288 × 432 points, 4 × 6 inches, so they filled the label exactly.
  • A page of another shape, such as US Letter, is scaled to fit inside the label and put at the top left, with white below or to the right. A label that sits in one corner of a big page comes out small: crop the page to the label first.

Too big for one ^GF

  • The logos guide gives the limit: Zebra's reference takes 1 to 99,999 bytes in one ^GF. A whole 4 × 6 label is 102 bytes a row by 1218 rows, 124,236 bytes.
  • So the script writes as many rows as fit in one field, then starts another below it. Ours wrote 980 rows (99,960 bytes) at the top and 238 rows (24,276 bytes) from ^FO0,980.

Grey: a threshold or dithering

ZPL prints an image as black and white dots. The script makes a pixel black when it is darker than half, so light grey disappears: our band was 217 out of 255, and none of its dots printed. For shading and photos, dither instead. Change the ink = line to:

python
ink = label.point(lambda v: 255 - v).convert('1')
  • Pillow then spreads black dots in proportion to the grey: 5,880 of the band's 39,780 dots beside the text, about 15%. Labelary drew every dot where the script set it.
  • It also roughens edges: outside the band, 3,513 dots differed from the threshold's, along the border and the edges of the text. The barcode's bars came out the same widths and still read. Keep the threshold for labels that are text and barcodes.

Why the result is a picture

  • The text is dots. The printer does not know it is text, so you cannot change a value, and it prints in the PDF's font at the PDF's size. To change anything, make the PDF again and convert it again.
  • Barcodes change width. A module of our barcode was 0.0125 inch, 2.54 dots. Bars of 1, 2, 3 and 4 modules came out 3, 6, 8, 11 dots wide, and spaces 1 or 2, 4 or 5, 7, 9 or 10: bars grew and spaces shrank. It still read, in Labelary's drawing, which is not a printed label. A barcode that ZPL draws itself, with ^BC, has modules of whole dots, as the Code 128 guide shows. Scan a printed label before you rely on it.
  • It is big. Our 2,892 byte PDF became 497,126 characters of ZPL: 248,472 of hex a label, two for each byte. The compressed form in the logos guide brought each label's data to about 10,286 characters, and Labelary drew it the same, but the script does not write it. The Stack Overflow answer says their printers struggled with megabytes of bitmap data until they compressed it.

Labelary converts images, not PDFs

Labelary's graphics API, which the logos guide uses for a PNG, turns an image into a ^GF. We sent it our PDF and it answered 400: ERROR: Unknown image format.

  • Sent page 1 as a 1-bit PNG of 812 × 1218 pixels, the script's dots, it answered one ^GF of 124,236 bytes in the compressed form, 10,324 characters, with the same dots. That is one field over the 99,999 byte limit above; split it before you print it.
  • Its API page limits that API to images of 2,000 pixels and 200 KB.
  • Everything you send goes to Labelary. A shipping label holds a name and an address. Send only what you are allowed to give a third party; the ZPL to PDF guide covers its plans and limits.

Other routes

  • PDFtoZPL is an open-source .NET library (MIT licence, version 6.4.0 on NuGet), the package the only answer to the Stack Overflow question links to. Its README describes the same steps: PDFium draws the page, it is made black and white, written as ^GF and compressed. We read its README on 2026-11-27; there is no .NET where we test, so we have not run it.
  • PDF Direct is Zebra's answer from the printer's side: its pages call it an on-printer emulation that lets Print DNA and Link-OS printers receive and print a PDF themselves. It is installed with Zebra's firmware downloader, and its support page says the install file comes with an activation licence. We read those pages on 2026-11-27 and have not tried it; ask Zebra whether your model takes it.

If you make the PDF, write the label instead

The asker on Stack Overflow came back to say they built the ZPL from scratch instead. If your system makes the PDF, it has the data, and the label can be written as ZPL fields. Page 1 of our test PDF:

zpl
^XA
^PW812
^LL1218
^LH0,0
^FO22,22^GB768,1174,4^FS
^CF0,60^FO62,100^FDSample box B-04^FS
^CF0,45^FO62,180^FDBox 1 of 2^FS
^FO62,258^GB688,3,3^FS
^CF0,50^FO90,350^FDKeep dry^FS
^BY3,2,59^FO62,592^BCN,200,Y,N,N,A^FDB01-0004^FS
^CF0,20^FO62,1120^FDTest file. Sample content only.^FS
^XZ
  • ^GB draws the border and the rule, as the boxes and lines guide shows. ^BY3 makes each module of the barcode 3 dots.
  • The printer draws the text in its own font 0, not Helvetica, and the grey band is left out.

The same label from data

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": "4x6"
4 },
5 "data": {
6 "item": "Sample box B-04",
7 "box": "Box 1 of 2",
8 "note": "Keep dry",
9 "code": "B01-0004",
10 "footer": "Test file. Sample content only."
11 },
12 "templates": [
13 {
14 "type": "Box",
15 "coords": [22, 22],
16 "width": 768,
17 "height": 1174,
18 "thickness": 4
19 },
20 {
21 "type": "Text",
22 "dataPath": "item",
23 "coords": [62, 100],
24 "font": "0,60"
25 },
26 {
27 "type": "Text",
28 "dataPath": "box",
29 "coords": [62, 180],
30 "font": "0,45"
31 },
32 {
33 "type": "Line",
34 "coords": [62, 258],
35 "length": 688,
36 "direction": "h",
37 "thickness": 3
38 },
39 {
40 "type": "Text",
41 "dataPath": "note",
42 "coords": [90, 350],
43 "font": "0,50"
44 },
45 {
46 "type": "BarCode",
47 "dataPath": "code",
48 "coords": [62, 592],
49 "style": "code128",
50 "moduleWidth": 3,
51 "height": 200,
52 "hri": "below"
53 },
54 {
55 "type": "Text",
56 "dataPath": "footer",
57 "coords": [62, 1120],
58 "font": "0,20"
59 }
60 ]
61}

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

zpl
^XA^MCY^XZ
    ^XA
    ^SZ2
    ^MMT
    ^MNY
    ^LL1218
    ^PW812
    ^LH0,0
^FO22,22^GB768,1174,4^FS
^CF0,60^FO62,100^FDSample box B-04^FS
^CF0,45^FO62,180^FDBox 1 of 2^FS
^FO62,258^GB688,3,3^FS
^CF0,50^FO90,350^FDKeep dry^FS
^BY3,2,59^FO62,592^BCN,200,Y,N,N,A^FDB01-0004^FS
^CF0,20^FO62,1120^FDTest file. Sample content only.^FS
^XZ

zeepler builds a label from JSON and has no PDF or image input: a request is JSON, and its elements are Text, BarCode, QRCode, Line and Box. This request makes the label above, with the same fields.

  • In Labelary it drew the same dots as the plain ZPL above, and zxing-cpp read B01-0004. Every bar and space was a whole number of 3 dot modules.
  • The response is 338 characters, setup included, against 248,472 characters of hex for the same page as dots.
  • What it cannot do: draw a PDF, an image or a logo, print grey, or use the PDF's fonts. A logo can be added to the response as a ^GF, as the logos guide shows.

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.