方法一:通过本地安装openoffice实现
步骤如下:
1.引入对应jar包
<!--jodconverter 核心包 -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.2.2</version>
</dependency>
<!--springboot支持包,里面包括了自动配置类 -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.2.2</version>
</dependency>
<!--jodconverter 本地支持包 -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.2.2</version>
</dependency>
2.在配置文件里面配置openoffice路径
# openOffice相关配置
jodconverter:
local:
enabled: true
#openOffice安装路径 (linux环境:/opt/openoffice4 windows环境:C:/Program Files (x86)/OpenOffice 4)
office-home: C:/Program Files (x86)/OpenOffice 4
max-tasks-per-process: 10
port-numbers: 8100
3.代码调用
//自动注入转换器
@Autowired
private DocumentConverter converter;
public Result toPdfFile(@PathVariable("filePath") String filePath) {
String url = new String(Base64.getDecoder().decode(filePath), StandardCharsets.UTF_8);
try {
File file = FileUtils.getFile(url);
//转换之后文件生成的地址
String outPath = new File("").getAbsolutePath() + "/outputPdfDir";
File newFile = new File(outPath);
if (!newFile.exists()) {
newFile.mkdirs();
}
//文件转化
converter.convert(file).to(new File(outPath + "/convert.pdf")).execute();
//使用response,将pdf文件以流的方式发送的前段
ServletOutputStream outputStream = response.getOutputStream();
// 读取文件
InputStream in = new FileInputStream(new File(outPath + "/convert.pdf"));
// copy文件
int i = IOUtils.copy(in, outputStream);
System.out.println(i);
in.close();
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return new Result();
}
方法二:通过ip+端口访问其他机器上的openoffice
1.引入jar包
<!--jodconverter 核心包 -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.2.2</version>
</dependency>
<!--springboot支持包,里面包括了自动配置类 -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.2.2</version>
</dependency>
<!--jodconverter 本地支持包 -->
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.2</version>
</dependency>
2.调用
只需将上面代码中
//文件转化
converter.convert(file).to(new File(outPath + "/convert.pdf")).execute();
替换成下面这段即可
//文件转化
SocketOpenOfficeConnection connection = new SocketOpenOfficeConnection("xx.xx.xx.xx", 8100);
connection.connect();
StreamOpenOfficeDocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
converter.convert(file,new File(outPath + "/convert.pdf"));
在此过程中遇到的一些坑:
1、openoffice在linux安装卸载,由于一开始openoffice装的版本不太对,后面想卸载发现怎么都卸载不干净,这里提供一个完全卸载的命令rpm -e `rpm -qa |grep openoffice` `rpm -qa |grep ooobasis`
2、本地调用openoffice转pdf正常,部署到服务器文件乱码。这是由于linux缺少windows环境下中文字体库,通过将C:\Windows\Fonts文件夹下字体,上传到/usr/share/fonts文件夹下,再通过fc-cache -fv命令更新字体缓存,然后重启openoffice即可解决
3、遗留的问题:excel转pdf如果超过了A4纸大小,转换会有问题,暂时没找到解决方案
4、报 unknown document format for file 错误,原因是通过ip+端口调用方式需引入jodconverter-2.2.2的jar,2.2.1版本无法转换docx,该jar包在maven无法找到,可以通过以下链接下载:
https://download.csdn.net/download/qq_19800029/20700197?spm=1001.2014.3001.5501
5、报connection refused错误,这是因为你在启动openoffice的时候,使用了soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard & 命令来启动,这种启动方式,只有本机才能访问openoffice服务,需要改成 soffice -headless -accept="socket,host=0.0.0.0,port=8100;urp;" -nofirststartwizard & 方式调用