若依中整合JodConverter实现文件在线预览

第一步 安装Libreoffice
在线安装:
Ubuntu
sudo apt update
sudo apt install libreoffice
Centos
sudo yum update
sudo yum install libreoffice

如果安装失败记得更新数据源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sudo yum clean all
sudo yum makecache
sudo yum update

离线安装:

下载地址:LibreOffice 官网

cd /opt
解压文字编码和libreoffice
tar -zxvf LibreOffice_7.5.9.2_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
tar -zxvf LibreOffice_7.5.9.2_Linux_x86-64_rpm.tar.gz

安装LibreOffice
cd LibreOffice_7.5.9.2_Linux_x86-64_rpm/RPMS/
yum localinstall *.rpm
cd LibreOffice_7.5.9.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/

安装字体编码
yum localinstall *.rpm

验证是否安装成功
libreoffice7.5 --version

卸载LibreOffice
yum remove libreoffice*

更新依赖
yum install -y libreoffice-headless

报错更新依赖
yum install cairo -y
yum install cups-libs -y
yum install libSM -y

第二步 maven依赖

<dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-core</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-local</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.jodconverter</groupId>
            <artifactId>jodconverter-spring-boot-starter</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.libreoffice</groupId>
            <artifactId>ridl</artifactId>
            <version>5.4.2</version>
        </dependency>
        

第三步 配置DocumentConverter

jodconverter:
local:
enabled: true
# 设置LibreOffice主目录
# office-home: /opt/libreoffice6.4
office-home: C:/Program Files/LibreOffice
# 开启多个LibreOffice进程,每个端口对应一个进程
portNumbers: 9080,9081,9089
# LibreOffice进程重启前的最大进程数
maxTasksPerProcess: 100

第四步 使用DocumentConverter
使用@Autowire注入 documentConverter

@Autowired
    private DocumentConverter documentConverter;

    #使用流的方式转换PDF
    documentConverter.convert(inputStream)
                            .as(fileExtension.compareToIgnoreCase(DefaultDocumentFormatRegistry.DOC.getExtension()) == 0 ? DefaultDocumentFormatRegistry.DOC : DefaultDocumentFormatRegistry.DOCX)
                            .to(outputStream)
                            .as(DefaultDocumentFormatRegistry.PDF)
                            .execute();
    #使用文件方式转换成PDF
                documentConverter
                    .convert(oldFile)
                    .to(file)
                    .execute();
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值