Skip to content

Send ZPL to a printer from code

To send ZPL to a network printer, open a TCP connection to port 9100 and write the text; the printer prints what it receives. Here it is in Python, C#, Node.js, Java and PHP.

Your label

Preview

100%

Python

import socket
with socket.create_connection(("192.168.1.50", 9100), timeout=5) as s:
    s.sendall(zpl.encode("utf-8"))

C#

using var client = new TcpClient("192.168.1.50", 9100);
var bytes = Encoding.UTF8.GetBytes(zpl);
client.GetStream().Write(bytes, 0, bytes.Length);

Node.js

const net = require("net");
const s = net.connect(9100, "192.168.1.50", () => s.end(zpl));

Java

try (Socket s = new Socket("192.168.1.50", 9100)) {
    s.getOutputStream().write(zpl.getBytes(StandardCharsets.UTF_8));
}

PHP

$s = fsockopen("192.168.1.50", 9100, $e, $m, 5);
fwrite($s, $zpl);
fclose($s);

Check it first

Render the label with the API in your tests, so a bad template fails there and not on the line. For accented text, add ^CI28 and send UTF-8.

Questions

What about USB printers?

On Windows, share the printer and copy the file to it, or send raw bytes through the spooler. See [Print a ZPL file](/print-zpl/).

Why does nothing print?

Check the address and that port 9100 is open, and that the data starts with ^XA and ends with ^XZ.

Related

Need something we don't have yet?

A connector for your system, a label format or a new tool: tell us what you use and we'll look at building it.

Request a tool or integration
Feedback