首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以ZPL打印ZPL为文本的Java打印

以ZPL打印ZPL为文本的Java打印
EN

Stack Overflow用户
提问于 2022-01-17 22:37:52
回答 1查看 1.1K关注 0票数 1

我正在尝试使用使用Java的斑马标签打印机,我遇到了一些问题:

我正在尝试以下代码,其中包含从这里获取的代码:https://github.com/w3blogfr/zebra-zpl

代码语言:javascript
复制
    public static void printZpl(String zpl, String printerName) throws ZebraPrintException {
        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(printerName.toLowerCase()) >= 0) {
                    psZebra = services[i];
                    break;
                }
            }

            if (psZebra == null) {
                throw new ZebraPrintNotFoundException("Zebra printer not found : " + printerName);
            }
            DocPrintJob job = psZebra.createPrintJob();

            byte[] by = zpl.getBytes();
            DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
            Doc doc = new SimpleDoc(by, flavor, null);
            job.print(doc, null);
        } catch (PrintException e) {
            throw new ZebraPrintException("Cannot print label on this printer : " + printerName, e);
        }
    }

我要打印的字符串是:

代码语言:javascript
复制
^XA
^MMT
^FT0,110^FH\^FDTestString^FS
^XZ

问题是,虽然我能够连接到打印机并打印一些东西,但输出是以纯文本形式打印的,就好像打印机没有识别出一个ZDL命令一样。

该打印机是斑马ZD220,我已经连接到我的Mac作为通用标签打印机,并选择Zebra作为软件来使用它。

我有什么好怀念的吗?

请注意,我已经能够使用NodeJS (所以我知道这是工作的)使用节点打印机,所以我知道它应该能工作,但是使用Java,我似乎不能让打印机理解我给它ZPL命令,它会打印普通字符。

EN

回答 1

Stack Overflow用户

发布于 2022-03-05 16:17:56

我通过使用ZPL打印机创建套接字连接并将ZPL内容写入输出流来实现这一工作。有关更多细节,请参阅https://github.com/khpraful/ZPLPrint/blob/master/src/ZebraPrinter.java。我尝试使用本地安装的ZPL打印仿真器。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70748317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档