在线预览"/>
Linux环境Libreoffice实现Word、Excel等在线预览
目录
转PDF
一、Linux安装libreoffice
二、Java代码实现
这里介绍的是在linux环境(windows环境类似)下实现讲word、Excel、ppt、txt以及png图片转换为PDF文件后实现的预览。由于需要转换为PDF文件,当Excel表格太大太宽的时候,可能出现换页等格式被破坏的情况。
一、Linux安装libreoffice
从官网下载对应版本的libreoffice
下载地址:/
下载完成后,把包上传到服务器。
1、安装步骤网上很多,大致如下:
(1)、解压文件
tar -zxvf LibreOffice_7.1.0.2_Linux_x86-64_rpm.tar.gz
(2)、进入文件RPMS目录下
cd /opt/libreoffice7.1/LibreOffice_7.1.0.2_Linux_x86-64_rpm/RPMS
(3)、安装rpm文件
执行命令:yum localinstall *.rpm (或rpm -Uivh *.rpm --nodeps)。默认安装到opt/下
2、安装后一般Linux环境可能缺乏一些依赖,从而导致libreoffice会启动失败的情况。
(1)
Caused by: org.jodconverter.office.OfficeException: A process with acceptString 'socket,host=127.0.0.1,port=8100,tcpNoDelay=1;urp;StarOffice.ServiceManager' started but its pid could not be found
报错的原因是因为项目目录出现中文!!!
(2)
/opt/libreoffice7.0/program/soffice.bin: error while loading shared libraries: libSM.so.6: cannot open shared object file: No such file or directory
sudo yum install libSM
sudo yum install libICE
sudo yum install libX11-xcb
(3)
error while loading shared libraries: libXext.so.6: cannot open shared object file: No such file or directory.
sudo yum install libXext.x86_64
(4)
/opt/libreoffice5.3/program/oosplash: error while loading shared libraries: libXinerama.so.1: cannot open shared object file: No such file or directory
sudo yum isntall -y libXinerama
(5)
/opt/libreoffice5.3/program/soffice.bin: error while loading shared libraries: libcairo.so.2: cannot open shared object file: No such file or directory
sudo yum install -y ibus
3、PDF文件预览中文出现乱码
[app@centos76-m-pod4-03-i-6 RPMS]$ libreoffice7.4 --headless --invisible --convert-to pdf /tmp/test.doc --outdir /tmp/
javaldx: Could not find a Java Runtime Environment!
Warning: failed to read path from javaldx
convert /tmp/test.doc -> /tmp/test.pdf using filter : writer_pdf_Export
安装完成后,按如上执行如上命令进行转换,得到的PDF文档如下:
出现乱码的问题一般是libreoffice不支持中文字体,可以将windows下(C:\Windows\Fonts目录)的字体库打包上传到libreoffice目录下
/opt/libreoffice5.3/share/fonts/truetype
fc-list #查看字体列表
mkfontscale #建立字体缓存
mkfontdir
fc-cache -fv #刷新缓存
存放字体的目录放在/usr/share/fonts下面
可以在这个目录下创建一个目录,在目录里放你的字体,记住目录权限是755,字体权限是644,完事刷新一下,不行重启。
执行以下命令即可:
另外将字体copy到目录下 /usr/share/fonts
sudo mkfontscale
sudo mkfontdir
#-f强制扫描,-v过程
sudo fc-cache -fv
如果提示mkfontscale命令未找到,则安装
sudo yum install mkfontscale
二、Java代码实现
1、引入maven依赖
<!--jodconverter 核心包 -->
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-core</artifactId><version>4.4.6</version>
</dependency><!--springboot支持包,里面包括了自动配置类 -->
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-spring-boot-starter</artifactId><version>4.4.6</version>
</dependency><!--jodconverter 本地支持包 -->
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-local-lo</artifactId><version>4.4.6</version>
</dependency>
LibreOffice 和OpenOffice依赖的包不一样,详见GitHub - jodconverter/jodconverter: JODConverter automates document conversions using LibreOffice or Apache OpenOffice.
With LibreOffice libraries:
Gradle:
implementation 'org.jodconverter:jodconverter-local-lo:4.4.6'
Maven:
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-local-lo</artifactId><version>4.4.6</version> </dependency>
With OpenOffice libraries:
Gradle:
implementation 'org.jodconverter:jodconverter-local:4.4.6'
or
implementation 'org.jodconverter:jodconverter-local-oo:4.4.6'
Maven:
<dependency><groupId>org.jodconverter</groupId><artifactId>jodconverter-local</artifactId><version>4.4.6</version> </dependency>
2、以@Bean方式添加配置DocumentConverter(也可以用yml添加)
@Configuration
public class JodConverterConfig {private final String linuxOfficeHome = "/opt/libreoffice7.4";private final String windowsOfficeHome = "E:\\software\\LibreOffice";@Bean(initMethod = "start",destroyMethod = "stop")public OfficeManager officeManager(){String os = System.getProperty("os.name").toLowerCase();return LocalOfficeManager.builder().officeHome(os.contains("windows") ? windowsOfficeHome : linuxOfficeHome).portNumbers(9080,9081).maxTasksPerProcess(100).build();}@Bean@ConditionalOnMissingBean@ConditionalOnBean({OfficeManager.class})public DocumentConverter jodConverter(OfficeManager officeManager) {return LocalConverter.make(officeManager);}
}
3、自动装配转换类DocumentConverter并实现转换
@Resource(name = "jodConverter")private DocumentConverter jodConverter;---------其他代码---------// 转为PDF(输入为InputStream,然后向前端输出OutputStream文件流-----看自己的具体场景进行改动)jodConverter.convert(response.body().asInputStream()).to(httpResponse.getOutputStream()).as(DefaultDocumentFormatRegistry.PDF).execute();
更多推荐
Linux环境Libreoffice实现Word、Excel等在线预览
发布评论