LibreOffice 下载、Linux安装、启动、Java集成pdf转换

下载

官网中文主页:https://zh-cn.libreoffice.org/

点击下载,可选Linux、macOS、Windows三大类及其不同处理器对应的定制包

本次在Linux安装使用 LibreOffice_7.3.3_Linux_x86-64_rpm.tar.gz

安装

上传

将安装包放到Linux上

解压

tar -xvf LibreOffice_7.3.3_Linux_x86-64_rpm.tar.gz

安装

进入RPMS文件夹中,

cd LibreOffice_7.3.3_Linux_x86-64_rpm/RPMS
yum localinstall *.rpm

安装过程中遇到选择,输入 y 回车

安装成功后会在 /opt/ 下生产一个 libreoffice7.3 的文件夹

启动

后台启动

nohup libreoffice7.3 --headless --accept="socket,host=0.0.0.0,port=8101;urp;" --nofirststartwizard &

启动时会把pid打到这行命令下面

测试

Linux本地测试文档转换命令:

/opt/libreoffice7.3/program/soffice --headless --invisible --convert-to pdf /opt/test.txt --outdir /opt

查看进程

ps -ef|grep libre

展示3个进程,第3个不算,如下

root      1459   789  0 12:08 pts/4    00:00:00 /opt/libreoffice7.3/program/oosplash --headless --accept=socket,host=0.0.0.0,port=8101;urp; --nofirststartwizard
root      1512  1459  0 12:08 pts/4    00:00:01 /opt/libreoffice7.3/program/soffice.bin --headless --accept=socket,host=0.0.0.0,port=8101;urp; --nofirststartwizard
root      7709   789  0 13:45 pts/4    00:00:00 grep --color=auto libre

查看端口占用

lsof -i:8101

展示如下
COMMAND    PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
soffice.b 1512 root   14u  IPv4 82428014      0t0  TCP *:ldoms-migr (LISTEN)

卸载

你可以尝试 yum erase libreoffice*

或者 yum remove openoffice.org* libreoffice.org*

Java集成

工具包版

maven依赖

	<dependency>
        <groupId>com.artofsolving</groupId>
        <artifactId>jodconverter</artifactId>
        <version>2.2.1</version>
    </dependency>

测试代码

package com.oxye;

import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.DocumentFormat;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

@Slf4j
public class ConvertUtil {
    public static void main(String[] args) throws IOException {
        convert("E:\\tmp1\\wsnd.txt", "E:\\tmp1\\test.pdf");
    }

    public static void convert(String fromPath, String toPath) {
        File fromFile = new File(fromPath);
        if (!fromFile.exists()) {
            log.error("源文件不存在");
            return;
        }
        File toFile = new File(toPath);

        SocketOpenOfficeConnection connection = new SocketOpenOfficeConnection("你滴ip", 你设置滴端口号);
        try {
            connection.connect();
            StreamOpenOfficeDocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
            converter.convert(fromFile, toFile);
        } catch (ConnectException e) {
            log.error("获取连接失败", e);
        } finally {
            if (connection.isConnected()) {
                connection.disconnect();
            }
        }
    }
}

SpringBoot starter版

maven依赖

SpringBoot starter

    <dependency>
        <groupId>org.jodconverter</groupId>
        <artifactId>jodconverter-spring-boot-starter</artifactId>
        <version>4.4.2</version>
    </dependency>
	<dependency>
        <groupId>org.jodconverter</groupId>
        <artifactId>jodconverter-core</artifactId>
        <version>4.4.2</version>
    </dependency>
	<dependency>
        <groupId>org.jodconverter</groupId>
        <artifactId>jodconverter-local</artifactId>
        <version>4.4.2</version>
    </dependency>
	<dependency>
        <groupId>org.jodconverter</groupId>
        <artifactId>jodconverter-remote</artifactId>
        <version>4.4.2</version>
    </dependency>

自动装配配置类,可以看出有两种模式,local是调用本地的,remote是远程的,docker启openoffice online才能用

org.jodconverter.boot.autoconfigure.JodConverterLocalAutoConfiguration
org.jodconverter.boot.autoconfigure.JodConverterLocalProperties
org.jodconverter.boot.autoconfigure.JodConverterRemoteAutoConfiguration
org.jodconverter.boot.autoconfigure.JodConverterRemoteProperties

先不考虑这个,项目要集群,不能使用local模式;也不使用docker,不能使用remote模式

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值