Springboot+Libreoffice集成开发

2023-12-13 13:12

本文主要是介绍Springboot+Libreoffice集成开发,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简介

LibreOffice 是一款功能强大的办公软件,默认使用开放文档格式 (OpenDocument Format , ODF), 并支持 *.docx, *.xlsx, *.pptx 等其他格式。
它包含了 Writer, Calc, Impress, Draw, Base 以及 Math 等组件,可用于处理文本文档、电子表格、演示文稿、绘图以及公式编辑。
它可以运行于 Windows, GNU/Linux 以及 macOS 等操作系统上,并具有一致的用户体验。

官网:链接
安装
SpringBoot word文档转pdf
centos7下安装LibreOffice

在线 - 安装

安装环境: centos7

# 查找yum源的安装包
& yum search libreoffice
# 查看Libreoffice版本
& yum info libreoffice# 安装
& yum install -y libreoffice# 建立软连接
ln -s /usr/bin/libreoffice7.0 /usr/bin/libreoffice# 测试word转PDF
& # 重启
/opt/libreoffice6.4/program/soffice --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard &# 查看端口号
$ netstat -anop | grep 8100

在线 - 指定版本安装

下载地址:ibreoffice7.5.8

LibreOffice_7.5.8_Linux_x86-64_rpm_sdk.tar.gz
LibreOffice_7.5.8_Linux_x86-64_rpm.tar.gz

依赖

LibreOffice需要依赖libcairo.so.2,libcups.so.2,libSM.so.6 等yum install cairo -y
yum install cups-libs -y
yum install libSM -y
/
yum install cairo cups-libs libSM -y
  1. 创建安装目录
mkdir /opt/libreoffice
  1. 下载安装包到安装目录并解压
[root@top213 libreoffice]# ll
总用量 287672
LibreOffice_7.5.8_Linux_x86-64_rpm_sdk.tar.gz
LibreOffice_7.5.8_Linux_x86-64_rpm.tar.gz# 解压
tar -xvzf LibreOffice_7.5.8_Linux_x86-64_rpm_sdk.tar.gz
tar -xvzf LibreOffice_7.5.8_Linux_x86-64_rpm.tar.gz
  1. 安装
cd /opt/libreoffice/LibreOffice_7.5.8.2_Linux_x86-64_rpm/RPMS
yum localinstall -y *.rpm# 喝杯92年的白开水休息一会儿cd /opt/libreoffice/LibreOffice_7.5.8.2_Linux_x86-64_rpm_sdk/RPMS
yum localinstall -y *.rpm
  1. 查看是否安装成功
[root@localhost RPMS]# libreoffice7.5 --version
LibreOffice 7.5.8.2 f718d63693263970429a68f568db6046aaa9df01

测试

libreoffice7.5 --headless --convert-to pdf xxxx.docx#打印结果 convert /home/libreofficedemo/xxxx.docx -> /home/libreofficedemo/xxxx.pdf using filter : writer_pdf_Export

离线 - 安装(未成功过勿使用)

# 下载yumdownloadonly插件
yum install yum-plugin-downloadonly# 下载到指定目录(依赖包会一起下载)
yum install --downloadonly --downloaddir=/root/httpd httpdrpm -ivh *.rpm
rpm -Uvh update/*.rpm# 使用rpm命令升级软件包时,有时候可能会与原来的安装的软件版本冲突。添加参数替换掉已有文件
rpm -Uvh --replacefiles *.rpm

启动服务

libreoffice6.4 --headless --accept="socket,host=0.0.0.0,port=8100;urp;" --nofirststartwizard
或者
/opt/libreoffice6.4/program/soffice --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard &netstat -nalp | grep 8100

停止服务

卸载软件

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

Springboot集成Libreoffice

  • 添加依赖
 <!-- jodconverter  word转pdf --><dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-core</artifactId><version>4.2.2</version></dependency><dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-spring-boot-starter</artifactId><version>4.2.2</version></dependency><dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-local</artifactId><version>4.2.2</version></dependency>
  • 添加配置
jodconverter:local:enabled: truemax-tasks-per-process: 10port-numbers: 8100office-home: ${office-home} # 这里填写安装目录
  • 核心代码
# 引入依赖
@Autowired
private DocumentConverter documentConverter;# 核心代码public MultipartFile docToPdf(MultipartFile file) {try {long old = System.currentTimeMillis();ByteArrayOutputStream os = new ByteArrayOutputStream();documentConverter.convert(file.getInputStream()).as(DefaultDocumentFormatRegistry.DOC).to(os).as(DefaultDocumentFormatRegistry.PDF).execute();long now = System.currentTimeMillis();log.info("doc转pdf耗时" + ((now - old) / 1000.0) + "秒");MultipartFile files = new MockMultipartFile("file", file.getName() + ".pdf", null, os.toByteArray());return files;} catch (Exception e) {e.printStackTrace();}return null;}

异常

1.Word转PDF后文字位置错乱

原来一直使用centos自带版本,后使用解压版后程序中指定安装目录后,解决此问题

2. Word转PDF 中文乱码

首次安装没有中文字体支持

# 检查系统中是否支持中文字体
fc-list
# 终端输入:yum -y install cups-libs fontconfig
# 成功后,在/usr/share目录就可以看到fonts和fontconfig目录了(之前是没有的)# 在window上将中文字体上传到libreoffice安装目录 /share/fonts/lll/
重启libreoffice

2. Could not read embedded OTF for font BCDEEE+MicrosoftYaHei

3. /lib64/libc.so.6: version `GLIBC_2.14’ not found

[root@top213 /]# libreoffice6.4 --headless --accept="socket,host=0.0.0.0,port=8100;urp;" --nofirststartwizard
/opt/libreoffice6.4/program/oosplash: /lib64/libz.so.1: version `ZLIB_1.2.3.4' not found (required by /opt/libreoffice6.4/program/oosplash)
/opt/libreoffice6.4/program/oosplash: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by /opt/libreoffice6.4/program/oosplash)
/opt/libreoffice6.4/program/oosplash: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.18' not found (required by /opt/libreoffice6.4/program/libuno_sal.so.3)
/opt/libreoffice6.4/program/oosplash: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by /opt/libreoffice6.4/program/libuno_sal.so.3)# 问题: /lib64/libc.so.6: version `GLIBC_2.14' not found# 1. 下载上传并解压
http://ftp.gnu.org/gnu/glibc/glibc-2.15.tar.gz
http://ftp.gnu.org/gnu/glibc/glibc-ports-2.15.tar.gz # 2. 解压
tar -zxvf  glibc-2.15.tar.gz
tar -zxvf  glibc-ports-2.15.tar.gz
mv glibc-ports-2.15 glibc-2.15/ports
mkdir glibc-build-2.15    
cd glibc-build-2.15   
../glibc-2.15/configure  --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin
make
make install

这篇关于Springboot+Libreoffice集成开发的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/488591

相关文章

Java堆转储文件之1.6G大文件处理完整指南

《Java堆转储文件之1.6G大文件处理完整指南》堆转储文件是优化、分析内存消耗的重要工具,:本文主要介绍Java堆转储文件之1.6G大文件处理的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言文件为什么这么大?如何处理这个文件?分析文件内容(推荐)删除文件(如果不需要)查看错误来源如何避

SpringBoot整合Dubbo+ZK注册失败的坑及解决

《SpringBoot整合Dubbo+ZK注册失败的坑及解决》使用Dubbo框架时,需在公共pom添加依赖,启动类加@EnableDubbo,实现类用@DubboService替代@Service,配... 目录1.先看下公共的pom(maven创建的pom工程)2.启动类上加@EnableDubbo3.实

SpringBoot整合(ES)ElasticSearch7.8实践

《SpringBoot整合(ES)ElasticSearch7.8实践》本文详细介绍了SpringBoot整合ElasticSearch7.8的教程,涵盖依赖添加、客户端初始化、索引创建与获取、批量插... 目录SpringBoot整合ElasticSearch7.8添加依赖初始化创建SpringBoot项

JAVA覆盖和重写的区别及说明

《JAVA覆盖和重写的区别及说明》非静态方法的覆盖即重写,具有多态性;静态方法无法被覆盖,但可被重写(仅通过类名调用),二者区别在于绑定时机与引用类型关联性... 目录Java覆盖和重写的区别经常听到两种话认真读完上面两份代码JAVA覆盖和重写的区别经常听到两种话1.覆盖=重写。2.静态方法可andro

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

Java docx4j高效处理Word文档的实战指南

《Javadocx4j高效处理Word文档的实战指南》对于需要在Java应用程序中生成、修改或处理Word文档的开发者来说,docx4j是一个强大而专业的选择,下面我们就来看看docx4j的具体使用... 目录引言一、环境准备与基础配置1.1 Maven依赖配置1.2 初始化测试类二、增强版文档操作示例2.

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命