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:
python -m pip install pypdfium2 pillowSave the script as pdf2zpl.py:
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:
python pdf2zpl.py label.pdf label.zpl- Each page of the PDF becomes one label, from
^XAto^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:
tobytespacks them 8 to a byte, a row at a time, as^GFwants them. ^PWand^LLset the label to the size the script drew, and^LH0,0puts 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-0004andB01-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
DPIto the printer's resolution andWIDTH, HEIGHTto the label in inches. A 4 × 6 inch label at 203 dpi is 812 × 1218 dots. For a 300 dpi printer, setDPI = 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:
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
^GFof 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
^GFand 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:
^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^GBdraws the border and the rule, as the boxes and lines guide shows.^BY3makes 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.
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": 419 },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": 338 },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:
^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
^XZzeepler 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
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.
- Logos and images in ZPL (^GF, ~DG, ^XG): Print a logo with ^GF, turn a PNG into hex, keep it under the size limit, and store it once with ~DG to print with ^XG.
- GS1-128 barcodes in ZPL, with the check digit: Print GS1-128 with ^BC and FNC1, work out the mod 10 check digit of a GTIN or SSCC, separate fields that vary in length, and what the printer adds itself.
- 300 dpi printers, and the same ZPL on 203 and 300 dpi: Why a 203 dpi label prints smaller at 300 dpi, scaling it by 1.5 and what does not scale exactly, ^MU in millimetres, and reading the printer's resolution.
- Comments in ZPL: ^FX: Write a comment with ^FX, why a caret inside one runs as a command, comments outside ^XA and ^XZ, and why // is not a comment.
- Emulate a Zebra printer for testing: A stand-in printer on port 9100 in a few lines of Python or Node.js, pointing your send code at it, checking what it caught in Labelary, and what a stand-in cannot test.
- Convert ZPL to PDF or PNG: Labelary's API turns ZPL into a PDF, one label to a page, or a PNG of one label: curl, Python and Node.js, its limits, and what it means to send it your labels.
- Convert EPL to ZPL: Translate an EPL2 label to ZPL by hand: each common EPL command and its ZPL equivalent, one label translated line by line, what does not carry over, and switching a printer's language.
- Convert HTML to ZPL: Two ways from an HTML label to ZPL: draw the page in a headless browser and send the dots as ^GF (a Node.js script), or write the label as ZPL fields, with which HTML and CSS ideas carry over and which do not.
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.