1.Java代码编写和依赖
https://blog.csdn.net/asing1elife/article/details/82963363
①纠正文中错误:
下载 JODConverter ,在其 lib 目录中找到 jodconverter-2.2.2.jar ,并引入以下 jar 包
补充:我是在src/main/resources/下建立了lib目录,并把jar包放进去,然后pom配置
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.2</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/jodconverter-2.2.2.jar</systemPath>
</dependency>
<build>
<!-- 解决了打包时缺失本地jar包问题 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webResources>
<resource>
<directory>src/main/resources/lib</directory>
<targetPath>WEB-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</build>
②有歧义的地方
File pdfFile = new File(fileName.substring(0, fileName.lastIndexOf(".")) + Constants.REPORT_FILE_PREVIEW_SUFFIX);
OpenOfficeConnection connection = new SocketOpenOfficeConnection(Constants.OPEN_OFFICE_CONNECTION_PORT);
Constants.REPORT_FILE_PREVIEW_SUFFIX 改成 ".pdf"
Constants.OPEN_OFFICE_CONNECTION_PORT 改成 "127.0.0.1",8100
其实就是openOffice的IP和端口。
2.openoffice4安装和启动
①补充:下载openOffice时,centos的系统选择下载rpm版本的,Ubuntu系统选择下载deb的
②补充:启动openOffice时,执行
/opt/openoffice4/program/soffice "-accept=socket,host=127.0.0.1,port=8100;urp;" -headless -nofirststartwizard &
127.0.0.1只能本地连接,改成0.0.0.0则可以远程连接。
3.openOffice4启动报错no suitable windowing system found, exiting.
执行以下命令:
yum groupinstall "X Window System"
4.springboot项目集成openOffice后启动报错
org\openoffice\lib' must exist and must be a directory
解决:根据报错提示,直接在本地仓库对应目录建lib或bin目录
5.文件转换错误
OpenOfficeException: conversion failed: could not load input document
解决:
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
改成
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
6.转成PDF字体方块乱码(原因:系统缺少对应的字体)
①下载linux字体包:
链接:https://pan.baidu.com/s/1xR9ujoYFVXoiv8LNtbcu1w
提取码:wd8h
②解压放到linux的/usr/share/fonts/路径下
③刷新字体缓存
fc-cache 如果提示 fc-cache: command not found,则需要安装# yum install fontconfig )
④kill掉openOffice服务,然后重新启动