xiongzhu 4 жил өмнө
parent
commit
a507f7184e

+ 11 - 3
src/main/java/com/izouma/awesomeAdmin/web/Word2PDFController.java

@@ -18,11 +18,11 @@ import java.io.*;
 
 @Controller
 public class Word2PDFController {
+    private ActiveXComponent app = null;
 
     @PostMapping(value = "/word2pdf", produces = "application/pdf")
     @ResponseBody
     public byte[] word2pdf(@RequestParam("file") MultipartFile file) {
-        ActiveXComponent app = null;
 
         System.out.println("开始转换...");
         // 开始时间
@@ -33,7 +33,7 @@ public class Word2PDFController {
             FileUtils.write(file.getInputStream(), word);
 
             // 打开word
-            app = new ActiveXComponent("Word.Application");
+
             // 设置word不可见,很多博客下面这里都写了这一句话,其实是没有必要的,因为默认就是不可见的,如果设置可见就是会打开一个word文档,对于转化为pdf明显是没有必要的
             //app.setProperty("Visible", false);
             // 获得word中所有打开的文档
@@ -68,9 +68,17 @@ public class Word2PDFController {
 
     @PostConstruct
     public void init() {
+        app = new ActiveXComponent("Word.Application");
     }
 
     @PreDestroy
     public void destroy() {
+        if (app != null) {
+            try {
+                app.invoke("Quit", 0);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
     }
-}
+}