Kaynağa Gözat

Merge branch 'master' of http://116.63.33.55/git/read_opc

Zt 1 yıl önce
ebeveyn
işleme
a6fc5f393a

+ 17 - 0
PrintServer/src/main/java/com/jd/printserver/controller/PrintServerController.java

@@ -26,7 +26,9 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
+import javax.print.PrintService;
 import javax.servlet.http.HttpServletRequest;
+import java.awt.print.PrinterJob;
 import java.io.File;
 import java.io.IOException;
 import java.util.*;
@@ -98,6 +100,21 @@ public class PrintServerController extends BaseController {
     }
 
     /**
+     * 查询打印机名称的列表
+     * @return 名称列表
+     */
+    @RequestMapping(value = "/getPrintName")
+    @CrossOrigin
+    public AjaxResult getPrintName(){
+        List<String> list = new ArrayList<>();
+        for (PrintService ps : PrinterJob.lookupPrintServices()) {
+            String printName = ps.toString();
+            list.add(printName);
+        }
+        return success(list);
+    }
+
+    /**
      *
      * @param request 获取客户端IP
      * @param excelUrl 报表地址。使用id时,excelUrl无效。id或excelUrl必须有一项必填

+ 9 - 0
PrintServer/src/main/java/com/jd/printserver/javafx/entity/PrintParam.java

@@ -14,6 +14,11 @@ public class PrintParam {
     private String jobName = "EI Auto Print Job";
 
     /**
+     * 打印机名称
+     */
+    private String printName = "";
+
+    /**
      * (print)
      * 打印方向  0 自动(停用) 1 横向 2 纵向
      */
@@ -121,6 +126,10 @@ public class PrintParam {
 
     public void setNullToDefault(){
         PrintParam defaultPrintParam = new PrintParam();
+        if(StrUtil.isEmpty(this.printName)){
+            this.printName = defaultPrintParam.getPrintName();
+        }
+
         if(StrUtil.isEmpty(this.jobName)){
             this.jobName = defaultPrintParam.getJobName();
         }

+ 31 - 9
PrintServer/src/main/java/com/jd/printserver/utils/PrintUtils.java

@@ -133,20 +133,42 @@ public class PrintUtils {
 
             PrinterJob printJob = PrinterJob.getPrinterJob();
             printJob.setJobName(printParam.getJobName());
-            //默认打印机
-            PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
+//            //默认打印机
+//            PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
+//
+//            if (printService != null) {
+//                try {
+//                    printJob.setPrintService(printService);
+//                } catch (PrinterException e) {
+//                    log.error("打印失败,打印机不可用,请检查");
+//                    return false;
+//                }
+//            } else {
+//                log.error("打印失败,未找到打印机,请检查");
+//                return false;
+//            }
 
-            if (printService != null) {
-                try {
-                    printJob.setPrintService(printService);
-                } catch (PrinterException e) {
-                    log.error("打印失败,打印机不可用,请检查");
-                    return false;
+            // 遍历所有打印机的名称
+            boolean isChoose = false;
+            for (PrintService printService : PrinterJob.lookupPrintServices()) {
+                String psName = printService.toString();
+                // 选用指定打印机
+                if (psName.equals(printParam.getPrintName())) {
+                    isChoose = true;
+                    try {
+                        printJob.setPrintService(printService);
+                    } catch (PrinterException e) {
+                        log.error("打印失败,打印机不可用,请检查");
+                        return false;
+                    }
+                    break;
                 }
-            } else {
+            }
+            if(!isChoose){
                 log.error("打印失败,未找到打印机,请检查");
                 return false;
             }
+
             // 设置纸张及缩放
             // ACTUAL_SIZE 以100%比例打印图像
             // SHRINK_TO_FIT 如果需要,缩小图像以适应页面

+ 5 - 0
cqcy-ei/cqcy-ei-common/src/main/java/com/example/opc_common/entity/PrintConfig.java

@@ -19,6 +19,11 @@ public class PrintConfig {
     private String jobName = "EI Auto Print Job";
 
     /**
+     * 打印机名称
+     */
+    private String printName = "";
+
+    /**
      * 打印方向  0 自动 1 横向 2 纵向
      */
     private Integer orientation = 0;

+ 5 - 4
cqcy-ei/cqcy-ei-da/src/main/resources/mapper/PrintDao.xml

@@ -9,7 +9,7 @@
 
     <sql id="printConfig">
         id
-        ,job_name,orientation,copies,page_type, width,height,margin_left,margin_top,margin_right,margin_bottom,
+        ,print_name,job_name,orientation,copies,page_type, width,height,margin_left,margin_top,margin_right,margin_bottom,
         unit,sides,chromaticity,fit_to_pages,scale,page_range,page_range_start,page_range_end,print_area,print_area_value,create_time
     </sql>
 
@@ -20,11 +20,11 @@
     </insert>
 
     <insert id="addPrintConfig">
-        insert into t_print_config (job_name, orientation, copies, page_type, width, height, margin_left, margin_top,
+        insert into t_print_config (print_name,job_name, orientation, copies, page_type, width, height, margin_left, margin_top,
                                     margin_right, margin_bottom,
                                     unit, sides, chromaticity, fit_to_pages, scale, page_range, page_range_start,
                                     page_range_end, print_area, print_area_value, create_time)
-            value (#{jobName},#{orientation},#{copies},#{pageType},#{width},#{height},#{marginLeft},#{marginTop},
+            value (#{printName},#{jobName},#{orientation},#{copies},#{pageType},#{width},#{height},#{marginLeft},#{marginTop},
             #{marginRight},#{marginBottom},#{unit},#{sides},#{chromaticity},#{fitToPages},#{scale},#{pageRange},
             #{pageRangeStart},#{pageRangeEnd},#{printArea},#{printAreaValue},now())
     </insert>
@@ -46,7 +46,8 @@
 
     <update id="updatePrintConfig">
         update t_print_config
-        set job_name=#{jobName},
+        set print_name=#{printName},
+            job_name=#{jobName},
             orientation=#{orientation},
             page_type=#{pageType},
             width=#{width},