Print a barcode label on a Zebra printer using Browser Print
- Download the Zebra Browser Print install application. Install the application service as described in the installation notes (attached). Localize the library "\ZebraBrowserPrintDocsWebCodeExamples\lib" and upload the script "BrowserPrint-1.0.4.min" (as it is, no "prettify") to your instance as a UI Script.Tip: Create an empty UI Script (naming it "BrowserPrint-1.0.4.min") and save the UI Script not pasting/inserting the script. From list view add "Script" field and paste the code using the list view. This preventing syntax check and being able to save when using the form view.
- If you want to use the UI Script from different forms etc. make it global (Check "Global" field) otherwise leave it unchecked and load the UI Script in an OnLoad Client Script using ScriptLoader.In this example i'll leave it unchecked and use ScriptLoader described below.
Create a UI action "Print Zebra Barcode" as shown below.
Script:
```
function printZebraLabel() {
try {
// Using Zebra Browser Print
var labelText = "";
var available_printers = null;
var selected_category = null;
var default_printer = null;
var selected_printer = null;
var format_start = "XALL200FO80,50A0N36,36FD";
var format_end = "FSXZ";
var default_mode = true;// Check if printing is triggered from the form or the list view var sysIds = []; if (typeof g_list !== 'undefined') { sysIds = g_list.getChecked().split(","); } // Check if default printer is set jslog("Connecting to default Zebra printer..."); BrowserPrint.getDefaultDevice("printer", foundDevice, deviceError);}
catch(err) {
jslog('A JavaScript runtime error occurred: ' + err.message);
}
function userCallback(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = JSON.parse(answer);
if (answer) {
jslog("Printing barcode with values: Model = " + answer.model.displayValue + ", Serial no: " + answer.serial_number.value);
printLabel(answer.model.displayValue, answer.serial_number.value);
}
}
function printLabel(model, serial) {
try {
// Create Zebra label using ZPL language
labelText = "^XA"; // Start command
labelText += "^CF0,20"; // Font size
labelText += "^FO20,115^FDModel: " + model + "^FS"; // Set Model
labelText += "^BY2,3,74^FT37,84^B3N,N,,Y,N"; // Set barcode format
labelText += "^FD" + serial + "^FS"; // Set barcode data
labelText += "^PQ1,0,1,Y^XZ"; // End command
selected_printer.send(labelText, printComplete, printerError);
}
catch(err) {
jslog('A JavaScript runtime error occurred: ' + err.message);
}
}
function foundDevice(printer) {
try {
default_printer = printer;
if ((printer != null) && (printer.connection != undefined)) {
selected_printer = printer;
jslog("Found Zebra printer: " + printer.name + " connected via: " + printer.connection);
// Check if printing is triggered from the form or the list view
if (sysIds.length > 0) {
for (var i = 0; i < sysIds.length; i++) {
var ga = new GlideAjax('AdvaniaUtils');
ga.addParam('sysparm_name','ajaxClientDataHandler');
ga.addParam('sysparm_tablename','alm_asset'); // Table name
ga.addParam('sysparm_sysid', sysIds[i]); // Sysid
ga.addParam('sysparm_fieldnames','model,serial_number'); // Field names we want to retrieve
ga.getXML(userCallback);
}
}
else {
printLabel(g_form.getDisplayValue('model'), g_form.getValue('serial_number'));
}
}
else {
alert("No default Zebra printer is configured. Please right-click on Zebra Browser Print icon in system tray and set a default Zebra printer.");
}
}
catch(err) {
jslog('A JavaScript runtime error occurred: ' + err.message);
}
}
function deviceError(errorMessage) {
try {
alert("An error occured while attempting to connect to your Zebra Printer. You may not have Zebra Browser Print installed, installed SSL certificate or it may not be running. Install Zebra Browser Print, or start the Zebra Browser Print Service, and try again! Error message: " + errorMessage);
}
catch(err) {
jslog('A JavaScript runtime error occurred: ' + err.message);
}
}
function sendData() {
jslog("Printing on Zebra...");
selected_printer.send(format_start + labelText + format_end, printComplete, printerError);
checkPrinterStatus( function (text){
if (text == "Ready to Print") {
selected_printer.send(format_start + labelText + format_end, printComplete, printerError);
}
else {
printerError(text);
}
});
}
function checkPrinterStatus(finishedFunction) {
selected_printer.sendThenRead("~HQES",
function(text){
var that = this;
var statuses = [];
var ok = false;
var is_error = text.charAt(70);
var media = text.charAt(88);
var head = text.charAt(87);
var pause = text.charAt(84);
// check each flag that prevents printing
if (is_error == '0')
{
ok = true;
statuses.push("Ready to Print");
}
if (media == '1')
statuses.push("Paper out");
if (media == '2')
statuses.push("Ribbon Out");
if (media == '4')
statuses.push("Media Door Open");
if (media == '8')
statuses.push("Cutter Fault");
if (head == '1')
statuses.push("Printhead Overheating");
if (head == '2')
statuses.push("Motor Overheating");
if (head == '4')
statuses.push("Printhead Fault");
if (head == '8')
statuses.push("Incorrect Printhead");
if (pause == '1')
statuses.push("Printer Paused");
if ((!ok) && (statuses.Count == 0))
statuses.push("Error: Unknown Error");
finishedFunction(statuses.join());
}, printerError);
}
function printComplete() {
jslog("Zebra bar code printing complete.");
}
function printerError(text) {
alert("An error occurred while printing on Zebra printer. Please check if Zebra printer is online and try again." + text);
}
}
```
https://www.servicenow.com/community/now-platform-articles/print-a-barcode-label-on-a-zebra-printer-using-browser-print/ta-p/2322299