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编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis