How to set the value of variables for a label when printing to a zebra printer using the sdk on android

MindWire picture MindWire · Oct 11, 2011 · Viewed 14.5k times · Source

How can one print a pre-made label (made using Zeba Label Designer) that contains variables and set those variables before printing.

I have the following code, but I am not sure how to set a variable (eg. I have a QR Code in the label I designed and I would like to set its data before printing).

TcpPrinterConnection zebraPrinterConnection = new TcpPrinterConnection("192.168.1.100", TcpPrinterConnection.DEFAULT_ZPL_TCP_PORT);
 try {
     ZebraPrinter printer = ZebraPrinterFactory.getInstance(zebraPrinterConnection);
     printer.getFileUtil().sendFileContents("/sdcard/documents/labels/sample.lbl");
     zebraPrinterConnection.close();
 } catch (ZebraPrinterConnectionException e) {
     e.printStackTrace();
 } catch (ZebraPrinterLanguageUnknownException e) {
     e.printStackTrace();
 } catch (ZebraIllegalArgumentException e) {
     e.printStackTrace();
 }

Answer

Ovi Tisler picture Ovi Tisler · Oct 11, 2011

You need to look at the output from Zebra Label Designer to get your variables and then hook them up through the sdk

Checkout the documentation that came with the ZebraLink SDK, it has a bunch of good examples on how to print stored formats. Here's one of the examples. In this example, the "First Name" variable is number 12. The "Last Name" variable is number 11.

 ^XA
 ^DFE:FORMAT.ZPL
 ^FS
 ^FT26,243^A0N,56,55^FH\^FN12"First Name"^FS
 ^FT26,296^A0N,56,55^FH\^FN11"Last Name"^FS
 ^FT258,73^A0N,39,38^FH\^FDVisitor^FS
 ^BY2,4^FT403,376^B7N,4,0,2,2,N^FH^FDSerial Number^FS
 ^FO5,17^GB601,379,8^FS
 ^XZ

 TcpPrinterConnection zebraPrinterConnection = new TcpPrinterConnection("192.168.1.32", TcpPrinterConnection.DEFAULT_ZPL_TCP_PORT);
 try {
     zebraPrinterConnection.open();
     ZebraPrinter printer = ZebraPrinterFactory.getInstance(zebraPrinterConnection);
     Map<Integer, String> vars = new HashMap<Integer, String>();
     vars.put(12, "John");
     vars.put(11, "Smith");
     printer.getFormatUtil().printStoredFormat("E:FORMAT.ZPL", vars);
     zebraPrinterConnection.close();
 } catch (ZebraPrinterConnectionException e) {
     e.printStackTrace();
 } catch (ZebraPrinterLanguageUnknownException e) {
     e.printStackTrace();
 }