LibreOffice Java使用
下载地址
// 历史版本地址
downloadarchive.documentfoundation.org/libreoffice/old/
// 最新版本地址
zh-cn.libreoffice.org/download/libreoffice/
// 国内镜像地址
mirrors.cloud.tencent.com/libreoffice/libreoffice/stable/
// 建议使用最新版
安装方式
-
windows下安装
一路默认安装即可,自定义安装时切忌不要安装在中文目录下 安装成功后,可以配置环境变量,方便java程序调用
-
linux下安装
# 验证之前有没有安装过 libreoffice --version # 有安装进行卸载,没有直接下一步 yum remove libreoffice-* # 解压安装 tar -zxvf LibreOffice_7.3.3_Linux_x86-64_rpm.tar.gz cd /opt/LibreOffice_7.3.3_Linux_x86-64_rpm/RPMS # 安装*.rpm yum -y localinstall *.rpm # 安装libreoffice-headless yum install -y libreoffice-headless # 检验是否安装完成 libreoffice7.3 --version # 测试Word转PDF并安装libreoffice-writer [root@bogon Public]# libreoffice --headless --convert-to pdf 123.docx Error: source file could not be loaded # 报这个错,表示缺少writer,需要安装 yum install libreoffice-writer # 转换格式说明 libreoffice --headless --convert-to pdf {文档路径} --outdir {导出目录路径}
使用方式
- 通过jodconverter调用,特点是屏蔽了具体的命令细节,直接调用convert方法即可,并且是同步返回结果或者异常;
- 通过命令调用,特点是需要自己拼接命令以及是异步返回结果;
Jodconverter
// pom 依赖
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.2.0</version>
</dependency>
// 伪代码
public static boolean convertOffice2PDFSyncIsSuccess(File sourceFile, File targetFile) {
try {
LocalOfficeManager.Builder builder = LocalOfficeManager.builder();
builder.officeHome("C:\\Program Files\\LibreOffice");
builder.portNumbers(8100);
builder.taskExecutionTimeout(5 * 1000 * 60); // minute
builder.taskQueueTimeout(1 * 1000 * 60 * 60); // hour
OfficeManager build = builder.build();
build.start();
LocalConverter make = LocalConverter.make(build);
make.convert(sourceFile).to(targetFile).execute();
build.stop();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
Command
/**
* 利用 LibreOffice 将 Office 文档转换成 PDF,该转换是异步的,返回时,转换可能还在进行中,转换是否有异常也未可知
* @param filePath 目标文件地址
* @param targetFilePath 输出文件夹
* @return 子线程执行完毕的返回值
*/
public static int convertOffice2PDFAsync(String filePath, String fileName, String targetFilePath) throws Exception {
String command;
int exitStatus;
String osName = System.getProperty("os.name");
String outDir = targetFilePath.length() > 0 ? " --outdir " + targetFilePath : "";
if (osName.contains("Windows")) {
command = "cmd /c cd /d " + filePath + " && start soffice --headless --invisible --convert-to pdf ./" + fileName + outDir;
} else {
command = "libreoffice6.3 --headless --invisible --convert-to pdf:writer_pdf_Export " + filePath + fileName + outDir;
}
exitStatus = executeOSCommand(command);
return exitStatus;
}
/**
* 调用操作系统的控制台,执行 command 指令
* 执行该方法时,并没有等到指令执行完毕才返回,而是执行之后立即返回,返回结果为 0,只能说明正确的调用了操作系统的控制台指令,但执行结果如何,是否有异常,在这里是不能体现的,所以,更好的姿势是用同步转换功能。
*/
private static int executeOSCommand(String command) throws Exception {
Process process;
process = Runtime.getRuntime().exec(command); // 转换需要时间,比如一个 3M 左右的文档大概需要 8 秒左右,但实际测试时,并不会等转换结束才执行下一行代码,而是把执行指令发送出去后就立即执行下一行代码了。
int exitStatus = process.waitFor();
if (exitStatus == 0) {
exitStatus = process.exitValue();
}
// 销毁子进程
process.destroy();
return exitStatus;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?