Sending a barcode to a Zebra printer from a Java application

I am creating a java application that retrieves userId from a database, converts it to barcode and then sends it to the printer. I plan to use a Zebra printer, and I was wondering if anyone had experience printing with a Zebra printer from a Java application; if so, could you share some code to make this possible?

Thanks in advance, Tumaini

+8
java zebra-printers
source share
2 answers

There are two ways to work with Zebra printers. The first is to print like on a regular printer. The basics of printing in Java are well explained in the official guide. The end of the page will be treated by the printer as the end of the sticker. The disadvantage of this approach is that all painting must be done by hand. That is, you cannot use the printer's internal barcoding ability.

The second is to write ZPL commands directly to the printer. Something like that:

PrintService pservice = ... // acquire print service of your printer DocPrintJob job = pservice.createPrintJob(); String commands = "^XA\n\r^MNM\n\r^FO050,50\n\r^B8N,100,Y,N\n\r^FD1234567\n\r^FS\n\r^PQ3\n\r^XZ"; DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; Doc doc = new SimpleDoc(commands.getBytes(), flavor, null); job.print(doc, null); 

The disadvantage is that you need to learn the ZPL-Zebra Programming Language. Although simple enough, things like images and custom fonts can cause you a headache. Programming guides are available on the Zebra website: Part 1 and Part 2 .

+7
source share

Not every Zebra printer has a ZPL II, but then you can use the EPL

EPL2 Programming Guide for Zebra

Code example:

 private static boolean printLabel(PrintService printService, String label) { if (printService == null || label == null) { System.err.println("[Print Label] print service or label is invalid."); return false; } String czas = new SimpleDateFormat("d MMMMM yyyy'r.' HH:mm s's.'").format(new Date()); String command = "N\n"+ "A50,50,0,2,2,2,N,\""+label+"\"\n"+ "B50,100,0,1,2,2,170,B,\""+label+"\"\n"+ "A50,310,0,3,1,1,N,\""+czas+"\"\n"+ "P1\n" ; byte[] data; data = command.getBytes(StandardCharsets.US_ASCII); Doc doc = new SimpleDoc(data, DocFlavor.BYTE_ARRAY.AUTOSENSE, null); boolean result = false; try { printService.createPrintJob().print(doc, null); result = true; } catch (PrintException e) { e.printStackTrace(); } return result; } 
+8
source share

All Articles