Procházet zdrojové kódy

update electron pdf cmd.

deason před 6 roky
rodič
revize
5cd750ec07

+ 13 - 0
examcloud-core-print-common/src/main/java/cn/com/qmth/examcloud/core/print/common/upyun/SystemProperty.java

@@ -21,6 +21,11 @@ import java.io.Serializable;
 @Component
 public class SystemProperty implements Serializable {
     private static final long serialVersionUID = 1L;
+    /**
+     * 配置文件环境
+     */
+    @Value("${spring.profiles.active}")
+    private String profile;
     /**
      * 文件存放目录
      */
@@ -48,4 +53,12 @@ public class SystemProperty implements Serializable {
         this.tempDir = tempDir;
     }
 
+    public String getProfile() {
+        return profile;
+    }
+
+    public void setProfile(String profile) {
+        this.profile = profile;
+    }
+
 }

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

@@ -27,8 +27,14 @@ public class ElectronUtils {
     /**
      * 将URL响应内容转为PDF文件
      */
-    public static boolean toPdf(String cmdDir, String url, String path) {
+    public static boolean toPdf(String profile, String cmdDir, String url, String path) {
+        if (StringUtils.isBlank(profile)) {
+            log.warn("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.");
             return false;
         }
 
@@ -37,13 +43,12 @@ public class ElectronUtils {
             if (path.indexOf(":") > 0 && path.startsWith("/")) {
                 path = path.replaceFirst("/", "");
             }
-            final String script = "electron-pdf %s %s -p A3 -l true -e view-ready";
-            String command = WIN_PREFIX + String.format(script, url, path);
+
+            String command = String.format("cmd /c electron-pdf %s %s -p A3 -l true -e view-ready", url, path);
             return executeCommand(command, "GBK");
         } else {
             //Linux
-            final String script = "%s/electron-pdf-A3.sh %s %s";
-            String command = String.format(script, cmdDir, url, path);
+            String command = String.format("%s/electron-pdf-%s.sh %s %s", cmdDir, profile, url, path);
             return executeCommand(command, "UTF-8");
         }
     }
@@ -52,8 +57,9 @@ public class ElectronUtils {
      * 执行命令
      */
     private static boolean executeCommand(String command, String charsetName) {
-        Process process;
+        log.debug("Execute Command:" + command);
 
+        Process process;
         try {
             process = Runtime.getRuntime().exec(command);
         } catch (IOException e) {

+ 2 - 2
examcloud-core-print-service/src/main/java/cn/com/qmth/examcloud/core/print/service/impl/CoursePaperServiceImpl.java

@@ -160,13 +160,13 @@ public class CoursePaperServiceImpl implements CoursePaperService {
             final String answerPdfPath = rootDir + "/" + FileUtils.randomUUID() + SUFFIX_PDF;
             FileUtils.makeDirs(rootDir);
 
-            boolean paperResult = ElectronUtils.toPdf(systemProperty.getDir(), coursePaper.getPaperHtmlUrl(), paperPdfPath);
+            boolean paperResult = ElectronUtils.toPdf(systemProperty.getProfile(), systemProperty.getDir(), coursePaper.getPaperHtmlUrl(), paperPdfPath);
             if (!paperResult) {
                 log.warn(String.format("试卷页面转换PDF文件失败!paperId = %s", coursePaper.getPaperId()));
                 continue;
             }
 
-            boolean answerResult = ElectronUtils.toPdf(systemProperty.getDir(), coursePaper.getAnswerHtmlUrl(), answerPdfPath);
+            boolean answerResult = ElectronUtils.toPdf(systemProperty.getProfile(), systemProperty.getDir(), coursePaper.getAnswerHtmlUrl(), answerPdfPath);
             if (!answerResult) {
                 log.warn(String.format("答案页面转换PDF文件失败!paperId = %s", coursePaper.getPaperId()));
                 continue;

+ 0 - 0
examcloud-core-print-starter/shell/electron-pdf-A3.sh → examcloud-core-print-starter/shell/electron-pdf-dev.sh


+ 3 - 0
examcloud-core-print-starter/shell/electron-pdf-prod.sh

@@ -0,0 +1,3 @@
+export DISPLAY=':99.0'
+Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
+electron-pdf $1 $2 -p A3 -l true -e view-ready

+ 4 - 0
examcloud-core-print-starter/shell/electron-pdf-test.sh

@@ -0,0 +1,4 @@
+export DISPLAY=':99.0'
+Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
+cd /usr/local/nodejs/node_global/bin
+./electron-pdf $1 $2 -p A3 -l true -e view-ready

+ 1 - 1
examcloud-core-print-starter/shell/start.vmoptions

@@ -1 +1 @@
--server -Xms512m -Xmx512m
+-server -Xms1g -Xmx1g

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

@@ -9,6 +9,7 @@ package cn.com.qmth.examcloud.core.print.controller;
 
 import cn.com.qmth.examcloud.core.print.common.upyun.SystemProperty;
 import cn.com.qmth.examcloud.core.print.common.utils.ElectronUtils;
+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;
@@ -49,11 +50,14 @@ public class IndexController {
     }
 
     @ResponseBody
-    @RequestMapping(value = "/demo", method = RequestMethod.GET)
+    @RequestMapping(value = "/test", method = RequestMethod.GET)
     public void demo() {
+        final String rootDir = systemProperty.getTempDir();
+        FileUtils.makeDirs(rootDir);
+
         String url = "http://baidu.com";
-        String path = systemProperty.getTempDir() + "/test.pdf";
-        ElectronUtils.toPdf(systemProperty.getDir(), url, path);
+        String path = rootDir + "/test.pdf";
+        ElectronUtils.toPdf(systemProperty.getProfile(), systemProperty.getDir(), url, path);
     }
 
 }

+ 1 - 1
examcloud-core-print-starter/src/main/resources/security-exclusions.conf

@@ -1,6 +1,6 @@
 regexp:.*swagger.*
 [][/doc.html][GET]
-[][/demo][GET]
+[][/test][GET]
 [][/init][GET]
 [][/][GET]