Question

my printer is Zebra ZM400 label printer and it's connected to one of the pc (connected with USB) in the network.

I want to send the command to the label printer from my pc via network and print label.

How to connect that printer from network and print label from java application?

I know I've to use ZPL langauage but I don't know how to make connection and send command to label printer.

Is it possible? I surfed in the google but i can't find any sample code yet.

EDIT

I used norbi771's method.. but when it sent the command, just blank come out..

my label's dimension is 3.25" x 3.75"..

This is my sample code for label .. but nothing comes out..

public class TestLabelPrinter {

    /**
     * @param args
     */
    public static void printLabel(String label, String company, String docDate)  {
        try {
            FileOutputStream os = new FileOutputStream("\\\\192.168.42.57\\zd");
            PrintStream ps = new PrintStream(os); 
            String commands = "^XA" +
                              "^LH30,30" +
                              "^F020,10^AD^FDZEBRA^FS" + 
                              "F020,60^B3^FDAAA001^FS" + 
                              "^XZ";     

            ps.println(commands);
            ps.print("\f");
            ps.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        printLabel("label 12345", "Company name", "2013-05-10 12:45");
        System.out.println("Successful..");
    }
Was it helpful?

Solution

Maybe not the best answer, but I recently did it like that. I connected the printer to the PC with Windows. Then I shared the printer. Then this shared printer I mapped into LPT1 via simple command (all of this can be done one one PC):

net use \\pcname\sharedprinter LPT1:

Since now this LPT1 port is aka file you can write to. Now I simply write data to that file in JAVA and it is working fine. I know it is not very elegant, but works for me and lets me use one label printer shared between a few PCs

    public class EplPrint1 {
            private final String port;
            public EplPrint1(String port) {
                    this.port = port;
            }
            public void printLabel(String label, String company, String docDate) throws FileNotFoundException {
                    FileOutputStream os = new FileOutputStream(port);
                    PrintStream ps = new PrintStream(os);
                    String commands = "N\n"
                            +  "A1,1,0,1,1,1,N,\""+asciiNormalize(company)+"\"\n"
                            + "A1,20,0,1,1,1,N,\""+asciiNormalize("Entry date")+": " + docDate+"\"\n"
                            + "B1,40,0,1,3,2,80,B,\""+label+"\"\n"
                            + "P1,1\n";     

                    ps.println(commands);
                    ps.print("\f");
                    ps.close();
            }

            public static void main(String[] argv) throws FileNotFoundException {
                    //EplPrint1 p = new EplPrint1("d:\\tmp\\eplcommands.txt");
                    EplPrint1 p = new EplPrint1("LPT1");
                    //p.printLabel("23535.A.33.B.233445");  
                    p.printLabel("label 12345", "Company name", "2013-05-10 12:45");
            }
    }

The example provided is for EPL printing, but ZPL should work the same way.

OTHER TIPS

Zebra does provide a Java API at www.zebra.com/link. It doesn't claim to support the ZM400, but it's worth looking into for 20 minutes. I'd be surprised if it didn't support that model since all of the supported printers speak ZPL.

For your ZPL, you are missing a caret ^ before the fourth line, right before the FO20,60. Also, you are using font 'D' (as indicated by the command ^AD). You should consider changing that to '0' (as in ^A0) to use the default printer font at first. You can read the ZPL manual here: https://support.zebra.com/cpws/docs/zpl/zpl_manual.pdf. Here is a quick hello world example:

^XA
^FO50,50
^A0N,50,50
^FD Hello World ^FS
^XZ

It worked for me you can try.

public class ZebraGiftPrinter {

    public void getPrint(String zpl_data, String printer) throws IOException {

        PrintService myPrintService = findPrintService(printer);

        DocPrintJob job = myPrintService.createPrintJob();
        DocFlavor flvr = DocFlavor.BYTE_ARRAY.AUTOSENSE;
        Doc doc = new SimpleDoc(zpl_data.getBytes(), flvr, null);
        try {
            job.print(doc, null);
            System.out.println("Print Done!");
        } catch (PrintException e) {
            System.out.println(e.getCause());
        }
    }

    private static PrintService findPrintService(String printerName) {
        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(
                null, null);
        for (PrintService printService : printServices) {
            if (printService.getName().equals(printerName)) {
                System.out.println(printService);
                return printService;
            }
        }
        return null;
    }

    public static void main(String[] args) throws IOException {
        String g = " ^XA^FO250,200^AQN,50,50^FDSAMPLE ARIALI^FS ^XZ";
        ZebraGiftPrinter gift = new ZebraGiftPrinter();
        gift.getPrint(g, "\\\\192.168.42.57\\printer_name");    // name of network printer
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top