Skip to main content

ZPL how-to

Convert HTML to ZPL

ZPL places each field at a position in dots; HTML is laid out by a browser. There are two honest routes from one to the other. Draw the HTML in a headless browser at the printer's resolution and send the picture as ^GF: it looks like the page, but its text and barcodes become dots. Or leave the HTML out and write the label's data as ZPL fields, at the positions the HTML gave them: smaller, and the printer draws the text and barcodes itself, but CSS layout does not carry over.

Route 1: draw the HTML, send the dots as ^GF

This Node.js script uses Playwright to draw the page in a headless Chromium, then turns the screenshot into a ^GF image. Install them, with JsBarcode for the sample's barcode:

bash
npm install playwright jsbarcode
npx playwright install chromium

Save the script as html2zpl.mjs:

javascript
import { writeFileSync } from 'node:fs';
import { pathToFileURL } from 'node:url';
import { chromium } from 'playwright';

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

// Whole dots, rounded down. The page is drawn 1 CSS pixel to a dot.
const w = Math.floor(WIDTH * DPI), h = Math.floor(HEIGHT * DPI);
const rowBytes = Math.ceil(w / 8);
const rowsPerField = Math.floor(LIMIT / rowBytes);

const browser = await chromium.launch();
const page = await browser.newPage({ viewport: { width: w, height: h }, deviceScaleFactor: 1 });
await page.goto(pathToFileURL(process.argv[2]).href);
const png = await page.screenshot();

// Read the screenshot back in the browser: a black dot for each pixel
// darker than half, 8 dots to a byte, a row at a time, as hex.
const hex = await page.evaluate(async ({ b64, w, h, rowBytes }) => {
  const img = new Image();
  img.src = 'data:image/png;base64,' + b64;
  await img.decode();
  const ctx = new OffscreenCanvas(w, h).getContext('2d');
  ctx.fillStyle = 'white';
  ctx.fillRect(0, 0, w, h);
  ctx.drawImage(img, 0, 0);
  const px = ctx.getImageData(0, 0, w, h).data;
  const bytes = new Uint8Array(rowBytes * h);
  for (let y = 0; y < h; y++) {
    for (let x = 0; x < w; x++) {
      const i = (y * w + x) * 4;
      const grey = (px[i] * 299 + px[i + 1] * 587 + px[i + 2] * 114) / 1000;
      if (grey < 128) bytes[y * rowBytes + (x >> 3)] |= 0x80 >> (x & 7);
    }
  }
  return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('').toUpperCase();
}, { b64: png.toString('base64'), w, h, rowBytes });
await browser.close();

const zpl = ['^XA^PW' + w + '^LL' + h + '^LH0,0'];
for (let top = 0; top < h; top += rowsPerField) {
  const part = hex.slice(top * rowBytes * 2, (top + rowsPerField) * rowBytes * 2);
  const n = part.length / 2;
  zpl.push('^FO0,' + top + '^GFA,' + n + ',' + n + ',' + rowBytes + ',' + part + '^FS');
}
zpl.push('^XZ');
writeFileSync(process.argv[3], zpl.join('\n') + '\n');

Then give it the HTML and the file to write:

bash
node html2zpl.mjs label.html label.zpl
  • The browser window is the label in dots, 812 × 406 for 4 × 2 inches at 203 dpi, one CSS pixel to a dot. So the HTML must be laid out in pixels as dots; the next section shows why.
  • A pixel darker than half becomes a black dot, the same rule as the PDF to ZPL guide's script, packed 8 to a byte, a row at a time, as ^GF wants them. The logos and images guide explains the bytes.
  • Like the PDF script, it writes as many rows as fit in one ^GF of 99,999 bytes, then starts another below. A 4 × 2 label fits in one; a 4 × 6 label takes two.

What it made of our sample

The sample is a 4 × 2 inch label with sample content only: a rounded border, a bold line, a rule, a line of text, a Code 128 barcode drawn by JsBarcode and a centred line in red. Copy JsBarcode next to it (cp node_modules/jsbarcode/dist/JsBarcode.all.min.js .) and save it as label.html:

html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
  /* 1 px is 1 dot: a 4 x 2 inch label at 203 dpi is 812 x 406. */
  body { margin: 0; width: 812px; height: 406px; position: relative; font-family: 'DejaVu Sans', sans-serif; }
  div, svg { position: absolute; }
  .box { left: 20px; top: 15px; width: 772px; height: 375px; box-sizing: border-box; border: 4px solid black; border-radius: 24px; }
  .bin { left: 40px; top: 30px; font-size: 64px; line-height: 64px; font-weight: bold; }
  .rule { left: 40px; top: 110px; width: 732px; height: 3px; background: black; }
  .shelf { left: 40px; top: 130px; font-size: 40px; line-height: 40px; }
  #code { left: 40px; top: 190px; }
  .note { left: 40px; top: 335px; width: 732px; text-align: center; font-size: 36px; line-height: 36px; color: #c00; }
</style>
</head>
<body>
  <div class="box"></div>
  <div class="bin">Bin A-12</div>
  <div class="rule"></div>
  <div class="shelf">Shelf 3, left</div>
  <svg id="code"></svg>
  <div class="note">Sample only</div>
  <script src="JsBarcode.all.min.js"></script>
  <script>
    JsBarcode('#code', 'A12-0003', { format: 'CODE128', width: 2, height: 100, margin: 0, fontSize: 24, font: 'DejaVu Sans' });
  </script>
</body>
</html>

We ran the script with Node 22, Playwright 1.63 and its Chromium, and JsBarcode 3.12, on Linux.

  • It wrote 82,881 bytes of ZPL: one ^GF of 41,412 bytes, written as hex, two characters a byte.
  • In Labelary, every dot was where the script set it, and no others: 32,151 black dots. zxing-cpp read the barcode as A12-0003, its bars from x 40 to 263.
  • JsBarcode's width: 2 is 2 pixels a module, so 2 dots: the bars came out 2, 4, 6, 8 dots wide, and the spaces 2, 4, 6, 8, whole modules.
  • The red line printed black. A colour is black or white after the threshold, by how dark it is; the PDF guide shows grey, and dithering.
  • The fonts are the browser's, so the label looks the way it does on the machine that runs the script: there, sans-serif was DejaVu Sans. Run it where you will run it in production.
  • That is Labelary's drawing, not a printed label. Print one and scan it before you rely on it.

Lay the page out in dots, not inches

CSS has 96 pixels to the inch. A page laid out in inches or millimetres, as print CSS usually is, has to be drawn at deviceScaleFactor: DPI / 96 for its inches to come out at the printer's size. Then every length is multiplied by 2.11, and most no longer land on whole dots.

  • We wrote the sample in inches, with a 1 pixel module, and drew it that way. The bars came out 2, 3, 4, 6, 7, 9 dots wide, where whole modules would be multiples of one width. zxing-cpp still read A12-0003 from the screenshot, which we did not send to Labelary.
  • Barcodes are where this matters. A barcode ZPL draws itself has modules of whole dots, as the Code 128 guide shows; a picture of one has whatever the scaling left. Lay the label out in pixels as dots, as the sample is, and draw it at a scale of 1.

What the picture costs

  • Size. 82,881 bytes for one 4 × 2 label, against 329 characters for the same label as fields, below. The logos guide's compressed form makes the picture smaller; the script does not write it.
  • The text is dots. The printer cannot change a value or use its own fonts: every new value is a new screenshot. Small text is only as sharp as the threshold leaves it.
  • Barcodes are only as good as the layout. In dots, as above, the bars land on whole dots. Scaled, they do not.
  • A browser has to run. Chromium runs wherever the label is made, for every label.

Route 2: write the label as ZPL fields

If your system makes the HTML, it has the data, and the label can be written as ZPL instead. The sample, field by field at the HTML's own positions:

zpl
^XA
^PW812
^LL406
^LH0,0
^FO20,15^GB772,375,4,B,1^FS
^CF0,64
^FO40,30^FDBin A-12^FS
^FO42,30^FDBin A-12^FS
^FO40,110^GB732,3,3^FS
^CF0,40
^FO40,130^FDShelf 3, left^FS
^BY2^FO40,190^BCN,100,Y,N,N,A^FDA12-0003^FS
^CF0,36
^FO40,335^FB732,1,0,C,0^FDSample only\&^FS
^XZ

What carries over from HTML and CSS:

  • Absolute positions. left and top in pixels as dots are ^FO's x and y: both measure from the top left corner. The label size guide covers ^PW, ^LL and ^LH.
  • Font size, in dots. ^CF0,64 is font 0, 64 dots tall. A size in points is points × dpi ÷ 72 dots, as the font size guide shows.
  • text-align. ^FB makes a block as wide as the HTML's box and centres or right-aligns the text in it, as the center text guide shows; with more than one line, it wraps, as the wrap text guide shows.
  • Borders and filled boxes. ^GB draws a box, a line or a solid block, and its last value rounds the corners: 0 to 8, a radius of that many eighths of half the shorter side, so 1 is about 23 dots here. The boxes and lines guide draws them.
  • font-weight: bold. ZPL has no bold; the boxes and lines guide prints the field twice, 2 dots apart, as the plain ZPL above does.
  • A barcode. ^BY2 and ^BC draw it with 2 dot modules, as JsBarcode did; the Code 128 guide covers the values.
  • An image. A logo can still be a ^GF, for the logo only, from the logos guide's script.
  • The same layout, new data. ZPL can store the layout on the printer and fill it, as the templates guide shows.

What does not:

  • Flow layout. Block, inline, flex and grid have nothing to match: every field goes where its ^FO puts it, and nothing moves when the text before it gets longer.
  • CSS fonts. The printer draws its own fonts. Zebra's ZPL Programming Guide has ^A@, which calls a font stored on the printer by name (p. 62); we have not tried it.
  • Colour. ^GB's colour is black or white (p. 210), and a ^GF dot is black or not.
  • JavaScript. Whatever the page works out in the browser has to be worked out before the ZPL is written.

The two routes side by side

  • In Labelary the plain ZPL drew the same bars as the picture: 0 dots differed along them, along the rule and along the box's straight sides. zxing-cpp read A12-0003.
  • 14,098 dots differed in all, in the text and the corners. The bold line was 252 dots wide in font 0, against 292 in DejaVu Sans Bold, and each rounded corner differed by about 36 dots.
  • So the fields are not a copy of the picture: choose the sizes by eye and print one, as the font size guide does.

Tools that do route 1 for you

Two of the three answers to the Stack Overflow question point to a converter, and both draw the page as a picture. The third builds the screen's HTML and the printer's commands from the same data, which is route 2. We read these pages on 2026-11-27 and have run neither tool.

  • ZplBox is open source (AGPL-3.0, version 1.3.2). The answer that links it quotes the project: it draws the page in an embedded Chromium as a PNG, makes it monochrome and sends it as one ^GF. Its README says it takes PDFs too, runs as a Docker container you host, and is also offered as a hosted API.
  • htmltozpl.com is a paid API with a free plan whose labels carry a watermark, its pricing page says. Its quick start says it runs the page's CSS, images and JavaScript, and its sample answer is one ^GFA,20706,20706,51 image, in the compressed form.
  • A hosted converter receives the label. Send only what you are allowed to give a third party.

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": "4x2"
4 },
5 "data": {
6 "bin": "Bin A-12",
7 "shelf": "Shelf 3, left",
8 "code": "A12-0003",
9 "note": "Sample only"
10 },
11 "templates": [
12 {
13 "type": "Box",
14 "coords": [20, 15],
15 "width": 772,
16 "height": 375,
17 "thickness": 4,
18 "rounded": 1
19 },
20 {
21 "type": "Text",
22 "dataPath": "bin",
23 "coords": [40, 30],
24 "font": "0,64"
25 },
26 {
27 "type": "Text",
28 "dataPath": "bin",
29 "coords": [42, 30],
30 "font": "0,64"
31 },
32 {
33 "type": "Line",
34 "coords": [40, 110],
35 "length": 732,
36 "direction": "h",
37 "thickness": 3
38 },
39 {
40 "type": "Text",
41 "dataPath": "shelf",
42 "coords": [40, 130],
43 "font": "0,40"
44 },
45 {
46 "type": "BarCode",
47 "dataPath": "code",
48 "coords": [40, 190],
49 "style": "code128",
50 "moduleWidth": 2,
51 "height": 100,
52 "hri": "below"
53 },
54 {
55 "type": "Text",
56 "dataPath": "note",
57 "coords": [40, 335],
58 "font": "0,36",
59 "align": "center"
60 }
61 ]
62}

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

zpl
^XA^MCY^XZ
    ^XA
    ^SZ2
    ^MMT
    ^MNY
    ^LL406
    ^PW812
    ^LH0,0
^FO20,15^GB772,375,4,B,1^FS
^CF0,64^FO40,30^FDBin A-12^FS
^CF0,64^FO42,30^FDBin A-12^FS
^FO40,110^GB732,3,3^FS
^CF0,40^FO40,130^FDShelf 3, left^FS
^BY2,2,59^FO40,190^BCN,100,Y,N,N,A^FDA12-0003^FS
^CF0,36^FO40,335^FB732,1,0,C,0^FDSample only\&^FS
^XZ

zeepler is route 2 as a service: it takes the label's data as JSON, not HTML, and writes the ZPL fields. Its elements are Text, BarCode, QRCode, Line and Box. This request makes the label above: a Box with rounded, the bold line as two Text elements 2 dots apart, and the centred line with align.

  • In Labelary it drew the same dots as the plain ZPL above, and zxing-cpp read A12-0003. The response is 329 characters, setup included.
  • What it cannot do: take HTML, draw an image or a logo, lay text out in flow, wrap it (one Text a line), or use CSS fonts or colour. A logo can be added to the response as a ^GF, as the logos guide shows.
  • Bold this way works only on left-aligned text. With align, a copy at x 42 gets a block 4 dots narrower than one at x 40, with the same middle, so it lands on the first. That is from the generator; we have not drawn it.

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.