From c4148dbd5c2d3a1376073b780e96e2a7aac6c7e6 Mon Sep 17 00:00:00 2001
From: 林致杰 <1003392067@qq.com>
Date: 星期一, 06 三月 2023 23:19:04 +0800
Subject: [PATCH] Merge branch 'framework-v11-20220919' into framework-v12-20221221

---
 src/main/java/com/ots/project/tool/libreoffice/OfficeManagerInstance.java |   68 ++++++++++++++++++++++
 src/main/java/com/ots/project/tool/libreoffice/LibreOfficeUtil.java       |   73 ++++++++++++++++++++++++
 src/main/java/com/ots/common/utils/poi/WordUtil.java                      |   21 ++++---
 3 files changed, 153 insertions(+), 9 deletions(-)

diff --git a/src/main/java/com/ots/common/utils/poi/WordUtil.java b/src/main/java/com/ots/common/utils/poi/WordUtil.java
index ff09e38..937c127 100644
--- a/src/main/java/com/ots/common/utils/poi/WordUtil.java
+++ b/src/main/java/com/ots/common/utils/poi/WordUtil.java
@@ -17,6 +17,7 @@
 import com.ots.project.tool.exam.ExamUtil;
 import com.ots.project.tool.exam.ImageUtil;
 import com.ots.project.tool.exam.ZipUtil;
+import com.ots.project.tool.libreoffice.LibreOfficeUtil;
 import com.ots.project.tool.report.MAQTR.chart.MAQTRChart;
 import com.ots.project.tool.report.PAQ.chart.PAQChart;
 import org.apache.commons.collections.map.HashedMap;
@@ -902,9 +903,8 @@
 
         if (checkText(runValue)) {
             runValue = runValue.replaceAll("%.*%", "");
-            runValue = runValue.replaceAll("N/A NONE", "");
-
         }
+        runValue = runValue.replaceAll("N/A NONE", "");
 
         return runValue;
     }
@@ -1171,20 +1171,23 @@
 
             //妯℃澘鏂囦欢闇�瑕佸姞閿�
             out = getDownLoadFileOutputStream(reportName);
-            synchronized (fileName.intern()){
+            //synchronized (fileName.intern()){
                 //String templateType = tReportTemplate.getTemplateType();
                 setChageWord(fileName, out, textMap, tReportTemplate.getReportType(),tReportTemplate.getDataPath(), autoTableMap);
                 out.flush();
                 changColorIfJAQ(textMap, out, reportName, tReportTemplate.getReportType());
-            }
+            //}
 
             //PAQ銆丮AQTR閲囩敤鏂扮殑doc杞琾df
             if(ReportTypeEnum.PAQ.getCode().equals(reportType) || ReportTypeEnum.MAQTR.getCode().equals(reportType)){
                 PdfUtil.convertPDF(EssConfig.getProfile() + "/" + reportName);
             }else{
-                //ShellTool.execLibreofficeCommand("pdf", EssConfig.getProfile() + "/" + reportName, EssConfig.getProfile() + "/");
-                ShellTool shellTool = ShellTool.builder();
-                shellTool.execNewLibreofficeCommand("pdf", EssConfig.getProfile() + "/" + reportName, EssConfig.getProfile() + "/" + reportName.replace("docx","pdf"));
+                ShellTool.execLibreofficeCommand("pdf", EssConfig.getProfile() + "/" + reportName, EssConfig.getProfile() + "/");
+                // ShellTool shellTool = ShellTool.builder();
+                // shellTool.execNewLibreofficeCommand("pdf", EssConfig.getProfile() + "/" + reportName, EssConfig.getProfile() + "/" + reportName.replace("docx","pdf"));
+
+                //鏂版柟娉曟祴璇�
+                //LibreOfficeUtil.convertOffice2PDFSyncIsSuccess(new File(EssConfig.getProfile() + "/" + reportName),new File(EssConfig.getProfile() + "/" + reportName.replace("docx","pdf")));
             }
             zipFilePath = getPdfPath(reportName);
 
@@ -1344,9 +1347,9 @@
             repText = repText.replaceAll("<BoldText>", "");
             repText = repText.replaceAll("</BoldText>", "");
         }
-        if (repText.indexOf("N/A NONE") != -1) {
+/*        if (repText.indexOf("N/A NONE") != -1) {
             repText = repText.replaceAll("N/A NONE", "");
-        }
+        }*/
         return repText;
     }
     public static void changTableColor(String fileName, OutputStream out, List<JAQTableStyle> jaqTableStyleList) throws IOException {
diff --git a/src/main/java/com/ots/project/tool/libreoffice/LibreOfficeUtil.java b/src/main/java/com/ots/project/tool/libreoffice/LibreOfficeUtil.java
new file mode 100644
index 0000000..8b9a75c
--- /dev/null
+++ b/src/main/java/com/ots/project/tool/libreoffice/LibreOfficeUtil.java
@@ -0,0 +1,73 @@
+package com.ots.project.tool.libreoffice;
+
+import org.jodconverter.local.JodConverter;
+
+import java.io.File;
+
+/**
+ * @version 1.0
+ * @Author linzhijie
+ * @Description //TODO
+ * @Date 2023/02/14 0:21
+ */
+public class LibreOfficeUtil {
+
+    /*
+     * 鍚屾杞崲
+     * @Date    2021骞�11鏈�09鏃� 11:41:04
+     * @Param   [sourceFile, targetFile]
+     * @Return  boolean
+     */
+    public static boolean convertOffice2PDFSyncIsSuccess(File sourceFile, File targetFile) {
+        try {
+            OfficeManagerInstance.start();
+            JodConverter.convert(sourceFile).to(targetFile).execute();
+        } catch (Exception e) {
+            e.printStackTrace();
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * 鍒╃敤 LibreOffice 灏� Office 鏂囨。杞崲鎴� PDF锛岃杞崲鏄紓姝ョ殑锛岃繑鍥炴椂锛岃浆鎹㈠彲鑳借繕鍦ㄨ繘琛屼腑锛岃浆鎹㈡槸鍚︽湁寮傚父涔熸湭鍙煡
+     * @param filePath       鐩爣鏂囦欢鍦板潃
+     * @param targetFilePath 杈撳嚭鏂囦欢澶�
+     * @return 瀛愮嚎绋嬫墽琛屽畬姣曠殑杩斿洖鍊�
+     */
+    public static int convertOffice2PDFAsync(String filePath, String fileName, String targetFilePath) throws Exception {
+        String command;
+        int exitStatus;
+        String osName = System.getProperty("os.name");
+        String outDir = targetFilePath.length() > 0 ? " --outdir " + targetFilePath : "";
+
+        if (osName.contains("Windows")) {
+            command = "cmd /c cd /d " + filePath + " && start soffice --headless --invisible --convert-to pdf ./" + fileName + outDir;
+        } else {
+            command = "libreoffice --headless --invisible --convert-to pdf:writer_pdf_Export " + filePath + fileName + outDir;
+        }
+
+        exitStatus = executeOSCommand(command);
+        return exitStatus;
+    }
+
+    /**
+     * 璋冪敤鎿嶄綔绯荤粺鐨勬帶鍒跺彴锛屾墽琛� command 鎸囦护
+     * 鎵ц璇ユ柟娉曟椂锛屽苟娌℃湁绛夊埌鎸囦护鎵ц瀹屾瘯鎵嶈繑鍥烇紝鑰屾槸鎵ц涔嬪悗绔嬪嵆杩斿洖锛岃繑鍥炵粨鏋滀负 0锛屽彧鑳借鏄庢纭殑璋冪敤浜嗘搷浣滅郴缁熺殑鎺у埗鍙版寚浠わ紝浣嗘墽琛岀粨鏋滃浣曪紝鏄惁鏈夊紓甯革紝鍦ㄨ繖閲屾槸涓嶈兘浣撶幇鐨勶紝鎵�浠ワ紝鏇村ソ鐨勫Э鍔挎槸鐢ㄥ悓姝ヨ浆鎹㈠姛鑳姐��
+     */
+    private static int executeOSCommand(String command) throws Exception {
+        Process process;
+        process = Runtime.getRuntime().exec(command); // 杞崲闇�瑕佹椂闂达紝姣斿涓�涓� 3M 宸﹀彸鐨勬枃妗eぇ姒傞渶瑕� 8 绉掑乏鍙筹紝浣嗗疄闄呮祴璇曟椂锛屽苟涓嶄細绛夎浆鎹㈢粨鏉熸墠鎵ц涓嬩竴琛屼唬鐮侊紝鑰屾槸鎶婃墽琛屾寚浠ゅ彂閫佸嚭鍘诲悗灏辩珛鍗虫墽琛屼笅涓�琛屼唬鐮佷簡銆�
+
+        int exitStatus = process.waitFor();
+
+        if (exitStatus == 0) {
+            exitStatus = process.exitValue();
+        }
+
+        // 閿�姣佸瓙杩涚▼
+        process.destroy();
+        return exitStatus;
+    }
+
+}
diff --git a/src/main/java/com/ots/project/tool/libreoffice/OfficeManagerInstance.java b/src/main/java/com/ots/project/tool/libreoffice/OfficeManagerInstance.java
new file mode 100644
index 0000000..a023852
--- /dev/null
+++ b/src/main/java/com/ots/project/tool/libreoffice/OfficeManagerInstance.java
@@ -0,0 +1,68 @@
+package com.ots.project.tool.libreoffice;
+
+import org.jodconverter.core.office.OfficeManager;
+import org.jodconverter.local.office.LocalOfficeManager;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+
+/**
+ * @version 1.0
+ * @Author linzhijie
+ * @Description //TODO
+ * @Date 2023/02/14 0:23
+ */
+@Component
+public class OfficeManagerInstance {
+
+    private static OfficeManager INSTANCE = null;
+
+    @Value("${jodconverter.local.office-home}")
+    private String prilibreOfficeHome;
+    @Value("${jodconverter.local.portNumbers}")
+    private String portNumbers;
+    @Value("${libreOffice.taskExecutionTimeoutMinutes:5}")
+    private String taskExecutionTimeoutMinutes;
+    @Value("${libreOffice.taskQueueTimeoutHours:1}")
+    private String taskQueueTimeoutHours;
+
+    public static synchronized void start() {
+        officeManagerStart();
+    }
+
+    @PostConstruct
+    private void init() {
+        try {
+            String[] portNum = portNumbers.split(",");
+            int[] ports = new int[portNum.length];
+            for (int i = 0; i < portNum.length; i++) {
+                ports[i] = Integer.parseInt(portNum[i]);
+            }
+            LocalOfficeManager.Builder builder = LocalOfficeManager.builder().install();
+            builder.officeHome(prilibreOfficeHome);
+            builder.portNumbers(ports);
+            // minute
+            builder.taskExecutionTimeout(Long.valueOf(Integer.parseInt(taskExecutionTimeoutMinutes) * 1000 * 60));
+            // hour
+            builder.taskQueueTimeout(Long.valueOf(Integer.parseInt(taskQueueTimeoutHours) * 1000 * 60 * 60));
+            INSTANCE = builder.build();
+            officeManagerStart();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static void officeManagerStart() {
+        if (INSTANCE.isRunning()) {
+            return;
+        }
+
+        try {
+            INSTANCE.start();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+}

--
Gitblit v1.9.1