[测评系统]--测评系统核心代码库
林致杰
2023-03-06 c4148dbd5c2d3a1376073b780e96e2a7aac6c7e6
Merge branch 'framework-v11-20220919' into framework-v12-20221221
2个文件已添加
1个文件已修改
162 ■■■■■ 已修改文件
src/main/java/com/ots/common/utils/poi/WordUtil.java 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ots/project/tool/libreoffice/LibreOfficeUtil.java 73 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ots/project/tool/libreoffice/OfficeManagerInstance.java 68 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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、MAQTR采用新的doc转pdf
            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 {
src/main/java/com/ots/project/tool/libreoffice/LibreOfficeUtil.java
New file
@@ -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 左右的文档大概需要 8 秒左右,但实际测试时,并不会等转换结束才执行下一行代码,而是把执行指令发送出去后就立即执行下一行代码了。
        int exitStatus = process.waitFor();
        if (exitStatus == 0) {
            exitStatus = process.exitValue();
        }
        // 销毁子进程
        process.destroy();
        return exitStatus;
    }
}
src/main/java/com/ots/project/tool/libreoffice/OfficeManagerInstance.java
New file
@@ -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();
        }
    }
}