kkFileView二开之pdf转图片接口

打印 上一主题 下一主题

主题 1027|帖子 1027|积分 3081

kkFileView二开系列文章:


  • kkFileView二开之源码编译及部署
  • kkFileView二开之内外网转换
  • kkFileView二开之word转pdf接口
  • kkFileView二开之Excel转pdf接口
  • kkFileView二开之pdf转图片接口
  • kkFileView二开之企业级安全题目处理
    对应二开代码堆栈:https://gitee.com/wbj_1/kk-file-view
1 kkFileView源码下载及编译

前文 【kkFileView二开之源码编译及部署】 已完成了kkFileView源码二开的基础预备。
2 Pdf转图片接口

2.1 背景

在实际工作过程中,存在Pdf转图片的需求,好比职员证书,通过pdf模板填充后,天生对应的图片。
2.2 分析

kkFiewView 针对pdf在线预览会有两种方式,一种是转换为图片举行预览,一种是保存原始pdf格式举行预览,此处可以调用kkfiewView底层中pdf转图片预览的方式,实现对应的接口
2.2 接口开发

2.2.1 编写Pdf转图片方法

在cn.keking.service.FileHandlerService.java 中,新增转换方法:
  1. /**
  2.      * pdf转换为图片
  3.      * @param pdfFilePath
  4.      * @param fileAttribute
  5.      * @return
  6.      * @throws Exception
  7.      */
  8.     public List<String> pdf2jpgBase64(String pdfFilePath,FileAttribute fileAttribute) throws Exception {
  9.         String filePassword = fileAttribute.getFilePassword();
  10.         PDDocument doc = null;
  11.         List<String> imageFile = new ArrayList<>();
  12.         try {
  13.             File pdfFile = new File(pdfFilePath);
  14.             if (!pdfFile.exists()) {
  15.                 return null;
  16.             }
  17.             doc = Loader.loadPDF(pdfFile, filePassword);
  18.             doc.setResourceCache(new NotResourceCache());
  19.             int pageCount = doc.getNumberOfPages();
  20.             PDFRenderer pdfRenderer = new PDFRenderer(doc);
  21.             for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
  22.                 BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, ConfigConstants.getPdf2JpgDpi(), ImageType.RGB);
  23.                 imageFile.add(ImgUtil.toBase64DataUri(image,"jpg"));
  24.             }
  25.         } catch (IOException e) {
  26.             logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e);
  27.             throw new Exception(e);
  28.         } finally {
  29.             if (doc != null) {   //关闭
  30.                 doc.close();
  31.             }
  32.         }
  33.         return imageFile;
  34.     }
复制代码
2.2.2 编写转换接口

在cn.keking.web.controller包下,新增ConvertController.java 文件
  1. package cn.keking.web.controller;
  2. import cn.hutool.core.io.FileUtil;
  3. import cn.keking.config.ConfigConstants;
  4. import cn.keking.model.FileAttribute;
  5. import cn.keking.model.FileType;
  6. import cn.keking.service.FileHandlerService;
  7. import cn.keking.service.OfficeToPdfService;
  8. import cn.keking.utils.KkFileUtils;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Controller;
  11. import org.springframework.web.bind.annotation.PostMapping;
  12. import org.springframework.web.bind.annotation.RequestParam;
  13. import org.springframework.web.bind.annotation.ResponseBody;
  14. import org.springframework.web.multipart.MultipartFile;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import java.io.File;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. /**
  22. * 文件转换接口
  23. */
  24. @Controller
  25. public class ConvertController {
  26.     private final String fileDir = ConfigConstants.getFileDir();
  27.     //临时目录
  28.     private final String tempPath = "temp" + File.separator;
  29.     @Autowired
  30.     private OfficeToPdfService officeToPdfService;
  31.     @Autowired
  32.     private FileHandlerService fileHandlerService;
  33.     private static final String FILE_DIR = ConfigConstants.getFileDir();
  34.    
  35.     /**
  36.      * pdf转换为图片
  37.      * @param req
  38.      * @param rep
  39.      * @param file
  40.      */
  41.     @PostMapping("/pdf2Image")
  42.     @ResponseBody
  43.     public Map<String,Object> pdf2Image(HttpServletRequest req, HttpServletResponse rep, @RequestParam("file") MultipartFile file) {
  44.         Map<String,Object> result = new HashMap<>();
  45.         FileAttribute fileAttribute = new FileAttribute();
  46.         String fullFileName = file.getOriginalFilename();
  47.         fileAttribute.setType(FileType.typeFromFileName(fullFileName));
  48.         fileAttribute.setName(fullFileName);
  49.         fileAttribute.setSuffix(KkFileUtils.suffixFromFileName(fullFileName));
  50.         try {
  51.             String pdfName = fullFileName.substring(0, fullFileName.lastIndexOf(".") + 1) + "pdf";
  52.             String outFilePath = FILE_DIR + pdfName;
  53.             FileUtil.writeFromStream(file.getInputStream(),outFilePath);
  54.             List<String> imageUrls = fileHandlerService.pdf2jpgBase64(outFilePath, fileAttribute);
  55.             result.put("code",200);
  56.             result.put("msg","转换成功");
  57.             result.put("data",imageUrls);
  58.         }catch (Exception e){
  59.             e.printStackTrace();
  60.             result.put("code",500);
  61.             result.put("msg","pdf转换图片异常:"+e.getMessage());
  62.         }
  63.         return result;
  64.     }
  65. }
复制代码
2.3 接口测试

2.3.1 Pdf文件预备


2.3.2 pdf2Image

使用Apifox新建接口,按如下方式配置,并点击发送
留意:通过源码分析可知,在Pdf举行预览过程中,预览速率会随着pdf的巨细差别而差别,pdf越大,则接口速率越慢,由于是一次性将对应的pdf全部转换后返回至前端的。

效果格式化效果(pdf文件有85页,所以data中有85条数据)

按如下方式,将天生的每一条数据写入到img标签中
  1. <!DOCTYPE html>
  2. <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.         <meta charset="utf-8" />
  5.         <title>New Document</title>
  6. </head>
  7. <body>
  8.         <img src="data标签中的每一行数据" alt="" />
  9. </body>
  10. </html>
复制代码
在浏览器中打开编写的html文件,如效果图所示,即为转换后的base64图片

3 部署

可参考 【kkFileView二开之源码编译及部署】 文档中,【部署】目录下的方式,根据部署的平台选择符合的方式举行部署。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

愛在花開的季節

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表