ly пре 3 година
родитељ
комит
60675363a1

+ 19 - 14
master/pom.xml

@@ -312,23 +312,28 @@
 			<artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
 			<version>2.0.2</version>
 		</dependency>
-		<!-- https://mvnrepository.com/artifact/com.artofsolving/jodconverter -->
+
+		<dependency>
+			<groupId>org.jodconverter</groupId>
+			<artifactId>jodconverter-spring-boot-starter</artifactId>
+			<version>4.4.2</version>
+		</dependency>
+		<dependency>
+			<groupId>org.jodconverter</groupId>
+			<artifactId>jodconverter-local</artifactId>
+			<version>4.4.2</version>
+		</dependency>
+		<dependency>
+			<groupId>org.jodconverter</groupId>
+			<artifactId>jodconverter-core</artifactId>
+			<version>4.4.2</version>
+		</dependency>
 		<dependency>
-			<groupId>com.artofsolving</groupId>
-			<artifactId>jodconverter</artifactId>
-			<version>2.2.1</version>
+			<groupId>org.libreoffice</groupId>
+			<artifactId>ridl</artifactId>
+			<version>7.2.5</version>
 		</dependency>
 
-		<!--<dependency>
-			<groupId>org.apache.poi</groupId>
-			<artifactId>poi-ooxml</artifactId>
-			<version>3.17</version>
-		</dependency>-->
-		<!--<dependency>
-			<groupId>org.apache.poi</groupId>
-			<artifactId>ooxml-schemas</artifactId>
-			<version>1.1</version>
-		</dependency>-->
 
 		<!--审批流-->
 		<dependency>

+ 71 - 48
master/src/main/java/com/ruoyi/project/common/controller/WordController.java

@@ -1,9 +1,6 @@
 package com.ruoyi.project.common.controller;
 
-import com.artofsolving.jodconverter.DocumentConverter;
-import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
-import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
-import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
+
 import com.deepoove.poi.XWPFTemplate;
 import com.ruoyi.common.constant.Constants;
 import com.ruoyi.common.utils.StringUtils;
@@ -14,7 +11,13 @@ import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
 import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
 import io.jsonwebtoken.lang.Assert;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
+
+import org.jodconverter.core.DocumentConverter;
+import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -35,6 +38,9 @@ import java.util.Map;
 @RequestMapping("/common/word")
 public class WordController extends BaseController
 {
+    @Resource
+    private DocumentConverter documentConverter;
+
     /**
      * @author lsc
      * @param templatePath word模板文件路径
@@ -75,42 +81,59 @@ public class WordController extends BaseController
     @GetMapping("/createWord")
     public AjaxResult word() throws IOException
     {
-        Map<String, Object> params = new HashMap<>();
-        // 渲染文本
-        params.put("no","10210120102");
-//        params.put("apply_man","知识追寻者");
-
-        // 模板路径
-        String templatePath = "static/word/ticket.docx";
-        // 生成word的路径
-        String fileDir = "D:/ticket/demo";
-        // 生成word的文件
-        String fileName = "zszxz.doc";
-        String wordPath = createWord(templatePath, fileDir, fileName, params);
-
+        System.out.println("使用流的方式转换PDF!");
+        //使用流的方式转换PDF
         try {
-            //读取word文档
-            XWPFDocument document = null;
-            try (InputStream in = Files.newInputStream(Paths.get("D:/ticket/demo/sems.docx"))) {
-                document = new XWPFDocument(in);
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-//             将word转成pdf
-            PdfOptions options = PdfOptions.create();
-            try (OutputStream outPDF = Files.newOutputStream(Paths.get("D:/ticket/demo/html.pdf"))) {
-                PdfConverter.getInstance().convert(document, outPDF, options);
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-            String srcPath = "D:/ticket/demo/sems.doc";
-            String desPath = "D:/ticket/demo/openoffice.pdf";
-            Word2Pdf(srcPath, desPath);
-        } catch (Exception e) {
-            e.printStackTrace();
+            InputStream in = Files.newInputStream(Paths.get("D:/ticket/demo/sems.docx"));
+            OutputStream outPDF = Files.newOutputStream(Paths.get("D:/ticket/demo/html.pdf"));
+            documentConverter.convert(in)
+                    .as(DefaultDocumentFormatRegistry.DOCX)
+                    .to(outPDF)
+                    .as(DefaultDocumentFormatRegistry.PDF)
+                    .execute();
+            System.out.println("转换完成!");
+        }catch (Exception e){
+            System.out.println("转换出错!");
+
         }
 
-        System.out.println("生成文档路径:" + wordPath);
+
+//        Map<String, Object> params = new HashMap<>();
+//        // 渲染文本
+//        params.put("no","10210120102");
+////        params.put("apply_man","知识追寻者");
+//
+//        // 模板路径
+//        String templatePath = "static/word/ticket.docx";
+//        // 生成word的路径
+//        String fileDir = "D:/ticket/demo";
+//        // 生成word的文件
+//        String fileName = "zszxz.doc";
+//        String wordPath = createWord(templatePath, fileDir, fileName, params);
+//
+//        try {
+//            //读取word文档
+//            XWPFDocument document = null;
+//            try (InputStream in = Files.newInputStream(Paths.get("D:/ticket/demo/sems.docx"))) {
+//                document = new XWPFDocument(in);
+//            } catch (IOException e) {
+//                e.printStackTrace();
+//            }
+////             将word转成pdf
+//            PdfOptions options = PdfOptions.create();
+//            try (OutputStream outPDF = Files.newOutputStream(Paths.get("D:/ticket/demo/html.pdf"))) {
+//                PdfConverter.getInstance().convert(document, outPDF, options);
+//            } catch (IOException e) {
+//                e.printStackTrace();
+//            }
+//            String srcPath = "D:/ticket/demo/sems.doc";
+//            String desPath = "D:/ticket/demo/openoffice.pdf";
+//            Word2Pdf(srcPath, desPath);
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//
+//        System.out.println("生成文档路径:" + wordPath);
         return null;
     }
 
@@ -132,17 +155,17 @@ public class WordController extends BaseController
         Process p = Runtime.getRuntime().exec(command);
 
         // 连接openoffice服务
-        OpenOfficeConnection connection = new SocketOpenOfficeConnection(
-                "127.0.0.1", 8100);
-        connection.connect();
-
-        // 转换word到pdf
-        DocumentConverter converter = new OpenOfficeDocumentConverter(
-                connection);
-        converter.convert(inputFile, outputFile);
-
-        // 关闭连接
-        connection.disconnect();
+//        OpenOfficeConnection connection = new SocketOpenOfficeConnection(
+//                "127.0.0.1", 8100);
+//        connection.connect();
+//
+//        // 转换word到pdf
+//        DocumentConverter converter = new OpenOfficeDocumentConverter(
+//                connection);
+//        converter.convert(inputFile, outputFile);
+//
+//        // 关闭连接
+//        connection.disconnect();
 
         // 关闭进程
         p.destroy();

+ 10 - 0
master/src/main/resources/application.yml

@@ -185,3 +185,13 @@ gen:
 jasypt:
   encryptor:
     password: test
+jodconverter:
+  local:
+    enabled: true
+    # 设置LibreOffice主目录
+    #    office-home: /opt/libreoffice6.4
+    office-home: C:/Program Files/LibreOffice
+    # 开启多个LibreOffice进程,每个端口对应一个进程
+    portNumbers: 9090,9081,9089
+    # LibreOffice进程重启前的最大进程数
+    maxTasksPerProcess: 100