mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/per-boe/java-servers.git
synced 2025-12-11 20:06:51 +08:00
110 lines
3.2 KiB
Java
110 lines
3.2 KiB
Java
package com.xboe.converter;
|
|
|
|
import java.io.*;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import com.aspose.slides.Presentation;
|
|
import com.xboe.common.utils.StringUtil;
|
|
import com.xboe.core.SysConstant;
|
|
import com.xboe.module.course.service.ICourseFileConverter;
|
|
import com.xboe.standard.BaseConstant;
|
|
|
|
/**
|
|
* ppt转化为pdf
|
|
* <p>
|
|
* https://www.cnblogs.com/name-lizonglin/p/12836451.html
|
|
*
|
|
* @author seastar
|
|
*/
|
|
@Slf4j
|
|
@Component
|
|
public class PPTToPdfConverter implements ICourseFileConverter {
|
|
|
|
private static String types = "ppt,pptx";
|
|
|
|
private static boolean loadLicense = false;
|
|
|
|
/**
|
|
* 获取license
|
|
*
|
|
* @return
|
|
*/
|
|
public boolean getLicense() {
|
|
if (!loadLicense) {
|
|
InputStream is=null;
|
|
try {
|
|
is = this.getClass().getResourceAsStream("/aspose/license.xml");
|
|
com.aspose.slides.License aposeLic = new com.aspose.slides.License();
|
|
aposeLic.setLicense(is);
|
|
loadLicense = true;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
if(is!=null){
|
|
try {
|
|
is.close();
|
|
} catch (IOException e) {
|
|
log.error("关流异常",e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return loadLicense;
|
|
}
|
|
|
|
@Override
|
|
public boolean canConvert(String fileType) {
|
|
//检查是否可以转化
|
|
if (StringUtil.isNotBlank(fileType) && types.indexOf(fileType.toLowerCase()) > -1) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public String convert(String fileType, String filePath) throws Exception{
|
|
if (filePath.contains("..")) {
|
|
throw new SecurityException("输入路径包含不安全的字符");
|
|
}
|
|
|
|
if (this.getLicense()) {
|
|
InputStream slides=null;
|
|
Presentation pres=null;
|
|
File file=null;
|
|
FileOutputStream fileOS=null;
|
|
String previewPath = null;
|
|
try {
|
|
String savePath = SysConstant.getConfigValue(BaseConstant.CONFIG_UPLOAD_FILES_SAVEPATH);
|
|
previewPath = filePath.substring(0, filePath.indexOf(fileType)) + "pdf";
|
|
slides = new FileInputStream(new File(savePath + filePath));// 原始ppt路径
|
|
pres = new Presentation(slides);
|
|
file = new File(savePath + previewPath);// 输出pdf路径
|
|
//设置文件属性
|
|
//.setAttribute(file, "dos:archive", false);
|
|
|
|
fileOS = new FileOutputStream(file);
|
|
|
|
pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf);
|
|
} catch (FileNotFoundException e) {
|
|
// e.printStackTrace();
|
|
log.error("转化异常",e);
|
|
}finally {
|
|
if (fileOS!=null) {
|
|
try {
|
|
fileOS.close();
|
|
} catch (IOException e) {
|
|
log.error("关流异常",e);
|
|
}
|
|
}
|
|
}
|
|
|
|
return previewPath;
|
|
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|