1. 项目背景与需求分析
在企业级应用开发中,文档格式转换是一个常见需求。最近我在开发一个需要将Word文档转换为PDF的项目时,遇到了性能瓶颈。经过技术选型对比,最终选择了LibreOffice作为转换引擎,主要原因有三:
- 转换质量:相比其他开源方案,LibreOffice能完美保留原文档的格式、样式和布局
- 性能表现:实测转换速度比Apache POI等方案快3-5倍
- 稳定性:作为成熟的办公套件,长期维护且社区支持良好
但在实际集成过程中,发现Spring Boot与LibreOffice的整合存在多种方案,且每种方案都有其适用场景和坑点。本文将详细介绍两种主流集成方式(本地调用与远程服务),以及如何在Docker环境中部署的方案选型。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 本地LibreOffice集成方案
2.1 环境准备与依赖配置
首先需要在服务器上安装LibreOffice。对于Ubuntu系统,推荐使用以下命令安装最新稳定版:
bash复制sudo add-apt-repository ppa:libreoffice/ppa
sudo apt update
sudo apt install libreoffice
然后在Spring Boot项目中引入JODConverter依赖:
xml复制<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.4.2</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.4.2</version>
</dependency>
2.2 配置详解与优化
在application.yml中配置本地LibreOffice参数:
yaml复制jodconverter:
local:
enabled: true
office-home: /opt/libreoffice24.2 # Linux安装路径
portNumbers: [8101,8102,8103] # 多端口实现并发处理
maxTasksPerProcess: 100 # 单进程最大任务数
task-execution-timeout: 360000 # 任务执行超时(毫秒)
task-queue-timeout: 360000 # 队列等待超时
process-timeout: 360000 # 进程存活超时
