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实战之利用POI生成Excel图表

《Java实战之利用POI生成Excel图表》ApachePOI是Java生态中处理Office文档的核心工具,这篇文章主要为大家详细介绍了如何在Excel中创建折线图,柱状图,饼图等常见图表,需要的... 目录一、环境配置与依赖管理二、数据源准备与工作表构建三、图表生成核心步骤1. 折线图(Line Ch

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元

Java中将异步调用转为同步的五种实现方法

《Java中将异步调用转为同步的五种实现方法》本文介绍了将异步调用转为同步阻塞模式的五种方法:wait/notify、ReentrantLock+Condition、Future、CountDownL... 目录异步与同步的核心区别方法一:使用wait/notify + synchronized代码示例关键

Java 8 Stream filter流式过滤器详解

《Java8Streamfilter流式过滤器详解》本文介绍了Java8的StreamAPI中的filter方法,展示了如何使用lambda表达式根据条件过滤流式数据,通过实际代码示例,展示了f... 目录引言 一.Java 8 Stream 的过滤器(filter)二.Java 8 的 filter、fi

Java中实现订单超时自动取消功能(最新推荐)

《Java中实现订单超时自动取消功能(最新推荐)》本文介绍了Java中实现订单超时自动取消功能的几种方法,包括定时任务、JDK延迟队列、Redis过期监听、Redisson分布式延迟队列、Rocket... 目录1、定时任务2、JDK延迟队列 DelayQueue(1)定义实现Delayed接口的实体类 (

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

springboot的调度服务与异步服务使用详解

《springboot的调度服务与异步服务使用详解》本文主要介绍了Java的ScheduledExecutorService接口和SpringBoot中如何使用调度线程池,包括核心参数、创建方式、自定... 目录1.调度服务1.1.JDK之ScheduledExecutorService1.2.spring

将java程序打包成可执行文件的实现方式

《将java程序打包成可执行文件的实现方式》本文介绍了将Java程序打包成可执行文件的三种方法:手动打包(将编译后的代码及JRE运行环境一起打包),使用第三方打包工具(如Launch4j)和JDK自带... 目录1.问题提出2.如何将Java程序打包成可执行文件2.1将编译后的代码及jre运行环境一起打包2