java使用LibreOffice实现word转pdf

之前使用dom4j转换,依赖office;
 网上还有Apache poi和itextpdf方式,尝试后发现复杂word,比如包含表格曲线等支持性不好。
 最后发现 LibreOffice:不依赖office,免费,可跨平台
参考链接:
https://developer.aliyun.com/article/1629006

https://blog.youkuaiyun.com/u013984781/article/details/144430991

1、下载LibreOffice

https://zh-cn.libreoffice.org/download/libreoffice/

安装步骤参考:

优快云

2、代码

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class WordToPdfConverter {

    //LibreOffice的安装路径(如果环境变量已经配置,则不需要此路径)
    private static final String LIBREOFFICE_PATH = "/path/to/libreoffice"; //例如: "/usr/bin/libreoffice" 或 "C:\\Program Files\\LibreOffice\\program\\soffice.exe"
    private static String OS_NAME = "os.name";
    private static String WINDOWS = "windows";


    public static void main(String[] args) {
        String inputFilePath = "path/to/your/input.docx";
        String outputFilePath = "path/to/your/output.pdf";

        convertWordToPdf(inputFilePath, outputFilePath);
    }

    public static void convertWordToPdf(String inputFilePath, String outputFilePath) {
        File inputFile = new File(inputFilePath);
        File outputFile = new File(outputFilePath);

        if (!inputFile.exists()) {
            System.err.println("Input file does not exist: " + inputFilePath);
            return;
        }
        boolean IS_WINDOWS = System.getProperty(OS_NAME).toLowerCase().contains(WINDOWS);//是否是windows

        // 构建LibreOffice命令行指令
        StringBuilder command = new StringBuilder();
        if (IS_WINDOWS && LIBREOFFICE_PATH != null && !LIBREOFFICE_PATH.isEmpty()) {
            command.append(LIBREOFFICE_PATH).append(" --headless ");
        } else {
            //command.append("soffice --headless ");
            command.append("libreoffice24.8 --headless ");//由于我linux中安装的是24.8版本的Libreoffice,所以命令使用libreoffice24.8
        }
        command.append("--convert-to pdf ")
               .append("--outdir ").append(outputFile.getParent())
               .append(" ").append(inputFilePath);

        try {
            // 执行系统命令
            Process process = Runtime.getRuntime().exec(command.toString());

            // 读取命令执行的输出和错误流(可选)
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                System.out.println(line);
            }

            BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((line = errorReader.readLine()) != null) {
                System.err.println(line);
            }

            // 等待进程结束
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("Conversion successful: " + outputFilePath);
            } else {
                System.err.println("Conversion failed with exit code: " + exitCode);
            }

        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值