Printing Zebra ZPL Or Raw Data
Regular printing from a Windows application uses a printer driver supplied by the manufacturer. The driver is responsible for rendering the output and translating Windows print commands to a printing language native to the printer.
Some printers have unique capabilities that the printer driver does not support. One example is point-of-sale printers that can cut the paper after printing a receipt. Another is label printers with a feature to program RFID tags. Using the printer's native language can also significantly improve the printing speed. These are obvious reasons you may want to send data directly to a printer bypassing the driver.
Direct print introduced raw data printing for use with Zebra label printers. This is where we first saw the need for a feature to bypass the driver. Therefore, we refer to raw print as Zebra print. However, this is not specific to Zebra printers. It can be used with any printer with support for raw print jobs.
Raw print jobs
There are only two steps to create a raw print job for Direct Print.
Create a stream that contains the raw data for the job.
Create a print job on the print queue with the document and the type set to Zebra.
Zebra example
A Zebra code sample on GitHub shows you how to do this. Here is a quick walk-through of the example.
Create a Zebra label design
The Zebra Designer is an excellent place to start unless you are fluent in the Zebra language. We will use it to generate the zpl file we want to print.
This is the design we want to use:
The design contains two texts we will use as placeholders for item-related data.
If we print our label to a file, we can capture the ZPL language for the layout.
We get a ZPL file with the following content:
CT~~CD,~CC^~CT~
^XA~TA000~JSN^LT0^MNW^MTD^PON^PMN^LH0,0^JMA^PR5,5~SD15^JUS^LRN^CI0^XZ
^XA
^MMT
^PW609
^LL0406
^LS0
^BY4,3,84^FT102,199^BCN,,Y,N
^FD>:{NO}^FS
^FT102,80^A0N,28,28^FH\^FD{DESCRIPTION}^FS
^PQ1,0,1,Y^XZ
We need a way to get this file content into our AL-code. One way to do this is to convert it to BASE64 using one of the many online services. The BASE64 string looks like this:
Q1R+fkNELH5DQ15+Q1R+Cl5YQX5UQTAwMH5KU05eTFQwXk1OV15NVEReUE9OXlBNTl5MSDAsMF5KTUFeUFI1LDV+U0QxNV5KVVNeTFJOXkNJMF5YWgpeWEEKXk1NVApeUFc2MDkKXkxMMDQwNgpeTFMwCl5CWTQsMyw4NF5GVDEwMiwxOTleQkNOLCxZLE4KXkZEPjp7Tk99XkZTCl5GVDEwMiw4MF5BME4sMjgsMjheRkhcXkZEe0RFU0NSSVBUSU9OfV5GUwpeUFExLDAsMSxZXlha
In this example, an action on the item card creates the Zebra print job in the print queue. This is the resulting AL-code:
trigger OnAction()
var
DirPrtQueue: Record "ForNAV DirPrt Queue";
Base64Convert: Codeunit "Base64 Convert";
tempBlob: Codeunit "Temp Blob";
base64, zpl: Text;
zplInStream: InStream;
zplOutStream: OutStream;
begin
// Get Zebra language from stored base64 string
base64 := 'Q1R+fkNELH5DQ15+Q1R+Cl5YQX5UQTAwMH5KU05eTFQwXk1OV15NVEReUE9OXlBNTl5MSDAsMF5KTUFeUFI1LDV+U0QxNV5KVVNeTFJOXkNJMF5YWgpeWEEKXk1NVApeUFc2MDkKXkxMMDQwNgpeTFMwCl5CWTQsMyw4NF5GVDEwMiwxOTleQkNOLCxZLE4KXkZEPjp7Tk99XkZTCl5GVDEwMiw4MF5BME4sMjgsMjheRkhcXkZEe0RFU0NSSVBUSU9OfV5GUwpeUFExLDAsMSxZXlha';
zpl := Base64Convert.FromBase64(base64);
// Replace the placeholders with the real data
zpl := zpl.Replace('{NO}', Rec."No.").Replace('{DESCRIPTION}', Rec.Description);
// Create an InStream with the Zebra language
tempBlob.CreateOutStream(zplOutStream);
zplOutStream.WriteText(zpl);
tempBlob.CreateInStream(zplInStream);
// Create a print job on the print queue
DirPrtQueue.Create('Item Label', 'Zebra Demo', zplInStream, DirPrtQueue.ContentType::Zebra);
end;
The code converts the BASE64 back into a text string and substitutes the placeholders with data from the Item record.
Then, it creates an InStream with the raw data and calls the Create
function to create a print job with the data. It uses Zebra as the content type to tell direct print that the job should be printed as raw data.
Driver types
Some printers have special printer drivers for Windows when sending raw data to the printer.