[测评系统]--测评系统核心代码库
wzp
2025-08-01 95eb8a2a75cbeebcb3393d404c1952bd59b8989a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import java.io.*;
import java.util.concurrent.ScheduledExecutorService;
 
@Slf4j
@Component
public class ShellTool {
 
//    @Autowired
//    private DocumentConverter documentConverter;
 
    @Autowired
    private Office2PDFUtils office2PDFUtils;
 
    private static ShellTool shellTool;
 
    @PostConstruct
    public void init(){
        shellTool = this;
        //shellTool.documentConverter = documentConverter;
        shellTool.office2PDFUtils = office2PDFUtils;
    }
 
    public static ShellTool builder(){
        return shellTool;
    }
 
 
    public static String execLibreofficeCommand(String type, String sourcePath, String targetPath) {
        String os = System.getProperty("os.name");
        StringBuilder builder = new StringBuilder();
        if (os.toLowerCase().startsWith("mac")) {
            builder.append("/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to ");
        } else if(os.contains("Windows")){
            builder.append("cmd /c start soffice --headless --convert-to ");
        } else {
            builder.append("libreoffice6.4 --headless --convert-to ");
        }
        builder.append(type).append(":impress_pdf_Export ");
        builder.append("--outdir " + targetPath).append(" ");
        builder.append(sourcePath).append(" ");
        
        try {
            File file = new File(sourcePath.replaceAll(".docx", ".pdf"));
            if (file.exists()) {
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("执行命令:{}",builder.toString());
        return exec(builder.toString());
    }
 
    public static void execNewLibreofficeCommand(String type, String sourcePath, String targetPath) throws OfficeException {
        //使用文件方式转换成PDF
        //shellTool.documentConverter.convert(new File(sourcePath)).to(new File(targetPath)).execute();
        shellTool.office2PDFUtils.office2Pdf(sourcePath,targetPath);
    }
 
    private static String exec(String command) {
        String returnString = "";
        Runtime runTime = Runtime.getRuntime();
        if (runTime == null) {
            System.err.println("Create runtime false!");
        }
        try {
            Process pro = runTime.exec(command);
            BufferedReader input = new BufferedReader(new InputStreamReader(pro.getInputStream()));
            PrintWriter output = new PrintWriter(new OutputStreamWriter(pro.getOutputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                returnString = returnString + line + "\n";
            }
            input.close();
            output.close();
            pro.destroy();
        } catch (IOException ex) {
            log.error("执行PDF转换Shell失败:{}", ex.getMessage(), ex);
        }
        return returnString;
    }
    public static void main(String[] args) {
        //System.out.println(exec("soffice.exe --headless --invisible --convert-to  D:/ots/uploadPath/upload/2021/02/28/01e01cf7e372ba8b1c5d24b8d69a46f8.docx 01e01cf7e372ba8b1c5d24b8d69a46f8.pdf "));
    }
}