使用LibreOffice在线word转换pdf


相关资料

Windows安装包:LibreOffice_7.0.6_Win_x64.msi
Linux(CentOs)安装包:LibreOffice_7.0.6_Linux_x86-64_rpm.zip

安装完成后,后续将使用java代码调用软件服务来提供word格式转换



安装

windows安装:双击,点下一步…(安装路径不要含有中文)


Linux安装
解压上面资料的zip,得到两个tar压缩文件,上传至Linux




安装

# 解压
tar -zxvf LibreOffice_7.0.6_Linux_x86-64_rpm.tar.gz
tar -zxvf LibreOffice_7.0.6_Linux_x86-64_rpm_langpack_zh-CN.tar.gz

# 使用yum安装rpm目录里的rpm
yum install LibreOffice_7.0.6.2_Linux_x86-64_rpm/RPMS/*.rpm -y
yum install LibreOffice_7.0.6.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/*.rpm -y

# 修改profile添加环境变量
vim /etc/profile 
# 末尾追加
export LibreOffice_PATH=/opt/libreoffice7.0/program
export PATH=$LibreOffice_PATH:$PATH

# 使配置文件生效
source /etc/profile

# 打印安装路径(下列代码libreOfficePath填此值)
whereis libreoffice

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard


编码调用部分

pom.xml


<libreoffice.version>6.4.7</libreoffice.version>

<dependency>
	<groupId>org.artofsolving.jodconverter</groupId>
	<artifactId>jodconverter-core</artifactId>
	<version>3.0-beta-4</version>
	<scope>system</scope>
	<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/jodconverter-3.0-beta-4.jar</systemPath>
</dependency>
<dependency>
	<groupId>org.libreoffice</groupId>
	<artifactId>ridl</artifactId>
	<version>${libreoffice.version}</version>
</dependency>
<dependency>
	<groupId>org.libreoffice</groupId>
	<artifactId>juh</artifactId>
	<version>${libreoffice.version}</version>
</dependency>
<dependency>
	<groupId>org.libreoffice</groupId>
	<artifactId>jurt</artifactId>
	<version>${libreoffice.version}</version>
</dependency>
<dependency>
	<groupId>org.libreoffice</groupId>
	<artifactId>unoil</artifactId>
	<version>${libreoffice.version}</version>
</dependency>

utils

import lombok.extern.slf4j.Slf4j;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.ExternalOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;

import java.io.File;

/**
 * @author 954L
 * @create 2021/6/1 13:44
 */
@Slf4j
public class WordUtils {

    private static String libreOfficePath;

    static {
        try {
            String osName = System.getProperty("os.name").toLowerCase();
            libreOfficePath = osName.contains("windows") ? "E:\\libreOffice\\install": "/usr/bin/libreoffice7.0";
        } catch (Exception e) {
            log.error("初始化libreOfficePath出错", e);
        }
    }

    public static void main(String[] args) {
        String wordNameRealPath = "D:\\download\\1.docx";
        String pdfNameRealPath = "D:\\download\\1.pdf";

        docToPdfLibre(wordNameRealPath, pdfNameRealPath);
        System.out.println("转换完成!");
    }

    public static void docToPdfLibre(String docxPath, String pdfPath) {
        OfficeManager officeManager = getOfficeManager();
        try {
            // 转换
            OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
            File inputFile = new File(docxPath);
            File outputFile = new File(pdfPath);
            converter.convert(inputFile, outputFile);
        } finally {
            officeManager.stop();
        }
    }

    private static OfficeManager getOfficeManager() {
        OfficeManager officeManager = null;
        try {
            System.out.println("尝试连接已启动的服务...");
            ExternalOfficeManagerConfiguration externalProcessOfficeManager = new ExternalOfficeManagerConfiguration();
            externalProcessOfficeManager.setConnectOnStart(true);
            externalProcessOfficeManager.setPortNumber(8100);
            officeManager = externalProcessOfficeManager.buildOfficeManager();
            officeManager.start();
            System.out.println("转换服务启动成功!");
        } catch (Exception e) {
            System.out.println("启动新服务!");
            DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
            configuration.setOfficeHome(new File(libreOfficePath));
            configuration.setPortNumber(8100);
            // 设置任务执行超时时间
            configuration.setTaskExecutionTimeout(1000 * 60 * 5L);
            // 设置任务队列超时时间
            configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);

            officeManager = configuration.buildOfficeManager();
            officeManager.start();
            System.out.println("服务启动成功!");
        }
        return officeManager;
    }

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Node.js 是一种在服务器端运行的 JavaScript 运行环境,可以用于实现各种各样的应用程序。而 LibreOffice 是一款免费、开源的办公软件套件,其中包括了 Writer、Calc、Impress 等应用程序,支持多种文档格式。下面是一个基于 Node.js 和 LibreOffice 的实现 WordPDF 的简单方法: 1.安装 LibreOffice:首先需要在服务器上安装 LibreOffice,可以通过命令行或者图形界面进行安装。 2.使用 Node.js 的 child_process 模块:在 Node.js 中可以通过 child_process 模块来执行系统命令,在本例中可以使用该模块执行 LibreOffice 的命令行工具来进行 WordPDF 的操作。 3.编写 Node.js 代码:可以通过 Node.js 编写一个简单的脚本来实现 WordPDF。以下是一个简单的示例代码: ```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 脚本即可进行 WordPDF 的操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

954L

帮帮孩子把~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值