deason 6 gadi atpakaļ
vecāks
revīzija
af91b55e82

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

@@ -14,7 +14,6 @@ import org.slf4j.LoggerFactory;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
-import java.util.Arrays;
 
 /**
  * @author: fengdesheng
@@ -28,37 +27,35 @@ public class ElectronUtils {
     /**
      * 将URL响应内容转为PDF文件
      */
-    public static boolean toPdf(String url, String path) {
+    public static boolean toPdf(String cmdDir, String url, String path) {
         if (StringUtils.isBlank(url) || StringUtils.isBlank(path)) {
             return false;
         }
 
-        final String script = "electron-pdf %s %s -p A3 -l true -e view-ready";
         if (isWindows()) {
             //Windows带盘符路径,去掉首个"/"字符
             if (path.indexOf(":") > 0 && path.startsWith("/")) {
                 path = path.replaceFirst("/", "");
             }
-            String cmd = WIN_PREFIX + String.format(script, url, path);
-            return executeCommand(new String[]{cmd}, "GBK");
+            final String script = "electron-pdf %s %s -p A3 -l true -e view-ready";
+            String command = WIN_PREFIX + String.format(script, url, path);
+            return executeCommand(command, "GBK");
         } else {
-            String prefix = "export DISPLAY=':99.0' && Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &";
-            String cdDir = "cd /usr/local/nodejs/node_global/bin";
-            String cmd = LINUX_PREFIX + String.format(script, url, path);
-            String[] commands = new String[]{"/bin/sh", "-c", cdDir + " && " + cmd};
-            log.debug(Arrays.toString(commands));
-            return executeCommand(commands, "UTF-8");
+            //Linux
+            final String script = "%s/electron-pdf-A3.sh %s %s";
+            String command = String.format(script, cmdDir, url, path);
+            return executeCommand(command, "UTF-8");
         }
     }
 
     /**
      * 执行命令
      */
-    private static boolean executeCommand(String[] commands, String charsetName) {
+    private static boolean executeCommand(String command, String charsetName) {
         Process process;
 
         try {
-            process = Runtime.getRuntime().exec(commands);
+            process = Runtime.getRuntime().exec(command);
         } catch (IOException e) {
             log.error(e.getMessage());
             return false;

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

@@ -112,8 +112,8 @@ public class CoursePaperServiceImpl implements CoursePaperService {
         final String paperPdfPath = rootDir + "/" + FileUtils.randomUUID() + SUFFIX_PDF;
         final String answerPdfPath = rootDir + "/" + FileUtils.randomUUID() + SUFFIX_PDF;
         FileUtils.makeDirs(rootDir);
-        ElectronUtils.toPdf(paperHtmlUrl, paperPdfPath);
-        ElectronUtils.toPdf(answerHtmlUrl, answerPdfPath);
+        ElectronUtils.toPdf(systemProperty.getDir(), paperHtmlUrl, paperPdfPath);
+        ElectronUtils.toPdf(systemProperty.getDir(), answerHtmlUrl, answerPdfPath);
 
         //上传PDF至又拍云
         String paperPdfUrl = upYunClient.upload(new File(paperPdfPath));

+ 4 - 0
examcloud-core-print-starter/shell/electron-pdf-A3.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/src/main/java/cn/com/qmth/examcloud/core/print/controller/IndexController.java

@@ -53,7 +53,7 @@ public class IndexController {
     public void demo() {
         String url = "http://baidu.com";
         String path = systemProperty.getTempDir() + "/test.pdf";
-        ElectronUtils.toPdf(url, path);
+        ElectronUtils.toPdf(systemProperty.getDir(), url, path);
     }
 
 }