2
votes

I'm trying to send some ZPL code to a Zebra TLP 2824 connected with USB from a Java application in Windows 7. I've tried different approaches but was not able to print yet. In the driver settings I activated passthrough mode and tried to install the printer with the generic / text-mode driver but nothing worked.

I always get unspecified Windows errors in the printing queue.

This is my code:

        try {

           PrintService psZebra = null;
           String sPrinterName = null;
           PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);

           for (int i = 0; i < services.length; i++) {

               PrintServiceAttribute attr = services[i].getAttribute(PrinterName.class);
               sPrinterName = ((PrinterName) attr).getValue();

               if (sPrinterName.toLowerCase().indexOf("generic") >= 0) {
                   psZebra = services[i];
                   System.out.println(psZebra);
                   break;
               }
           }

           if (psZebra == null) {
               System.out.println("Zebra printer not found.");
               return;
           }
           DocPrintJob job = psZebra.createPrintJob();

           String s = "${^XA^FO100,100^BY7^BCN,100,Y,N,N^FD123456^FS^XZ}$";

           byte[] by = s.getBytes();
           DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
           Doc doc = new SimpleDoc(by, flavor, null);
           job.print(doc, null);

       } catch (PrintException e) {
           e.printStackTrace();
       }   
1

1 Answers

5
votes

Seems that I was very close. My printer just don't supportes ZPL and I had to use EPL2 code. An other thing was to use an InputStream instead of byteArrays

Thats what changed:

DocPrintJob job = psZebra.createPrintJob();

String s =  "N"+"\n"+
        "q305"+"\n"+
        "Q203,26"+"\n"+
        "B55,26,0,1,2,2,152,B,\""+code+"\""+"\n"+
        "P1,1";

InputStream is = new ByteArrayInputStream(s.getBytes());
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(is, flavor, null);

job.print(doc, null);
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}