3

我的打印机是 Zebra ZM400 标签打印机,它连接到网络中的一台电脑(通过 USB 连接)。

我想通过网络将命令从我的电脑发送到标签打印机并打印标签。

如何从网络连接该打印机并从 Java 应用程序打印标签?

我知道我必须使用 ZPL 语言,但我不知道如何建立连接并向标签打印机发送命令。

是否可以?我在谷歌上冲浪,但我还找不到任何示例代码。

编辑

我使用了norbi771的方法..但是当它发送命令时,只是空白出来..

我的标签尺寸是 3.25" x 3.75"..

这是我的标签示例代码..但没有任何结果..

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..");
    }
4

3 回答 3

6

也许不是最好的答案,但我最近就是这样做的。我使用 Windows 将打印机连接到 PC。然后我共享打印机。然后我通过简单的命令将这台共享打印机映射到 LPT1(所有这些都可以在一台 PC 上完成):

net use \\pcname\sharedprinter LPT1:

由于现在这个 LPT1 端口是您可以写入的又名文件。现在我只需在 JAVA 中将数据写入该文件,它工作正常。我知道它不是很优雅,但对我有用,让我可以使用在几台 PC 之间共享的一台标签打印机

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

提供的示例用于 EPL 打印,但 ZPL 应该以相同的方式工作。

于 2013-06-05T10:27:21.153 回答
2

Zebra 确实在www.zebra.com/link上提供了 Java API 。它没有声称支持 ZM400,但值得花 20 分钟研究一下。如果它不支持该型号,我会感到惊讶,因为所有受支持的打印机都使用 ZPL。

对于您的 ZPL,您在第四行之前缺少插入符号 ^,就在 FO20,60 之前。此外,您正在使用字体“D”(如命令 ^AD 所示)。您应该首先考虑将其更改为“0”(如 ^A0)以使用默认打印机字体。您可以在此处阅读 ZPL 手册:https: //support.zebra.com/cpws/docs/zpl/zpl_manual.pdf。这是一个快速的 hello world 示例:

^XA
^FO50,50
^A0N,50,50
^FD Hello World ^FS
^XZ
于 2013-06-06T13:00:18.817 回答
1

它对我有用,你可以试试。

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
    }
}
于 2018-03-30T07:39:53.947 回答