Sending a Barcode to a Zebra Printer from a Java Application

前端 未结 2 1972
借酒劲吻你
借酒劲吻你 2020-12-16 01:07

I am creating a java application that retrieves a userId from a database, transforms it into a barcode, then sends it to a printer. I am planning to use a Zebra printer and

2条回答
  •  执念已碎
    2020-12-16 01:40

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

    EPL2 programming guide for Zebra

    Author note: Please do not remove this link from here as this is a historical reference. Maybe the server starts working again or there is a wayback copy. Instead, see the comments section for some suggestions or use web search to find EPL language specification

    Code sample:

    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;
    }
    

提交回复
热议问题