服务端PPT转PDF支持前段查看PPT

最近有个在线查看PPT的需求,找了一圈也没一个很好的方案在前段查看,就想着把PPT转成PDF传到OSS上让前段查看

 @ApiOperation(value = "上传单张图片", notes = "fileType 1:art 2:temp ")
        @RequestMapping(value = "upload_img", method = RequestMethod.POST)
        public Object uploadImg(@ApiParam(value = "file", required = true, name = "file")
                                @RequestParam("file")
                                        MultipartFile file, @ApiParam(value = "businessType", required = true, name = "businessType")
                                @RequestParam("businessType") String businessType) {
            String fileName = "";
            try {
                Map<String, OSSResult> resultMap = Maps.newHashMap();
                fileName = file.getOriginalFilename();
                System.out.println(fileName.lastIndexOf(".") + "_" + fileName);
                String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
                OSSResult ossResult = null;
                if(0 == fileType.compareTo("ppt") || 0 == fileType.compareTo("pptx"))
                {
                  String armFile = PPTToPDF(file);
                  InputStream input = new FileInputStream(armFile);
                  ossResult= OSSUtil.uploadImage(input, "pdf",businessType);
                  File tempfile = new File(armFile);
                    if (tempfile.exists()) {
                        if (tempfile.isFile()) {
                            tempfile.delete();
                        }
                    }
                }else{
                    ossResult = OSSUtil.uploadImage(file.getInputStream(), fileType, businessType);
                }

                if (ossResult == null) {
                    return unSuccessResponse(ResponseCodeEnum.EXCEPTION);
                }
                System.out.println("ossResult:"+ossResult);
                resultMap.put("img", ossResult);
                return jsonMsg(true, ResponseCodeEnum.SUCCESS.getCode(), "upload success", resultMap);
            } catch (Exception e) {
                logger.error(e.getMessage());
                return unSuccessResponse(ResponseCodeEnum.EXCEPTION);
            }
        }

//OSS 后台上传接口

private String MultipartFileToFile(MultipartFile file) {
      if (file != null) {
        try {
          String rootpath = System.getProperty("user.home");
          String fileRealName = file.getOriginalFilename();//获得原始文件名;
          String filePath = rootpath+"/file_doc/"+UUID
              .randomUUID().toString().toUpperCase().replace("-", "");
          File path = new File(filePath); //判断文件路径下的文件夹是否存在,不存在则创建
          if (!path.exists()) {
            path.mkdirs();
          }
          String fileName = filePath+"/"+file.getOriginalFilename();
          try {
            file.transferTo(new File(fileName));
          }catch (Exception e) {
          }
          return fileName;
        } catch (Exception e) {
          e.printStackTrace();
          return "";
        }
      } else {
        return "";
      }
    }

//文件流转化成本地文件

 private String PPTToPDF(MultipartFile file)
        {
          try {
            String ogrpath = MultipartFileToFile(file);
            String path=ogrpath.substring(0,ogrpath.lastIndexOf("/"));
            String handler =  "/usr/bin/libreoffice  --invisible --convert-to pdf --outdir " + path + " " + ogrpath;
            String tempstr = ogrpath.substring(0,ogrpath.lastIndexOf(".")) +".pdf";
            Runtime.getRuntime().exec(handler).waitFor();
            return ogrpath.substring(0,ogrpath.lastIndexOf(".")) +".pdf";
          }catch (Exception ex)
          {
              logger.error(ex.getMessage());
              ex.printStackTrace();
        return "";
          }
        }

调用libreoffice 在服务器端进行PPT转PDF

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值