根据传过来的word转换为pdf

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

k5003

您的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值