deason 6 år sedan
förälder
incheckning
cebdf77ebb

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

@@ -22,6 +22,7 @@ import java.io.InputStreamReader;
 public class ElectronUtils {
     private static final Logger log = LoggerFactory.getLogger(ElectronUtils.class);
     private static final String WIN_PREFIX = "cmd /c ";
+    private static final String LINUX_PREFIX = "./";
 
     /**
      * 将URL响应内容转为PDF文件
@@ -38,22 +39,29 @@ public class ElectronUtils {
                 path = path.replaceFirst("/", "");
             }
 
-            String cmd = WIN_PREFIX + String.format(script, url, path);
+            String[] cmd = new String[]{WIN_PREFIX + String.format(script, url, path)};
             return executeCommand(cmd, "GBK");
         } else {
-            String cmd = String.format(script, url, path);
-            return executeCommand(cmd, "UTF-8");
+            String cmd = LINUX_PREFIX + String.format(script, url, path);
+            String[] commands = new String[]{
+                    "/bin/bash",
+                    "-c",
+                    "export DISPLAY=':99.0'",
+                    "Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &",
+                    "cd /usr/local/nodejs/node_global/bin", cmd
+            };
+            return executeCommand(commands, "UTF-8");
         }
     }
 
     /**
      * 执行命令
      */
-    private static boolean executeCommand(String cmd, String charsetName) {
+    private static boolean executeCommand(String[] commands, String charsetName) {
         Process process;
 
         try {
-            process = Runtime.getRuntime().exec(cmd);
+            process = Runtime.getRuntime().exec(commands);
         } catch (IOException e) {
             log.error(e.getMessage());
             return false;

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

@@ -7,6 +7,8 @@
 
 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.service.CourseStatisticService;
 import cn.com.qmth.examcloud.core.print.service.PrintingProjectService;
 import cn.com.qmth.examcloud.core.print.service.PrintingProjectStatisticService;
@@ -24,12 +26,14 @@ import javax.servlet.http.HttpServletRequest;
  */
 @Controller
 public class IndexController {
+    @Autowired
+    private PrintingProjectStatisticService printingProjectStatisticService;
     @Autowired
     private CourseStatisticService courseStatisticService;
     @Autowired
     private PrintingProjectService printingProjectService;
     @Autowired
-    private PrintingProjectStatisticService printingProjectStatisticService;
+    private SystemProperty systemProperty;
 
     @RequestMapping(value = "/", method = RequestMethod.GET)
     public String index(HttpServletRequest request) throws Exception {
@@ -44,4 +48,12 @@ public class IndexController {
         printingProjectStatisticService.initAllPrintingProjectStatistic();
     }
 
+    @ResponseBody
+    @RequestMapping(value = "/demo", method = RequestMethod.GET)
+    public void demo() {
+        String url = "http://baidu.com";
+        String path = systemProperty.getTempDir() + "/test.pdf";
+        ElectronUtils.toPdf(url, path);
+    }
+
 }