Browse Source

添加执行html接口

lhy 1 year ago
parent
commit
5b86014645

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

@@ -4,9 +4,11 @@ import cn.hutool.core.convert.Convert;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.lang.Snowflake;
+import cn.hutool.core.net.NetUtil;
 import cn.hutool.core.util.IdUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.extra.servlet.ServletUtil;
+import cn.hutool.http.HttpUtil;
 import cn.hutool.json.JSONUtil;
 import com.jd.printserver.common.controller.BaseController;
 import com.jd.printserver.common.domain.AjaxResult;
@@ -26,6 +28,7 @@ import javax.print.attribute.standard.PrinterIsAcceptingJobs;
 import javax.servlet.http.HttpServletRequest;
 import java.io.File;
 import java.io.IOException;
+import java.nio.file.Path;
 import java.util.*;
 
 
@@ -504,4 +507,38 @@ public class PrintServerController extends BaseController {
         return success();
     }
 
+    @RequestMapping(value = "/printReportTable")
+    @CrossOrigin
+    public AjaxResult printReportTable(HttpServletRequest request,String httpUrl) {
+        if (StringUtils.isEmpty(httpUrl)) {
+            log.info("接口参数httpUrl缺失");
+            return error("参数缺失");
+        }
+        boolean flag = SeleniumUtils.getResultDataByUrl(httpUrl);
+        if(flag){
+            return success();
+        }
+        return error();
+    }
+
+    @RequestMapping(value = "/printPdf")
+    @CrossOrigin
+    public AjaxResult printPdf(HttpServletRequest request,String pdfUrl, PrintParam printConfig) {
+        log.info("--------------------------本地打印开始");
+        if (StringUtils.isEmpty(pdfUrl)) {
+            log.info("接口参数pdfUrl缺失");
+            return error("参数缺失");
+        }
+        File tempFile = FileUtil.file("tempDir/printPdf"); // 临时文件存储目录
+
+        HttpUtil.downloadFile(pdfUrl, tempFile);
+
+        Boolean printFlag = PrintUtils.PDFprint(tempFile.getPath(), printConfig);
+        log.info("打印返回结果:{}", printFlag);
+        log.info("--------------------------结束本地打印");
+        if(!printFlag){
+            return error("打印失败");
+        }
+        return success();
+    }
 }

+ 61 - 0
PrintServer/src/main/java/com/jd/printserver/utils/SeleniumUtils.java

@@ -35,6 +35,67 @@ public class SeleniumUtils {
      */
     private static Long maxWaitTime = Constants.JXBROWSER_CONSOLE_BASE64_WAIT_TIME;
 
+    public synchronized static boolean getResultDataByUrl(final String url){
+
+        WebDriver driver = null;
+        try {
+            log.info("初始化隐藏浏览器");
+
+            String zipFilePath = "selenium/Chrome.zip";
+            File zipFile = initChromeZipFile(zipFilePath);
+
+            log.info("Chrome.zip地址:" + zipFile.getCanonicalPath());
+
+            String outputFolder = zipFile.getParentFile().getCanonicalPath();
+            String driverPath = outputFolder +  "/Chrome/Application/chromedriver.exe";
+            File driverFile = new File(driverPath);
+            if(driverFile == null || !driverFile.exists()){
+                unzipChromeZipFile(zipFile);
+            }
+            log.info("chromedriver.exe地址:" + driverPath);
+            // 设置 Chrome 驱动的路径
+            System.setProperty("webdriver.chrome.driver", driverPath);
+            // 创建 ChromeOptions 对象,用于设置浏览器选项
+            ChromeOptions options = new ChromeOptions();
+            String chromePath = outputFolder +  "/Chrome/Application/chrome.exe";
+            log.info("chrome.exe地址:" + chromePath);
+            options.setBinary(chromePath);
+            //设置 chrome 的无头模式
+
+            options.addArguments("--headless");
+
+            options.addArguments("--disable-gpu");
+
+            options.addArguments("--no-sandbox");
+
+            options.addArguments("--disable-dev-shm-usage");
+
+            options.addArguments("--window-size=1920,1050");
+
+            options.addArguments("--start-maximized");
+            // 创建 ChromeDriver 对象,并传入 ChromeOptions 对象
+            driver = new ChromeDriver(options);
+
+            // 最大化窗口
+            driver.manage().window().maximize();
+            // 访问网页
+            driver.get(url);
+            return true;
+        }catch (Exception e){
+            log.error("内置浏览器获取网页失败", e);
+            return false;
+        }finally {
+            if(driver != null){
+                try {
+                    // 关闭浏览器
+                    driver.quit();
+                }catch (Exception e){
+                    log.error("内置浏览器关闭失败", e);
+                }
+            }
+        }
+    }
+
     public synchronized static Map<String, String> getResultDataByUrl(final String url,final String key,final String value){
 
         Map<String, String> resultData = getResultData(key,value);

+ 2 - 6
industry-system/industry-da/src/main/java/com/example/opc_da/controller/ReportTableController.java

@@ -6,6 +6,7 @@ import com.example.opc_common.entity.ReportTable;
 import com.example.opc_common.entity.TableTemplate;
 import com.example.opc_common.enums.ResultEnum;
 import com.example.opc_common.util.Blank;
+import com.example.opc_common.util.ClientUtil;
 import com.example.opc_common.util.Result;
 import com.example.opc_common.util.XlsxToPdfUtil;
 import com.example.opc_da.service.ReportTableService;
@@ -16,6 +17,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletRequest;
+import java.util.HashMap;
 import java.util.List;
 
 /**
@@ -375,10 +377,4 @@ public class ReportTableController {
     public Result xlsToPdf(PageSetupEntity pageSetupEntity) {
         return Result.ok(XlsxToPdfUtil.convert(pageSetupEntity, filePath));
     }
-
-    @PostMapping("/printPdf")
-    public Result printPdf(String pdfUrl, String clientIP, PrintConfig printConfig, String reportId) {
-
-        return Result.ok(pdfUrl);
-    }
 }

+ 18 - 14
industry-system/industry-da/src/main/resources/static/reportSheet/js/report-history.js

@@ -1,13 +1,12 @@
-layui.use(['_$','rightMenu'], function() {
+layui.use(['_$', 'rightMenu'], function() {
 	var queryParams = new URLSearchParams(window.location.search);
 	// 例如获取查询参数中的key
-	let printType = '', serverUrl = '', reportId = '', clientIP, printConfig = {}
-	printType = queryParams.get("printType");
+	let printType = queryParams.get("printType");
 	if(printType && printType === 'task'){
-		reportId = queryParams.get("id");
-		serverUrl = queryParams.get("serverUrl");
-		clientIP = queryParams.get("clientIP");
-		printConfig = JSON.parse(queryParams.get("printConfig"));
+		let reportId = queryParams.get("id");
+		let serverUrl = queryParams.get("serverUrl");
+		let clientIP = queryParams.get("clientIP");
+		let printConfig = JSON.parse(queryParams.get("printConfig"));
 		/*printConfig = {
 			"id": 1,
 			"jobName": "121",
@@ -53,16 +52,21 @@ layui.use(['_$','rightMenu'], function() {
 			layui.excelUtil.xlsxToPdf({
 				data,
 				success: (json) => {
-					layui._$.post({
-						url: '/reportTable/printPdf',
+					// 默认 printConfig:
+					// {jobName:'',width:'PRINT_A4_WIDTH,单位:mm',height:'PRINT_A4_HEIGHT,单位:mm',printName:'',orientation:1,
+					// copies:1,pageRange:1,pageRangeStart:1,pageRangeEnd:1,sides:1}
+					$.ajax({
+						type: 'POST',
+						url: "http://" + clientIP + ":8084/api/printPdf",
 						data: {
 							pdfUrl: serverUrl + "/" + json.data,
-							clientIP: clientIP,
-							printConfig: printConfig,
-							reportId: reportId
+							printConfig: printConfig
+						},
+						dataType: 'json',
+						success: function(json) {
+						},
+						error: function(err) {
 						}
-					}).then(json => {
-
 					})
 				}
 			})