Using a zebra S4M label printer with PHP
As part of a larger system replacement/expansion we needed to work with a networked zebra label printer.
For those of you who don’t know what a zebra printer is: It’s a specialized label printer which comes is a wide variety of models. The main model we are using is the S4M which has built in barcode and ncurses-type graphics support. This makes life easy if you want to add EAN/Datamatrix barcodes without doing the creation and error correction yourself. Another selling point is the option to add a built-in (or external by connector) print-server.
This allows us to print process-specific labels directly by using raw print. The printer itself uses a specific markup language called ZPL-II which is an ASCII format that roughly looks like this:
^XA
^FO10,10
^A0,40,40
^FD
Hello World!
^FS
^XZ
This prints converts this to the actual PS and spits out a label containing “Hello World!” at position (x,y in dots) 10,10 using built-in font 0 with a height and width of 40 dots.
The most basic way of printing a label using PHP is:
- Connect to the IP address of the zebra printer (actually the built-in or connected print server)
- Send your label as ZPL-II to the printer
- Close socket
In code this roughly looks like this:
<?php
$sock = fsockopen('ip.or.hostname.of.printer', '9001'); // Port is usually 9001 when doing a raw print
$label = "My ZPL label data";
fwrite($sock, $label, strlen($label);
fclose($sock);
?>
The print server will not return a status code so error checking is somewhat more difficult.
Another option is the usage of the XML capacities that some models have. This roughly works as follows:
- Create template by using a label designer tool (or manually)
- Store this in the printer
- Send XML containing the variables to the server
I have not yet been able to use this feature. There are also references to FTP capabilties which allows you to upload an ascii file which will get processed.
I hope this will help some of you that want to interface directly with a networked zebra label printer.