ppt转pdf
这里写自定义目录标题
上传ppt转成pdf格式
在用SpringBoot做文件上传时,可能会遇到ppt转成pdf的问题,针对此问题有以下的解决方案
安装openoffice
本次转换使用的是apache的openoffice, 安装地址,根据自己电脑系统安装即可
添加pom依赖
org.jodconverter jodconverter-spring-boot-starter 4.2.2 org.jodconverter jodconverter-core 4.2.2 org.jodconverter jodconverter-local 4.2.2
在application.yml中进行配置
jodconverter:
local:
office-home: C:/Program Files (x86)/OpenOffice 4 (安装目录,我的是windows下此目录)
max-tasks-per-process: 10 (每个任务最大进程数)
port-numbers: 8000 (端口号)
enabled: true
task-queue-timeout: 80000 (任务超时时间,可自行设置)
将ppt文件转为pdf案例代码
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.jodconverter.DocumentConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/**
-
@ClassName OfficeUtil
-
@Author: wan
-
@Date:Create:in 2022/4/9
*/
@Slf4j
@Component
@RestController
public class OfficeUtil {
//pdf文件的保存地址,自行设置
private static final String savePath = “D:\4\”;
private static DocumentConverter converter;
@Autowired
public OfficeUtil(DocumentConverter converter) {
OfficeUtil.converter = converter;
}/**
- @Description: ppt转pdf
- @author: wan
- @date: 2022/4/9 16:25
*/
public static void pptToPdf(String pptPath){
File file = new File(pptPath);
//因为需要获取上传的ppt文件名字,所以使用了字符串分割,如不需要,可以删掉
String mid=“.”;
String substring = pptPath.substring(5, pptPath.indexOf(mid));
System.out.println(substring);
String pdfPath;
try {
//转换之后文件生成的地址
File newFile = new File(savePath);
if (!newFile.exists()) {
boolean mkdirs = newFile.mkdirs();
}
pdfPath = savePath + substring + “.pdf”;
System.out.println(pdfPath);
File pdfFile = new File(pdfPath);
System.out.println(pdfFile);
//文件转化
converter.convert(file).to(pdfFile).execute();
} catch (Exception e) {
e.printStackTrace();
}
}
Controller层代码
import com.wan.Service.PPTService;
import com.wan.domain.PPT;
import com.wan.utils.OfficeUtil;
import com.wan.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
- @author wan
- @version 1.0
- @date 2022/4/6 9:18
*/
@RestController
@CrossOrigin
public class pptController {
//上传的ppt文件保存路径,在yml文件中设置
@Value(“${file.PPTPath}”)
private String pptPath;
@PostMapping(“/up”)
public Result upload(@ModelAttribute MultipartFile file) throws IOException {
String originalFilename = file.getOriginalFilename();
String path=pptPath;
file.transferTo(new File(path+originalFilename));
OfficeUtil.pptToPdf(path+originalFilename);
return “上传成功”;
}
最后
上传成功后即可在对应文件夹里查看