使用 Java 打印 Zebra ZM400
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16935755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Print Zebra ZM400 using Java
提问by Raymond
my printer is Zebra ZM400 label printer and it's connected to one of the pc (connected with USB) in the network.
我的打印机是 Zebra ZM400 标签打印机,它连接到网络中的一台电脑(通过 USB 连接)。
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?
如何从网络连接该打印机并从 Java 应用程序打印标签?
I know I've to use ZPL langauage but I don't know how to make connection and send command to label printer.
我知道我必须使用 ZPL 语言,但我不知道如何建立连接并向标签打印机发送命令。
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..
我使用norbi771的方法..但是当它发送命令时,只是空白出来..
my label's dimension is 3.25" x 3.75"..
我的标签尺寸是 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..");
}
回答by norbi771
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):
也许不是最好的答案,但我最近就是这样做的。我使用 Windows 将打印机连接到 PC。然后我共享了打印机。然后我通过简单的命令将这个共享打印机映射到 LPT1(所有这些都可以在一台 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
因为现在这个 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");
}
}
The example provided is for EPL printing, but ZPL should work the same way.
提供的示例用于 EPL 打印,但 ZPL 应该以相同的方式工作。
回答by jason.zissman
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.
Zebra 确实在 www.zebra.com/link 上提供了 Java API。它没有声称支持 ZM400,但值得研究 20 分钟。如果它不支持该模型,我会感到惊讶,因为所有支持的打印机都使用 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:
对于您的 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
回答by Thibaut
For people who need some help to generate zpl directly on java, I make utils library.
对于需要帮助直接在 java 上生成 zpl 的人,我制作了 utils 库。
回答by Pankaj
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
}
}
回答by Marcus Tobias
You can simply print ZPL labels with javax.print. Don't matter if it's connected over USB or network.
您可以使用 javax.print 简单地打印 ZPL 标签。不管它是通过 USB 还是网络连接。
http://docs.oracle.com/javase/7/docs/api/javax/print/package-summary.html
http://docs.oracle.com/javase/7/docs/api/javax/print/package-summary.html
Lookup your printer by the name, set DocFlavor to AUTOSENSE, put your ZPL-Label into the SimpleDoc and print the job.
按名称查找您的打印机,将 DocFlavor 设置为 AUTOSENSE,将您的 ZPL-Label 放入 SimpleDoc 并打印作业。
回答by Dimitry K
Keep in mind that according to Zebras website, ZM400 runs firmware which either supports ZPL or EPL but not both at the same time.
请记住,根据 Zebras 网站,ZM400 运行的固件支持 ZPL 或 EPL,但不能同时支持两者。
https://km.zebra.com/kb/index?page=content&id=SO8156&actp=LIST
https://km.zebra.com/kb/index?page=content&id=SO8156&actp=LIST