我的打印机是 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..");
}
最佳答案
也许不是最好的答案,但我最近就是这样做的。 我将打印机连接到装有 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 应该以相同的方式工作。
关于java - 使用 Java 打印 Zebra ZM400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16935755/