deason 6 jaren geleden
bovenliggende
commit
93f332912f

+ 25 - 8
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/utils/ElectronUtils.java

@@ -12,6 +12,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.BufferedReader;
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStreamReader;
 
@@ -27,28 +28,44 @@ public class ElectronUtils {
     /**
      * 将URL响应内容转为PDF文件
      */
-    public static boolean toPdf(String profile, String cmdDir, String url, String path) {
+    public static boolean toPdf(String profile, String cmdDir, String targetUrl, String pdfPath) {
         if (StringUtils.isBlank(profile)) {
-            log.warn("spring.profiles.active must be not empty.");
+            log.warn("[toPdf] spring.profiles.active must be not empty.");
             return false;
         }
 
-        if (StringUtils.isBlank(url) || StringUtils.isBlank(path)) {
-            log.warn("Url or Path must be not empty.");
+        if (StringUtils.isBlank(targetUrl)) {
+            log.warn("[toPdf] targetUrl must be not empty.");
             return false;
         }
 
+        if (StringUtils.isBlank(pdfPath)) {
+            log.warn("[toPdf] pdfPath must be not empty.");
+            return false;
+        }
+
+        if (StringUtils.isBlank(cmdDir)) {
+            log.warn("[toPdf] cmdDir must be not empty.");
+            return false;
+        }
+
+        File dir = new File(cmdDir);
+        if (!dir.exists()) {
+            FileUtils.makeDirs(cmdDir);
+        }
+        final String parentDir = dir.getParent();
+
         if (isWindows()) {
             //Windows带盘符路径,去掉首个"/"字符
-            if (path.indexOf(":") > 0 && path.startsWith("/")) {
-                path = path.replaceFirst("/", "");
+            if (pdfPath.indexOf(":") > 0 && pdfPath.startsWith("/")) {
+                pdfPath = pdfPath.replaceFirst("/", "");
             }
 
-            String command = String.format("cmd /c electron-pdf %s %s -p A3 -l true -e view-ready", url, path);
+            String command = String.format("cmd /c electron-pdf %s %s -p A3 -l true -e view-ready", targetUrl, pdfPath);
             return executeCommand(command, "GBK");
         } else {
             //Linux
-            String command = String.format("%s/electron-pdf-%s.sh %s %s", cmdDir, profile, url, path);
+            String command = String.format("%s/electron-pdf-%s.sh %s %s", parentDir, profile, targetUrl, pdfPath);
             return executeCommand(command, "UTF-8");
         }
     }

+ 6 - 3
examcloud-core-print-starter/src/main/java/cn/com/qmth/examcloud/core/print/controller/IndexController.java

@@ -13,6 +13,7 @@ import cn.com.qmth.examcloud.core.print.common.utils.FileUtils;
 import cn.com.qmth.examcloud.core.print.service.CourseStatisticService;
 import cn.com.qmth.examcloud.core.print.service.PrintingProjectService;
 import cn.com.qmth.examcloud.core.print.service.PrintingProjectStatisticService;
+import cn.com.qmth.examcloud.web.support.Naked;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -34,6 +35,7 @@ public class IndexController {
     @Autowired
     private SystemProperty systemProperty;
 
+    @Naked
     @ResponseBody
     @RequestMapping(value = "/init", method = RequestMethod.GET)
     public void init() {
@@ -42,15 +44,16 @@ public class IndexController {
         printingProjectStatisticService.initAllPrintingProjectStatistic();
     }
 
+    @Naked
     @ResponseBody
     @RequestMapping(value = "/test", method = RequestMethod.GET)
     public void demo() {
         final String rootDir = systemProperty.getTempDir();
         FileUtils.makeDirs(rootDir);
 
-        String url = "http://baidu.com";
-        String path = rootDir + "/test.pdf";
-        ElectronUtils.toPdf(systemProperty.getProfile(), systemProperty.getDir(), url, path);
+        String targetUrl = "http://baidu.com";
+        String pdfPath = rootDir + "/test.pdf";
+        ElectronUtils.toPdf(systemProperty.getProfile(), systemProperty.getDir(), targetUrl, pdfPath);
     }
 
 }