我正在尝试使用使用Java的斑马标签打印机,我遇到了一些问题:
我正在尝试以下代码,其中包含从这里获取的代码:https://github.com/w3blogfr/zebra-zpl
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);
}
}我要打印的字符串是:
^XA
^MMT
^FT0,110^FH\^FDTestString^FS
^XZ问题是,虽然我能够连接到打印机并打印一些东西,但输出是以纯文本形式打印的,就好像打印机没有识别出一个ZDL命令一样。
该打印机是斑马ZD220,我已经连接到我的Mac作为通用标签打印机,并选择Zebra作为软件来使用它。
我有什么好怀念的吗?
请注意,我已经能够使用NodeJS (所以我知道这是工作的)使用节点打印机,所以我知道它应该能工作,但是使用Java,我似乎不能让打印机理解我给它ZPL命令,它会打印普通字符。
发布于 2022-03-05 16:17:56
我通过使用ZPL打印机创建套接字连接并将ZPL内容写入输出流来实现这一工作。有关更多细节,请参阅https://github.com/khpraful/ZPLPrint/blob/master/src/ZebraPrinter.java。我尝试使用本地安装的ZPL打印仿真器。
https://stackoverflow.com/questions/70748317
复制相似问题