|
|
@@ -1,4 +1,12 @@
|
|
|
package com.github.zuihou.business.util;
|
|
|
+import lombok.Data;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
@@ -14,15 +22,20 @@ import javax.print.SimpleDoc;
|
|
|
import javax.print.attribute.standard.PrinterName;
|
|
|
|
|
|
public class ZplPrinter {
|
|
|
- private String printerURI = null;//打印机完整路径
|
|
|
- private PrintService printService = null;//打印机服务
|
|
|
- private byte[] dotFont;
|
|
|
- private String begin = "^XA"; //标签格式以^XA开始
|
|
|
- private String end = "^XZ"; //标签格式以^XZ结束
|
|
|
- private String content = "";
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(ZplPrinter.class);
|
|
|
+
|
|
|
+ //@Value("${zuihou.print.printCodePath}")
|
|
|
+ private static String printerURI;//打印机完整路径
|
|
|
+ private static PrintService printService;//打印机服务
|
|
|
+ private static byte[] dotFont;
|
|
|
+ private static String begin = "^XA"; //标签格式以^XA开始
|
|
|
+ private static String end = "^XZ"; //标签格式以^XZ结束
|
|
|
+ private static String content = "";
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
- ZplPrinter p = new ZplPrinter("\\\\192.168.0.12\\ZDesigner 105SLPlus-300dpi ZPL");
|
|
|
+ //ZplPrinter p = new ZplPrinter("\\\\192.168.11.247\\ZDesigner ZT210-200dpi ZPL");
|
|
|
+ ZplPrinter p = new ZplPrinter();
|
|
|
+ printerURI = "\\\\192.168.11.247\\ZDesigner ZT210-200dpi ZPL";
|
|
|
//1.打印单个条码
|
|
|
String bar0 = "1234567890";//条码内容
|
|
|
String bar0Zpl = "^FO110,110^BY6,3.0,280^BCN,,Y,N,N^FD${data}^FS";//条码样式模板
|
|
|
@@ -54,11 +67,9 @@ public class ZplPrinter {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 构造方法
|
|
|
- * @param printerURI 打印机路径
|
|
|
+ * 构造方法 *
|
|
|
*/
|
|
|
- public ZplPrinter(String printerURI){
|
|
|
- this.printerURI = printerURI;
|
|
|
+ public static void init(){
|
|
|
//加载字体
|
|
|
String relativelyPath=System.getProperty("user.dir");
|
|
|
File file = new File(relativelyPath + "//ts24.lib");
|
|
|
@@ -75,6 +86,17 @@ public class ZplPrinter {
|
|
|
}else{
|
|
|
System.out.println("ts24.lib文件不存在");
|
|
|
}
|
|
|
+
|
|
|
+ printerURI = "\\\\192.168.11.247\\ZDesigner ZT210-200dpi ZPL";
|
|
|
+ //1.打印单个条码
|
|
|
+ String bar0 = "1234567890";//条码内容
|
|
|
+ String bar0Zpl = "^FO110,110^BY6,3.0,280^BCN,,Y,N,N^FD${data}^FS";//条码样式模板
|
|
|
+ ZplPrinter.setBarcode(bar0, bar0Zpl);
|
|
|
+ ZplPrinter.setText("云箭集团", 380, 260, 60, 60, 30, 2, 2, 24);
|
|
|
+
|
|
|
+ ZplPrinter.setText("品类:前格框", 380, 485, 56, 56, 30, 2, 2, 24);
|
|
|
+ ZplPrinter.setChar("78787878788", 500, 560, 40, 40);
|
|
|
+
|
|
|
//初始化打印机
|
|
|
PrintService[] services = PrintServiceLookup.lookupPrintServices(null,null);
|
|
|
if (services != null && services.length > 0) {
|
|
|
@@ -98,13 +120,14 @@ public class ZplPrinter {
|
|
|
System.out.println("找到打印机:["+printerURI+"]");
|
|
|
System.out.println("打印机名称:["+printService.getAttribute(PrinterName.class).getValue()+"]");
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
/**
|
|
|
* 设置条形码
|
|
|
* @param barcode 条码字符
|
|
|
* @param zpl 条码样式模板
|
|
|
*/
|
|
|
- public void setBarcode(String barcode,String zpl) {
|
|
|
+ public static void setBarcode(String barcode,String zpl) {
|
|
|
content += zpl.replace("${data}", barcode);
|
|
|
}
|
|
|
|
|
|
@@ -120,7 +143,7 @@ public class ZplPrinter {
|
|
|
* @param my 中文y轴字体图形放大倍率。范围1-10,默认1
|
|
|
* @param ms 中文字体间距。24是个比较合适的值。
|
|
|
*/
|
|
|
- public void setText(String str, int x, int y, int eh, int ew, int es, int mx, int my, int ms) {
|
|
|
+ public static void setText(String str, int x, int y, int eh, int ew, int es, int mx, int my, int ms) {
|
|
|
byte[] ch = str2bytes(str);
|
|
|
for (int off = 0; off < ch.length;) {
|
|
|
if (((int) ch[off] & 0x00ff) >= 0xA0) {
|
|
|
@@ -154,7 +177,7 @@ public class ZplPrinter {
|
|
|
* @param h 高度
|
|
|
* @param w 宽度
|
|
|
*/
|
|
|
- public void setChar(String str, int x, int y, int h, int w) {
|
|
|
+ public static void setChar(String str, int x, int y, int h, int w) {
|
|
|
content += "^FO" + x + "," + y + "^A0," + h + "," + w + "^FD" + str + "^FS";
|
|
|
}
|
|
|
/**
|
|
|
@@ -165,20 +188,20 @@ public class ZplPrinter {
|
|
|
* @param h 高度
|
|
|
* @param w 宽度
|
|
|
*/
|
|
|
- public void setCharR(String str, int x, int y, int h, int w) {
|
|
|
+ public static void setCharR(String str, int x, int y, int h, int w) {
|
|
|
content += "^FO" + x + "," + y + "^A0R," + h + "," + w + "^FD" + str + "^FS";
|
|
|
}
|
|
|
/**
|
|
|
* 获取完整的ZPL
|
|
|
* @return
|
|
|
*/
|
|
|
- public String getZpl() {
|
|
|
+ public static String getZpl() {
|
|
|
return begin + content + end;
|
|
|
}
|
|
|
/**
|
|
|
* 重置ZPL指令,当需要打印多张纸的时候需要调用。
|
|
|
*/
|
|
|
- public void resetZpl() {
|
|
|
+ public static void resetZpl() {
|
|
|
begin = "^XA";
|
|
|
end = "^XZ";
|
|
|
content = "";
|
|
|
@@ -187,7 +210,7 @@ public class ZplPrinter {
|
|
|
* 打印
|
|
|
* @param zpl 完整的ZPL
|
|
|
*/
|
|
|
- public boolean print(String zpl){
|
|
|
+ public static boolean print(String zpl){
|
|
|
if(printService==null){
|
|
|
System.out.println("打印出错:没有找到打印机:["+printerURI+"]");
|
|
|
return false;
|
|
|
@@ -210,7 +233,7 @@ public class ZplPrinter {
|
|
|
* @param s
|
|
|
* @return
|
|
|
*/
|
|
|
- private byte[] str2bytes(String s) {
|
|
|
+ private static byte[] str2bytes(String s) {
|
|
|
if (null == s || "".equals(s)) {
|
|
|
return null;
|
|
|
}
|