实现环境:要求可以在linux环境下运行,开源插件 , 跨平台
以下有五种方法:
结论:
office不支持跨平台,
aspose收费,
基于pio+itext转换后会严重失真,
基于libreOffice不支持插入图形,否则会造成文本发生走样
而openOffice各种效果,效率……都是未测的。。。。
总结:目前仅考虑openOffice和libreOffice
openOffice实战
原理:
通过第三方工具openOffice,将word、excel、ppt、txt等文件转换为pdf文件
先安装openOffice软件(Windows或Linux有提供软件)
使用JODConverter的Java的OpenDocument 文件转换器API操作Office系列文件转换为PDF文件
优点:
转换效果比较好。是比较主流的做法
缺点:
服务器需要安装openOffice,比较负重。启动服务时效率不是很高
具体实现:
1.下载安装软件
1)Openoffice:Apache下的一个开放免费的文字处理软件
2)JODConverter一个Java的OpenDocument 文件转换器,只用到它的jar包
下载地址:JODConverter - Browse /JODConverter at SourceForge.net
安装openoffice
1.安装linux的openoffice
1.Apache OpenOffice - 下载 去官网链接下载linux版本的openOffice 以4.1.2 版本为例。
2.将压缩包上传至服务器上,并进行解压安装。
1 tar -zxvf 对应的压缩包名字 2 cd 进入解压后的 /zh-cn/RPMS 3 yum localinstall *.rpm 4 cd desktop-integration 5 rpm -ivh 含redhat的
默认会安装在/opt目录下会出现openoffice4。
3.启动服务
1 /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard 临时启动 2 nohup /opt/openoffice4/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & 后台启动
端口号根据自己项目实际来确定。
后台启动会一直占用内存,据各路大神说 大概100M,我自己没测过具体值不清楚。
有的程序是需要预先启动openOffice 服务的,有的则在代码里自己启动服务。
4.查看进程
netstat -lnp |grep 端口号
大概显示成这样就算启动完了。
`tcp 0 0 127.0.0.1:8100 0.0.0.0:* LISTEN 14362``/soffice``.bin`
2.安装window的openoffice
比linux的更简单,安装好后,只需要在C:\Program Files (x86)\OpenOffice 4\program执行下面的命令就可以了 // soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststa
linux开机,重启openoffice服务(linux下openoffice启动和自动启动设置(centos)_resolute123的博客-CSDN博客_linux openoffice重启)
编辑vi /etc/rc.local
添加如下命令
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
重启linux,命令reboot netstat -lnp |grep 8100 - - - 查看是否会出现以下信息,表示成功 tcp 0 0 127.0.0.1:8100 0.0.0.0:* LISTEN 8220/soffice.bin
2.Java实现
依赖
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.4</version&g