DESKTOP-LMJ82IJ\Eno
2025-04-07 de2365da21526a6af8e0c1504ed489dcc3617de9
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.vincent.rsf.server.common.utils;
 
import com.vincent.rsf.framework.common.Cools;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
 
import java.io.File;
import java.util.Arrays;
import java.util.Base64;
 
/**
 * OpenOfficeUtil
 *
 * @author vincent
 * @since 2018-12-14 08:38:19
 */
public class OpenOfficeUtil {
    // 支持转换pdf的文件后缀列表
    private static final String[] CAN_CONVERTER_FILES = new String[]{
            "doc", "docx", "xls", "xlsx", "ppt", "pptx"
    };
 
    /**
     * 文件转pdf
     *
     * @param filePath   源文件路径
     * @param outDir     输出目录
     * @param officeHome OpenOffice安装路径
     * @return File
     */
    public static File converterToPDF(String filePath, String outDir, String officeHome) {
        return converterToPDF(filePath, outDir, officeHome, true);
    }
 
    /**
     * 文件转pdf
     *
     * @param filePath   源文件路径
     * @param outDir     输出目录
     * @param officeHome OpenOffice安装路径
     * @param cache      是否使用上次转换过的文件
     * @return File
     */
    public static File converterToPDF(String filePath, String outDir, String officeHome, boolean cache) {
        if (Cools.isEmpty(filePath)) {
            return null;
        }
        File srcFile = new File(filePath);
        if (!srcFile.exists()) {
            return null;
        }
        // 是否转换过
        String outPath = Base64.getEncoder().encodeToString(filePath.getBytes())
                .replace("/", "-").replace("+", "-");
        File outFile = new File(outDir, outPath + ".pdf");
        if (cache && outFile.exists()) {
            return outFile;
        }
        // 转换
        OfficeManager officeManager = null;
        try {
            officeManager = getOfficeManager(officeHome);
            OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
            return converterFile(srcFile, outFile, converter);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (officeManager != null) {
                officeManager.stop();
            }
        }
        return null;
    }
 
    /**
     * 转换文件
     *
     * @param inFile    源文件
     * @param outFile   输出文件
     * @param converter OfficeDocumentConverter
     * @return File
     */
    public static File converterFile(File inFile, File outFile, OfficeDocumentConverter converter) {
        if (!outFile.getParentFile().exists()) {
            if (!outFile.getParentFile().mkdirs()) {
                return outFile;
            }
        }
        converter.convert(inFile, outFile);
        return outFile;
    }
 
    /**
     * 判断文件后缀是否可以转换pdf
     *
     * @param path 文件路径
     * @return boolean
     */
    public static boolean canConverter(String path) {
        try {
            String suffix = path.substring(path.lastIndexOf(".") + 1);
            return Arrays.asList(CAN_CONVERTER_FILES).contains(suffix);
        } catch (Exception e) {
            return false;
        }
    }
 
    /**
     * 连接并启动OpenOffice
     *
     * @param officeHome OpenOffice安装路径
     * @return OfficeManager
     */
    public static OfficeManager getOfficeManager(String officeHome) {
        if (officeHome == null || officeHome.trim().isEmpty()) return null;
        DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
        config.setOfficeHome(officeHome);  // 设置OpenOffice安装目录
        OfficeManager officeManager = config.buildOfficeManager();
        officeManager.start();  // 启动OpenOffice服务
        return officeManager;
    }
 
}