it编程 > 编程语言 > Java

SpringBoot使用LibreOffice实现高保真Word转PDF的方法

9人参与 2026-02-01 Java

一、需要先安装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,确保端口未被占用

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

以上就是springboot使用libreoffice实现高保真word转pdf的方法的详细内容,更多关于springboot libreoffice高保真word转pdf的资料请关注代码网其它相关文章!

(0)

您想发表意见!!点此发布评论

推荐阅读

SSM设置欢迎页的三种方式总结

02-01

SpringBoot3配置文件的使用技巧分享

02-01

SpringBoot接口获取参数的常用注解详解

01-31

Java自动化设置PDF文档属性的示例代码

01-31

Spring Boot异常处理try-catch应该怎么使用?

01-31

Java多重数组使用及说明

01-31

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论