- 导入jar包 ( 我这里解决了下log4j冲突的问题)
<!-- ofd转pdf -->
<dependency>
<groupId>org.ofdrw</groupId>
<artifactId>ofdrw-converter</artifactId>
<version>1.17.9</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
- 其实重点就这一行代码 ConvertHelper.toPdf(ofdPath, pdfPath);
(两个参数分别是: 源文件地址, 新文件地址)
/**
* ofd转pdf
* @param fileUrl
* @param fileName
*/
@GetMapping("/pdf")
public void pdfConvert(String fileUrl, String fileName) {
if (fileName.endsWith("ofd")) {
String name = fileName.replace(".ofd", ".pdf");
//获取文件的上级全路径
File parentFile = new File(fileUrl).getParentFile();
//文件的输入路径
Path ofdPath = Paths.get(fileUrl);
// 转换后pdf的路径
Path pdfPath = Paths.get(parentFile + "\\" + name);
//OFD转PDF
ConvertHelper.toPdf(ofdPath, pdfPath);
}
}
这样就可以生成新的文件啦~