栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ppt转pdf

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ppt转pdf

这里写自定义目录标题
  • 上传ppt转成pdf格式
    • 安装openoffice
    • 添加pom依赖
    • 在application.yml中进行配置
    • 将ppt文件转为pdf案例代码
    • Controller层代码
    • 最后

上传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;

@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;
}

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;

@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 “上传成功”;
} 最后

上传成功后即可在对应文件夹里查看

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/825326.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号