java使用LibreOffice实现word转pdf

news/2025/2/6 14:49:20 标签: word, pdf

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

https://blog.csdn.net/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"

    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;
        }

        // 构建LibreOffice命令行指令
        StringBuilder command = new StringBuilder();
        if (LIBREOFFICE_PATH != null && !LIBREOFFICE_PATH.isEmpty()) {
            command.append(LIBREOFFICE_PATH).append(" --headless ");
        } else {
            command.append("soffice --headless ");
        }
        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();
        }
    }
}


http://www.niftyadmin.cn/n/5843088.html

相关文章

Ollama+deepseek+Docker+Open WebUI实现与AI聊天

1、下载并安装Ollama 官方网址:Ollama 安装好后,在命令行输入, ollama --version 返回以下信息,则表明安装成功, 2、 下载AI大模型 这里以deepseek-r1:1.5b模型为例, 在命令行中,执行&…

国防科大:双目标优化防止LLM灾难性遗忘

📖标题:How to Complete Domain Tuning while Keeping General Ability in LLM: Adaptive Layer-wise and Element-wise Regularization 🌐来源:arXiv, 2501.13669 🌟摘要 🔸大型语言模型(LLM…

产品经理的人工智能课 02 - 自然语言处理

产品经理的人工智能课 02 - 自然语言处理 1 自然语言处理是什么2 一个 NLP 算法的例子——n-gram 模型3 预处理与重要概念3.1 分词 Token3.2 词向量化表示与 Word2Vec 4 与大语言模型的交互过程参考链接 大语言模型(Large Language Models, LLMs)是自然语…

技术书籍写作与编辑沟通指南

引言 撰写技术书籍不仅仅是知识的输出过程,更是与编辑团队紧密合作的协同工作。优秀的技术书籍不仅依赖作者深厚的技术背景,还需要精准的表达、流畅的结构以及符合出版要求的编辑润色。因此,如何高效地与编辑沟通,确保书籍质量&a…

Android Studio:Application 和 Activity的区别

Application 和 Activity 是 Android 中非常重要的两个组件,它们分别负责不同的生命周期管理和应用的不同层次的操作。 Application 是应用级别的生命周期管理,它在整个应用运行时只有一个实例,负责应用的全局初始化和资源管理。Activity 是…

【PostgreSQL内核学习 —— (WindowAgg(二))】

WindowAgg WindowAggState 结构体窗口聚合行为ExecInitWindowAgg 函数ExecWindowAgg 函数代码逻辑解释:计算窗口偏移量代码逻辑详细解释: 代码逻辑解释:窗口聚合分区初始化与行推进逻辑代码逻辑详细解释: 代码逻辑解释&#xff1a…

Jupyterlab和notebook修改文件的默认存放路径的方法

文章目录 1.缘由2.操作流程2.1找到默认的路径2.2创建配置文件2.3修改配置文件内容2.4注意事项 1.缘由 我自己使用jupyterlab的时候,打开是在这个浏览器上面打开的,但是这个打开的文件路径显示的是C盘上面路径,所以这个就很麻烦,因…

WordPress使用(2)

上一篇文章讲述了WordPress的基本安装,主要是docker方式的处理。本文章主要介绍WordPress安装后的其他设置。 1. 安装后设置 安装后碰到的第一个需求就是安装一个合适的主题,但WordPress默认的上传文件大小是2M,远远无法满足要求&#xff0…