From 2f4cc85cd586618865ad2892fa378fff123613f9 Mon Sep 17 00:00:00 2001
From: linzhijie <1003392067@qq.com>
Date: 星期五, 21 十月 2022 12:58:58 +0800
Subject: [PATCH] 更改转换方法

---
 src/main/java/com/ots/project/tool/ShellTool.java                   |   13 ++
 src/main/java/com/ots/project/tool/libreoffice/Office2PDFUtils.java |  193 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 203 insertions(+), 3 deletions(-)

diff --git a/src/main/java/com/ots/project/tool/ShellTool.java b/src/main/java/com/ots/project/tool/ShellTool.java
index 2b4c2c8..07dff39 100644
--- a/src/main/java/com/ots/project/tool/ShellTool.java
+++ b/src/main/java/com/ots/project/tool/ShellTool.java
@@ -1,6 +1,7 @@
 package com.ots.project.tool;
 import com.ots.common.utils.spring.SpringUtils;
 import com.ots.common.utils.text.CharsetKit;
+import com.ots.project.tool.libreoffice.Office2PDFUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.jodconverter.core.DocumentConverter;
 import org.jodconverter.core.office.OfficeException;
@@ -15,14 +16,19 @@
 @Component
 public class ShellTool {
 
+//    @Autowired
+//    private DocumentConverter documentConverter;
+
     @Autowired
-    private DocumentConverter documentConverter;
+    private Office2PDFUtils office2PDFUtils;
+
     private static ShellTool shellTool;
 
     @PostConstruct
     public void init(){
         shellTool = this;
-        shellTool.documentConverter = documentConverter;
+        //shellTool.documentConverter = documentConverter;
+        shellTool.office2PDFUtils = office2PDFUtils;
     }
 
     public static ShellTool builder(){
@@ -58,7 +64,8 @@
 
     public static void execNewLibreofficeCommand(String type, String sourcePath, String targetPath) throws OfficeException {
         //浣跨敤鏂囦欢鏂瑰紡杞崲鎴怭DF
-        shellTool.documentConverter.convert(new File(sourcePath)).to(new File(targetPath)).execute();
+        //shellTool.documentConverter.convert(new File(sourcePath)).to(new File(targetPath)).execute();
+        shellTool.office2PDFUtils.office2Pdf(sourcePath,targetPath);
     }
 
     private static String exec(String command) {
diff --git a/src/main/java/com/ots/project/tool/libreoffice/Office2PDFUtils.java b/src/main/java/com/ots/project/tool/libreoffice/Office2PDFUtils.java
new file mode 100644
index 0000000..061e109
--- /dev/null
+++ b/src/main/java/com/ots/project/tool/libreoffice/Office2PDFUtils.java
@@ -0,0 +1,193 @@
+package com.ots.project.tool.libreoffice;
+
+import lombok.extern.slf4j.Slf4j;
+import org.jodconverter.core.DocumentConverter;
+import org.jodconverter.core.office.OfficeException;
+import org.jodconverter.core.office.OfficeManager;
+import org.jodconverter.local.LocalConverter;
+import org.jodconverter.local.office.LocalOfficeManager;
+import org.jodconverter.local.process.WindowsProcessManager;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import java.io.File;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingDeque;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+/**
+ * @version 1.0
+ * @Author linzhijie
+ * @Description TODO
+ * @Date 2022/10/21  10:03
+ */
+@Slf4j
+@Component
+public class Office2PDFUtils {
+    
+    @Value("${jodconverter.local.office-home}")
+    private String officeHome;
+
+    @Value("${jodconverter.local.portNumbers}")
+    private String portNumbers;
+
+    private static BlockingQueue<OfficeManager> OfficeManagerQueue = new LinkedBlockingDeque<>();
+
+    private static Lock lock = new ReentrantLock();
+
+    /**
+     * @Description: 浣縊ffice2003-2007鍏ㄩ儴鏍煎紡鐨勬枃妗�(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 杞寲涓簆df鏂囦欢
+     * @method: openOfficeToPDF
+     * @Param: inputFilePath
+     * @Param: outputFilePath
+     * @return: boolean
+     * @auther: LHL
+     */
+    public boolean openOfficeToPDF(String inputFilePath, String outputFilePath) {
+        return office2Pdf(inputFilePath, outputFilePath);
+    }
+
+    /**
+     * @Description: 鏍规嵁鎿嶄綔绯荤粺鐨勫悕绉帮紝鑾峰彇OpenOffice.org 鐨勫畨瑁呯洰褰�
+     * @method: getOfficeHome
+     * @Param:
+     * @return: java.lang.String OpenOffice.org 鐨勫畨瑁呯洰褰�
+     * @auther: LHL
+     */
+/*    private String getOfficeHome() {
+        String osName = System.getProperty("os.name");
+        log.debug("鎿嶄綔绯荤粺鍚嶇О:" + osName);
+        if (Pattern.matches("Linux.*", osName)) {
+            return linuxPath;
+        } else if (Pattern.matches("Windows.*", osName)) {
+            return winPath;
+        } else if (Pattern.matches("Mac.*", osName)) {
+            return winPath;
+        }
+        return null;
+    }*/
+
+    /**
+     * @Description: 鍦╞ean鍒涘缓瀹炰緥鏃�, 杩炴帴OpenOffice.org 骞朵笖鍚姩OpenOffice.org
+     * @method: initOpenOfficeService
+     * @Param:
+     * @return: void
+     * @auther: LHL
+     */
+    @PostConstruct
+    public void initOpenOfficeService() {
+
+        LocalOfficeManager.Builder builder = LocalOfficeManager.builder();
+        builder.officeHome(officeHome);
+        builder.portNumbers(2002);
+        builder.processTimeout(12000L);
+        builder.processRetryInterval(250L);
+        builder.maxTasksPerProcess(200);
+        builder.taskQueueTimeout(30000L);
+        for (String port : portNumbers.split(",")) {
+            builder.portNumbers(Integer.valueOf(port));
+            OfficeManager OfficeManager = builder.build();
+            try {
+                OfficeManager.start();
+                log.debug("***********************officeManager 鍚姩!***********************");
+            } catch (OfficeException e) {
+                //鎵撳嵃鏃ュ織
+                log.debug("***********************鍚姩 openOffice 澶辫触!***********************");
+                e.printStackTrace();
+            }
+            try {
+                OfficeManagerQueue.put(OfficeManager);
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    /**
+     * @Description: bean閿�姣佹椂鍋滄
+     * @method: destroyOpenOfficeService
+     * @Param:
+     * @return: void
+     * @auther: LHL
+     */
+    @PreDestroy
+    public void destroyOpenOfficeService() {
+        for (OfficeManager manager : OfficeManagerQueue) {
+            try {
+                log.debug("鍏抽棴 officeManager");
+                manager.stop();
+            } catch (OfficeException e) {
+                log.debug("officeManager鍏抽棴澶辫触!" + e.getMessage());
+                e.printStackTrace();
+            }
+        }
+    }
+
+    /**
+     * @Description: 杞崲鏂囦欢
+     * @method: convertFile
+     * @Param: inputFile
+     * @Param: outputFilePath
+     * @Param: officeManager
+     * @return: java.io.File
+     * @auther: LHL
+     */
+    private File convertFile(File inputFile, String outputFilePath, OfficeManager officeManager) throws OfficeException {
+        File outputFile = new File(outputFilePath);
+        // 鍋囧鐩爣璺緞涓嶅瓨鍦�,鍒欐柊寤鸿璺緞
+        if (!outputFile.getParentFile().exists()) {
+            outputFile.getParentFile().mkdirs();
+        }
+        DocumentConverter converter = LocalConverter.builder()
+                .officeManager(officeManager)
+                .build();
+        // 閿�
+        lock.lock();
+        //converter.convert(inputFile).to(outputFile).execute();
+        converter.convert(inputFile).to(outputFile).execute();
+        //閲婃斁閿�
+        lock.unlock();
+        try {
+            OfficeManagerQueue.put(officeManager);
+            log.debug("blockingQueue puted OfficeManagerQueue size :" + OfficeManagerQueue.size());
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
+        log.debug("鏂囦欢锛�" + inputFile.getAbsolutePath() + "\n杞崲涓虹洰鏍囨枃浠讹細" + outputFile + "\n鎴愬姛!");
+        return outputFile;
+    }
+
+    /**
+     * 鏂囦欢鍚嶉渶瑕佸敮涓�
+     */
+    public boolean office2Pdf(String inputFilePath, String outputFilePath) {
+        OfficeManager officeManager = null;
+        try {
+            if (StringUtils.isEmpty(inputFilePath)) {
+                log.debug("杈撳叆鏂囦欢鍦板潃涓虹┖锛岃浆鎹㈢粓姝�!");
+                return false;
+            }
+            File inputFile = new File(inputFilePath);
+            if (!inputFile.exists()) {
+                log.debug("杈撳叆鏂囦欢涓嶅瓨鍦紝杞崲缁堟!");
+                return false;
+            }
+            officeManager = OfficeManagerQueue.take();
+            log.debug("blockingQueue taked , OfficeManagerQueue size :" + OfficeManagerQueue.size());
+            File file = convertFile(inputFile, outputFilePath, officeManager);
+            if (!file.exists()) {
+                log.debug("杞崲鏂囦欢涓嶅瓨鍦�!杞崲澶辫触!!");
+                return false;
+            }
+            return true;
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.debug("杞崲寮傚父");
+            return false;
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.1