package com.fssi.pdfconvert.service.pdfpreview.impl;
import com.fssi.framework.common.utils.CommonUtil;
import com.fssi.pdfconvert.config.ConfigConstants;
import com.fssi.pdfconvert.model.ReturnResponse;
import com.fssi.pdfconvert.model.pdfpreview.FileAttributeMsg;
import com.fssi.pdfconvert.service.pdfpreview.PdfPreviewService;
import com.fssi.pdfconvert.utils.DownloadUtils;
import com.fssi.pdfconvert.utils.FileUtils;
import com.fssi.pdfconvert.utils.OfficeToPdf;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/**
* @author king
* @from 生成pdf并转成base64字符串
* 根据传过来的word转换
* @dare 2020.10.27 15:17
*/
@Service
public class PdfPreviewServiceImpl implements PdfPreviewService {
@Resource
private FileUtils fileUtils;
@Resource
private DownloadUtils downloadUtils;
@Resource
private OfficeToPdf officeToPdf;
private static final String FILE_DIR = ConfigConstants.getFileDir();
/**
* 根据传递过来的对象数据转换为pdf的base64字符串
* @param fileAttributeMsg 对象
* @return
*/
@Override
public Map<String,Object> filePreviewHandle(FileAttributeMsg fileAttributeMsg) {
Map<String,Object> kmap=new HashMap<String,Object>();
kmap.put("success",true);
InputStream is =null;
try {
// 预览Type,参数传了就取参数的,没传取系统默认
String suffix=fileAttributeMsg.getSuffix();
String fileName=fileAttributeMsg.getFileName();
String base64str="";
//判断是否excel格式
boolean isHtml = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx");
//传过来的文件格式
String senfType=fileAttributeMsg.getSuffix();
//转换为pdf或html格式
String fileType = (isHtml ? "html" : "pdf");
String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + fileType;
String outFilePath = FILE_DIR + pdfName;
kmap.put("fileName",pdfName);
kmap.put("fileType",fileType);
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
if (!fileUtils.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
String filePath;
ReturnResponse<String> response = downloadUtils.downLoadbase64(fileAttributeMsg, null);
Thread.sleep(500);//休眠500毫秒
if (0 != response.getCode()) {
kmap.put("success",false);
kmap.put("msg",response.getMsg());
return kmap;
}
//获取信息内容
filePath = response.getContent();
//判断是否存在路径文件信息(如果存文件并且传递的文件格式为pdf)
if (StringUtils.hasText(outFilePath) && !"pdf".equals(senfType.toLowerCase())) {
officeToPdf.openOfficeToPDF(filePath, outFilePath);
Thread.sleep(1000);//休眠1秒
//如果是html格式
if (isHtml) {
// 对转换后的文件进行操作(改变编码方式)
fileUtils.doActionConvertedFile(outFilePath);
Thread.sleep(500);//休眠500毫秒
}
//如果没有缓存,缓存三个小时清除()
if (ConfigConstants.isCacheEnabled()) {
// 加入缓存
fileUtils.addConvertedFile(pdfName, fileUtils.getRelativePath(outFilePath));
}
is = new FileInputStream(outFilePath);
base64str= CommonUtil.toByteString(is);
kmap.put("base64str",base64str);
}
}else{//从路径中取文件信息
//根据路径获取base64字符串
if("pdf".equals(senfType.toLowerCase())){
kmap.put("base64str",fileAttributeMsg.getBase64Str());
}else{
is = new FileInputStream(outFilePath);
base64str= CommonUtil.toByteString(is);
kmap.put("base64str",base64str);
}
}
//如果是html再次执行
if(isHtml){
FileAttributeMsg kfileAttributeMsg=new FileAttributeMsg();
kfileAttributeMsg.setBase64Str((String)kmap.get("base64str"));
kfileAttributeMsg.setSuffix(fileType);
kfileAttributeMsg.setFileName(pdfName);
filePreviewHandle(kfileAttributeMsg);
}
}catch (Exception e) {
e.printStackTrace();
kmap.put("success",false);
kmap.put("msg","出现异常,"+e.getMessage());
return kmap;
}finally {
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return kmap;
}
}
根据传过来的word转换为pdf
最新推荐文章于 2022-06-20 09:13:02 发布