浏览代码

打印机参数动态传入

zhuhao 2 年之前
父节点
当前提交
6490446ebe

+ 72 - 34
imcs-admin-boot/imcs-business-biz/src/main/java/com/github/zuihou/business/util/ZplPrinter.java

@@ -1,4 +1,5 @@
 package com.github.zuihou.business.util;
+
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.slf4j.Logger;
@@ -32,6 +33,29 @@ public class ZplPrinter {
     private static String end = "^XZ";     //标签格式以^XZ结束
     private static String content = "";
 
+    /**
+     * 斑马打印功能
+     * @param serviceUrl==》打印机服务地址
+     * @param materialCode ==》物料编码
+     * @return
+     */
+    public static boolean printer(String serviceUrl, String materialCode) {
+        ZplPrinter p = new ZplPrinter();
+        printerURI = serviceUrl;
+
+        //下边的条码
+        String bar2 = materialCode;//20位
+        String bar2Paper = "^FO380,600^BY3,3.0,100^BCN,,Y,N,N^FD${data}^FS";//条码样式模板
+        p.setBarcode(bar2, bar2Paper);
+
+        p.setText("云箭集团", 380, 260, 60, 60, 30, 2, 2, 24);
+        p.setText("物料编码", 380, 485, 56, 56, 30, 2, 2, 24);
+        p.setChar(materialCode, 500, 560, 40, 40);
+
+        String zpl2 = p.getZpl();
+        return p.print(zpl2);
+    }
+
     public static void main(String[] args) {
         //ZplPrinter p = new ZplPrinter("\\\\192.168.11.247\\ZDesigner ZT210-200dpi ZPL");
         ZplPrinter p = new ZplPrinter();
@@ -50,11 +74,11 @@ public class ZplPrinter {
         String bar1 = "07";
         p.setChar(bar1, 190, 130, 60, 60);
         String bar1Zpl = "^FO100,200^BY8,3.0,240^BCR,,N,N,N^FD${data}^FS";//条码样式模板
-        p.setBarcode(bar1,bar1Zpl);
+        p.setBarcode(bar1, bar1Zpl);
         //下边的条码
         String bar2 = "00000999990018822969";//20位
         String bar2Paper = "^FO380,600^BY3,3.0,100^BCN,,Y,N,N^FD${data}^FS";//条码样式模板
-        p.setBarcode(bar2,bar2Paper);
+        p.setBarcode(bar2, bar2Paper);
 
         p.setText("云箭集团", 380, 260, 60, 60, 30, 2, 2, 24);
 
@@ -69,11 +93,11 @@ public class ZplPrinter {
     /**
      * 构造方法     *
      */
-    public static void init(){
+    public static void init() {
         //加载字体
-        String relativelyPath=System.getProperty("user.dir");
+        String relativelyPath = System.getProperty("user.dir");
         File file = new File(relativelyPath + "//ts24.lib");
-        if(file.exists()){
+        if (file.exists()) {
             FileInputStream fis;
             try {
                 fis = new FileInputStream(file);
@@ -83,7 +107,7 @@ public class ZplPrinter {
             } catch (IOException e) {
                 e.printStackTrace();
             }
-        }else{
+        } else {
             System.out.println("ts24.lib文件不存在");
         }
 
@@ -98,7 +122,7 @@ public class ZplPrinter {
         ZplPrinter.setChar("78787878788", 500, 560, 40, 40);
 
         //初始化打印机
-        PrintService[] services = PrintServiceLookup.lookupPrintServices(null,null);
+        PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
         if (services != null && services.length > 0) {
             for (PrintService service : services) {
                 if (printerURI.equals(service.getName())) {
@@ -108,44 +132,47 @@ public class ZplPrinter {
             }
         }
         if (printService == null) {
-            System.out.println("没有找到打印机:["+printerURI+"]");
+            System.out.println("没有找到打印机:[" + printerURI + "]");
             //循环出所有的打印机
             if (services != null && services.length > 0) {
                 System.out.println("可用的打印机列表:");
                 for (PrintService service : services) {
-                    System.out.println("["+service.getName()+"]");
+                    System.out.println("[" + service.getName() + "]");
                 }
             }
-        }else{
-            System.out.println("找到打印机:["+printerURI+"]");
-            System.out.println("打印机名称:["+printService.getAttribute(PrinterName.class).getValue()+"]");
+        } else {
+            System.out.println("找到打印机:[" + printerURI + "]");
+            System.out.println("打印机名称:[" + printService.getAttribute(PrinterName.class).getValue() + "]");
         }
 
     }
+
     /**
      * 设置条形码
+     *
      * @param barcode 条码字符
-     * @param zpl 条码样式模板
+     * @param zpl     条码样式模板
      */
-    public static void setBarcode(String barcode,String zpl) {
+    public static void setBarcode(String barcode, String zpl) {
         content += zpl.replace("${data}", barcode);
     }
 
     /**
      * 中文字符、英文字符(包含数字)混合
+     *
      * @param str 中文、英文
-     * @param x x坐标
-     * @param y y坐标
-     * @param eh 英文字体高度height
-     * @param ew 英文字体宽度width
-     * @param es 英文字体间距spacing
-     * @param mx 中文x轴字体图形放大倍率。范围1-10,默认1
-     * @param my 中文y轴字体图形放大倍率。范围1-10,默认1
-     * @param ms 中文字体间距。24是个比较合适的值。
+     * @param x   x坐标
+     * @param y   y坐标
+     * @param eh  英文字体高度height
+     * @param ew  英文字体宽度width
+     * @param es  英文字体间距spacing
+     * @param mx  中文x轴字体图形放大倍率。范围1-10,默认1
+     * @param my  中文y轴字体图形放大倍率。范围1-10,默认1
+     * @param ms  中文字体间距。24是个比较合适的值。
      */
     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;) {
+        for (int off = 0; off < ch.length; ) {
             if (((int) ch[off] & 0x00ff) >= 0xA0) {
                 int qcode = ch[off] & 0xff;
                 int wcode = ch[off + 1] & 0xff;
@@ -169,35 +196,42 @@ public class ZplPrinter {
             }
         }
     }
+
     /**
      * 英文字符串(包含数字)
+     *
      * @param str 英文字符串
-     * @param x x坐标
-     * @param y y坐标
-     * @param h 高度
-     * @param w 宽度
+     * @param x   x坐标
+     * @param y   y坐标
+     * @param h   高度
+     * @param 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";
     }
+
     /**
      * 英文字符(包含数字)顺时针旋转90度
+     *
      * @param str 英文字符串
-     * @param x x坐标
-     * @param y y坐标
-     * @param h 高度
-     * @param w 宽度
+     * @param x   x坐标
+     * @param y   y坐标
+     * @param h   高度
+     * @param 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 static String getZpl() {
         return begin + content + end;
     }
+
     /**
      * 重置ZPL指令,当需要打印多张纸的时候需要调用。
      */
@@ -206,13 +240,15 @@ public class ZplPrinter {
         end = "^XZ";
         content = "";
     }
+
     /**
      * 打印
+     *
      * @param zpl 完整的ZPL
      */
-    public static boolean print(String zpl){
-        if(printService==null){
-            System.out.println("打印出错:没有找到打印机:["+printerURI+"]");
+    public static boolean print(String zpl) {
+        if (printService == null) {
+            System.out.println("打印出错:没有找到打印机:[" + printerURI + "]");
             return false;
         }
         DocPrintJob job = printService.createPrintJob();
@@ -228,8 +264,10 @@ public class ZplPrinter {
             return false;
         }
     }
+
     /**
      * 字符串转byte[]
+     *
      * @param s
      * @return
      */