Convert office files to pdf using OpenOffice/LibreOffice

Convert office files to pdf using OpenOffice/LibreOffice

Development Environment: Windows Operating System
Project Architecture: Maven
Depends on Jar Package: jodconverter-local-4.2.0.jar
Conversion Software: OpenOffice / LibreOffice

pom.xml configuration
<dependencies>
    <!-- https://mvnrepository.com/artifact/org.jodconverter/jodconverter-local -->
    <dependency>
        <groupId>org.jodconverter</groupId>
        <artifactId>jodconverter-local</artifactId>
        <version>4.2.0</version>
    </dependency>
</dependencies>
core code
public static void Word2Pdf(String srcPath, String desPath) throws IOException {
    // 源文件目录
    File inputFile = new File(srcPath);
    if (!inputFile.exists()) {
        System.out.println("源文件不存在!");
        return;
    }
    // 输出文件目录
    File outputFile = new File(desPath);
    if (!outputFile.getParentFile().exists()) {
        outputFile.getParentFile().exists();
    }
    // 连接OpenOffice/LibreOffice服务
    OfficeManager officeManager = LocalOfficeManager.builder().officeHome("D:\\Program Files\\OpenOffice 4").install().build();
    try {
        officeManager.start();
        // 转换文档到pdf
        long time = System.currentTimeMillis();
        JodConverter.convert(inputFile).to(outputFile).execute();
        logger.info("文件:{}转换PDF:{}完成,用时{}毫秒!", srcPath, desPath, System.currentTimeMillis() - time);
    } catch (OfficeException e) {
        e.printStackTrace();
        logger.warn("文件:{}转换PDF:{}失败!", srcPath, desPath);
    } finally {
        // 关闭连接
        OfficeUtils.stopQuietly(officeManager);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325794503&siteId=291194637