Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

How To: Printing directly to Networked Zebra Label Printers

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
pmjd
Posts: 132
Joined: Fri Mar 12, 2021 10:38 am
Has thanked: 3 times
Been thanked: 1 time

How To: Printing directly to Networked Zebra Label Printers

Unread post by pmjd »

Though this might useful for others, probably not perfect (and missing some errors handling) but functional :)

This was setup to allow any device on our company intranet to be able to print selected information from a nuBuilder record to a networked Zebra label printer.

Once you have a label template ready to use you need to get the code of the label in ZPL (Zebra Print Language) format. Label design programs should allow you to do this, I use Bartender 2016 and the print to file option, there is also the online Labelary ZPL Viewer which allows you to change the code and see the output directly.

So I have the following label
label(9).png
Which has the following underlying ZPL code:

Code: Select all

^XA
^SZ2^JMA
^MCY^PMN
^PW626
~JSN
^JZY
^LH0,0^LRN
^XZ
^XA
^FT197,170
^CI0
^A0N,59,80^FDT0129^FS
^FT30,250
^A0N,42,64^FDPack Number:^FS
^FT440,256
^A0N,59,80^SN006,1,Y^FS
^FT37,94
^A0N,42,57^FDThingy Batch Number:^FS
^PQ1,0,1,Y
^XZ
This label will print the Batch number, and the starting Pack Number, which in turn will autoincrement with each label printed until all labels in the print job have been printed. Take this code and save it to a nuBuilder procedure, lets call it

Code: Select all

zpl_label
, with the Run option set to hidden and best to set the procedure to have Global in the Access Level, so all users can access it. Obviously you can change this option if needed.

In this label there are three components I want to take the information from nuBuilder: the Thingy Batch Number (T0129 in the image), the Pack Number value (006 in the image), and the number of labels to print.

We will replace these with hash cookie values:
in the label code replace T0129 with

Code: Select all

'.$lot.'
replace the 3 digit number in SN with

Code: Select all

'.$startnum.'
find PQ in the label code and replace the first 1 (this determines the number of labels to print) with

Code: Select all

'.$numberlabels.'
to generate

Code: Select all

PQ'.$numberlabels.',0,1,Y
Now we need to add the following code before all the labelling code

Code: Select all

$printer_ip = 192.168.30.1;
$printer_port = 9100';
$lot = '#tng_lot_number#';
$startnum = '#tng_batch_label_first#';
$numberlabels = '#tng_number_batch_labels#';
if(($conn = fsockopen($printer_ip,$printer_port,$errno,$errstr))===false){ 
    echo 'Connection Failed' . $errno . $errstr; 
} 

$data = ' 
and the following after the labelling code

Code: Select all

'; 

#send request 
$fput = fputs($conn, $data, strlen($data)); 

#close the connection 
fclose($conn);
Here I have simply hardcoded the Zebra printer's IP and Port addresses (192.168.30.1 and 9100, respectively). This may work for some but not all. They can also be replaced with hash cookie values (if you have multiple Zebra printers available) and pull the information from nuBuilder. It is a setup I have in use here, as we have different Zebra printers for different sizes, but for the sake of clarity I'll leave that out of this example.

Next we need to put this code in the nuBuilder form for the end user to access.

In a Form, which has fields for tng_lot_number (the lot number), tng_batch_label_first (the number you want the Pack Number to start at), and tng_number_batch_labels (how many labels you want to print), fill the fields in as required.
Add a Print button for printing and change the Custom Code of the button to the following:

Code: Select all

nuRunPHPHidden('zpl_label',0);
var t = this;
nuHide(t.id);
This will run the code to print the labels, then hide the button until the record is refreshed. I added this as a failsafe to prevent users pressing the Print button more than once accidentally/deliberately (especially on tablets), as it is usually used to print a few hundred labels at a time and there is no "Success" dialogue box, or option to cancel the job prior to printing. Once it's pressed it goes!

Hope this may be of use to others. If using mulitple printers I can tell you how I've achieved it if you want to know.

Thanks,
Paul
You do not have the required permissions to view the files attached to this post.
kev1n
nuBuilder Team
Posts: 4305
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 71 times
Been thanked: 445 times
Contact:

Re: How To: Printing directly to Networked Zebra Label Printers

Unread post by kev1n »

Thanks Paul for sharing this!
Post Reply