在查询文档在线预览功能时看到许多都是需要jodconverter2.2.1,但是这版本不支持docx等格式,需要2.2.2版本,但是2.2.2没有mavne依赖,这对于使用springboot的人来说可真难受,又不想重新创建lib单独引入,又想要支持的格式全一点,在找了好几个示例之后终于找到一个可以使用的版本,在此进行一个记录,希望可以帮助到其他人
在使用前必须先安装openoffice
安装包地址 https://www.openoffice.org/download/
这里不使用
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.1</version>
</dependency>
而是使用
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.2.2</version>
</dependency>
<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>
在application.yml添加对应的配置
jodconverter:
local:
enabled: true
office-home: C:/Program Files (x86)/OpenOffice 4
max-tasks-per-process: 10
port-numbers: 8100
在需要进行文件类型转换的controller或者service中注入
@Autowired
private DocumentConverter converter;
转换文件关键语句
converter.convert(filePath).to(pdfFile).execute();
其中filePath
为原本文档FIle,而pdfFile
是转换后的PDF对应的File,执行该语句之后就可以去对应的文件夹下找到转换后的PDF了。