springboot中使用LibreOffice实现word转pdf(还原程度很高,可以配置线程并发!)

一、需要先安装LibreOffice

Windows:

访问官网:https://www.libreoffice.org/download/,下载Windows版本安装包,双击运行安装,按提示完成安装,默认安装路径:C:\Program
Files\LibreOffice

Mac:

# 方式1:Homebrew安装
brew install --cask libreoffice

# 方式2:官网下载DMG安装

Linux服务器安装:
1.Ubuntu/Debian

# 更新包列表
sudo apt update

# 安装LibreOffice
sudo apt install libreoffice

# 安装中文字体(可选)
sudo apt install fonts-wqy-zenhei fonts-wqy-microhei

# 验证安装
libreoffice --version

2.CentOS/RHEL:

# 添加EPEL仓库
sudo yum install epel-release

# 安装LibreOffice
sudo yum install libreoffice

# 安装中文字体
sudo yum install wqy-zenhei-fonts

查看安装路径

which libreoffice
# 通常路径:/usr/lib/libreoffice

Docker中安装:

# Dockerfile
FROM openjdk:11-jre-slim

# 安装LibreOffice
RUN apt update && \
    apt install -y libreoffice && \
    apt clean && \
    rm -rf /var/lib/apt/lists/*

# 设置环境变量
ENV OFFICE_HOME=/usr/lib/libreoffice

验证安装是否成功

# 查看版本
libreoffice --version

# 启动服务测试
soffice --headless --accept="socket,host=0.0.0.0,port=2002;urp;" --nofirststartwizard

# 查看进程
ps aux | grep soffice

常见问题解决
字体缺失:

# Linux安装常用字体
sudo apt install fonts-liberation fonts-noto-cjk

端口占用:

# 查看端口占用
netstat -tlnp | grep 2002

# 修改SpringBoot配置中的端口号

内存优化(服务器环境):

# application.yml
jodconverter:
  local:
    max-tasks-per-process: 20  # 根据服务器性能调整
    task-execution-timeout: 120000  # 超时时间(ms)

二、使用示例

添加依赖

<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-spring-boot-starter</artifactId>
    <version>4.4.6</version>
</dependency>
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-local</artifactId>
    <version>4.4.6</version>
</dependency>

配置application.yml

jodconverter:
  local:
    enabled: true
    office-home: /usr/lib/libreoffice  # Linux
    # office-home: C:\Program Files\LibreOffice  # Windows
    port-numbers: 2002
    max-tasks-per-process: 100

代码示例:

package com.geofly.geocloud.queryapp.intranet.service;

import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.OfficeException;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * word转pdf
 */
@Service
public class WordToPdfService {

    private final DocumentConverter documentConverter;

    public WordToPdfService(DocumentConverter documentConverter) {
        this.documentConverter = documentConverter;
    }

    /**
     * word转pdf
     * @param wordFile
     * @param pdfFile
     * @throws OfficeException
     */
    public void wordToPdf(File wordFile, File pdfFile) throws OfficeException {
        documentConverter.convert(wordFile)
                .to(pdfFile)
                .execute();
    }
}

简单使用示例

// 将DOCX转换为PDF
try {
       wordToPdfService.wordToPdf(docxFile, new File(upLoadPath, pdfFilePath));
    } catch (Exception e) {
       OpLogUtil.info("转pdf操作出错了:" + e.getMessage());
       return "转pdf操作出错了:" + e.getMessage() + ",pdfFilePath:" + pdfFilePath;
   }

非常的好用!
在这里插入图片描述

注意:

确保系统已安装LibreOffice

默认端口2002,确保端口未被占用

生产环境建议配置连接池参数

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐