Spring结合Libreoffice实现word转PDF

参考文献:https://www.jianshu.com/p/08ecffcbc50a
个人学习用!

一、目标

在Linux上部署Libreoffice,提供Spring项目的Word转Pdf功能。
优点:开源、效果好、速度快,与原word相比,还原度97%(个人看法)

二、Linux搭建步骤

1、安装Libreoffice

官网:Libreoffice官网根据需要下载不同操作系统的安装包,本文以Linux为例。
在这里插入图片描述

2、在Linux上解压并安装

/opt/zsh目录下,执行以下命令解压:

tar -zxvf LibreOffice_7.6.0_Linux_x86-64_rpm.tar.gz

然后在当前目录生成文件夹:LibreOffice_7.6.0_Linux_x86-64_rpm,进入该文件夹下的RPMS目录,执行以下命令:

yum localinstall *.rpm

安装成功后,会在/opt目录下生成文件夹:libreoffice7.6
到此安装成功!

3、启动LibreOffice服务

执行以下命令启动LibreOffice服务:注意是/opt,不是安装目录/opt/zsh

/opt/libreoffice7.6/program/soffice --headless --accept="  socket,host=0.0.0.0,port=8100;urp;"- -nofirststartwizard &

其中,0.0.0.0表示任何IP都可访问LibreOffice服务。

4、执行

执行以下命令,测试word转pdf:

​/opt/libreoffice7.6/program/soffice --headless --invisible --convert-to pdf /opt/zsh/test.word --outdir /opt/zsh/

三、Spring集成

1、导入依赖

<dependency>
    <groupId>com.artofsolving</groupId>
    <artifactId>jodconverter</artifactId>
    <version>2.2.2</version>
</dependency>
<dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>juh</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>ridl</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.openoffice</groupId>
    <artifactId>unoil</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.jodconverter</groupId>
    <artifactId>jodconverter-spring-boot-starter</artifactId>
    <version>4.2.0</version>
</dependency>

2、方法

public void wordToPdfConvert(String fromFilePath, String toFilePath) {
    File fromFile = new File(fromFilePath);
    File toFile = new File(toFilePath);

    // libreoffice服务
    SocketOpenOfficeConnection connection = new SocketOpenOfficeConnection(IP, PORT);

    try {
        connection.connect();
        System.out.println("获取连接成功!");
    } catch (ConnectException e) {
        System.out.println("获取连接失败!");
        e.printStackTrace();
        return;
    }
    StreamOpenOfficeDocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
    converter.convert(fromFile, toFile);
    connection.disconnect();
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Node.js 是一种在服务器端运行的 JavaScript 运行环境,可以用于实现各种各样的应用程序。而 LibreOffice 是一款免费、开源的办公软件套件,其中包括了 Writer、Calc、Impress 等应用程序,支持多种文档格式。下面是一个基于 Node.js 和 LibreOffice实现 Word PDF 的简单方法: 1.安装 LibreOffice:首先需要在服务器上安装 LibreOffice,可以通过命令行或者图形界面进行安装。 2.使用 Node.js 的 child_process 模块:在 Node.js 中可以通过 child_process 模块来执行系统命令,在本例中可以使用该模块执行 LibreOffice 的命令行工具来进行 Word PDF 的操作。 3.编写 Node.js 代码:可以通过 Node.js 编写一个简单的脚本来实现 Word PDF。以下是一个简单的示例代码: ```javascript const { spawn } = require('child_process'); const inputFilePath = '/path/to/input.docx'; const outputFilePath = '/path/to/output.pdf'; const libreoffice = spawn('libreoffice', [ '--headless', '--convert-to', 'pdf', inputFilePath, '--outdir', outputFilePath, ]); libreoffice.stdout.on('data', (data) => { console.log(`stdout: ${data}`); }); libreoffice.stderr.on('data', (data) => { console.error(`stderr: ${data}`); }); libreoffice.on('close', (code) => { console.log(`child process exited with code ${code}`); }); ``` 以上代码中,spawn 方法会启动一个新的进程来执行 LibreOffice 命令行工具。'--headless' 参数表示以无头模式运行,'--convert-to pdf' 参数表示换成 PDF 格式,inputFilePath 参数表示输入文件的路径,'--outdir' 参数表示输出文件的路径。 4.运行 Node.js 代码:在终端中运行 Node.js 脚本即可进行 Word PDF 的操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值