ZPL is a page description language used primarily for creating labels on thermal label printers. According to Wikipedia, there are at this moment about 170 commands in the language. Although I won’t delve into the language in this post, there are some basic rules that once you grasp them, you are well on your way to becoming a ZPL guru:
- All commands start with a caret sign ^.
An example command is ^FD, which represents the Field Data, used for writing out a string on the label.
- The code always starts with ^XA.
This acts as the opening bracket of your code and needs to go in the first line. It can be followed by other commands in the same line.
- Commands in the same line are separated by spaces.
- Add lines for your code as needed, usually ending each line with ^FS, the Field Separator denoting the end of the field definition.
- The last line is always just ^XZ.
This acts as the closing bracket of your code.
Here’s an example of a ZPL file that writes a simple label using embedded CFML variables:
Let’s assume that you have written your awesome ZPL label and, since ZPL code is basically text based, you have stored it in a text file or in your database. You now need to be able to send that to your label printer, and perhaps do so on demand from a web based app. I found 2 ways that you can do that, one by having the label printer installed as a printer on a computer, and another where the label printer is connected directly to the network as an IP printer.
Label printer installed on a computer
In this scenario the label printer is actually connected and installed on a computer as one of its available printers. The trick to sending data programatically to it is to force the computer into treating the lpt1 printer port as the one connected to it, and then copying, or sending, the data to it. We need a batch file to do that, which needs to exist on the computer with the web user having administrator rights to run it, and the cfexecute cfml tag available:
- %1 – the printer UNC path
- %2 – the ZPL data absolute file location
The limitations to this are obvious. You need to have the batch and the zpl data files available on the computer you need to print on, and access to the cfexecute tag. The Git repo has helper functions that create those 2 files, plus a main function that sends to the connected printer.
Label printer installed as an IP printer
A more flexible solution is to have the label printer connect to the network as an IP printer. This is simple to do by placing a USB over IP bridge between the network and the printer. Once you have that set up, we can talk to that printer by opening a socket connection and writing to it. We can take advantage of Java functionality which lies under the CFML server to help us do that.
The one caveat with this method is that I couldn’t get a verification that the ZPL data was successfully sent to the printer, just that the socket connection was set up properly. Perhaps someone who is better at Java can help with that.