当前位置: 首页 > article >正文

kkFileView二开之pdf转图片接口

kkFileView二开之Pdf转图片接口

  • kkFileView二开系列文章:
  • 1 kkFileView源码下载及编译
  • 2 Pdf转图片接口
    • 2.1 背景
    • 2.2 分析
    • 2.2 接口开发
      • 2.2.1 编写Pdf转图片方法
      • 2.2.2 编写转换接口
    • 2.3 接口测试
      • 2.3.1 Pdf文件准备
      • 2.3.2 pdf2Image
  • 3 部署

kkFileView二开系列文章:

  1. kkFileView二开之源码编译及部署
  2. kkFileView二开之内外网转换
  3. kkFileView二开之word转pdf接口
  4. kkFileView二开之Excel转pdf接口
  5. kkFileView二开之pdf转图片接口
  6. 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 中,新增转换方法:

/**
     * pdf转换为图片
     * @param pdfFilePath
     * @param fileAttribute
     * @return
     * @throws Exception
     */
    public List<String> pdf2jpgBase64(String pdfFilePath,FileAttribute fileAttribute) throws Exception {
        String filePassword = fileAttribute.getFilePassword();
        PDDocument doc = null;
        List<String> imageFile = new ArrayList<>();
        try {
            File pdfFile = new File(pdfFilePath);
            if (!pdfFile.exists()) {
                return null;
            }
            doc = Loader.loadPDF(pdfFile, filePassword);
            doc.setResourceCache(new NotResourceCache());
            int pageCount = doc.getNumberOfPages();
            PDFRenderer pdfRenderer = new PDFRenderer(doc);
            for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
                BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, ConfigConstants.getPdf2JpgDpi(), ImageType.RGB);
                imageFile.add(ImgUtil.toBase64DataUri(image,"jpg"));
            }
        } catch (IOException e) {
            logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e);
            throw new Exception(e);
        } finally {
            if (doc != null) {   //关闭
                doc.close();
            }
        }
        return imageFile;
    }

2.2.2 编写转换接口

在cn.keking.web.controller包下,新增ConvertController.java 文件

package cn.keking.web.controller;

import cn.hutool.core.io.FileUtil;
import cn.keking.config.ConfigConstants;
import cn.keking.model.FileAttribute;
import cn.keking.model.FileType;
import cn.keking.service.FileHandlerService;
import cn.keking.service.OfficeToPdfService;
import cn.keking.utils.KkFileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件转换接口
 */
@Controller
public class ConvertController {
    private final String fileDir = ConfigConstants.getFileDir();
    //临时目录
    private final String tempPath = "temp" + File.separator;
    @Autowired
    private OfficeToPdfService officeToPdfService;
    @Autowired
    private FileHandlerService fileHandlerService;
    private static final String FILE_DIR = ConfigConstants.getFileDir();
    
    /**
     * pdf转换为图片
     * @param req
     * @param rep
     * @param file
     */
    @PostMapping("/pdf2Image")
    @ResponseBody
    public Map<String,Object> pdf2Image(HttpServletRequest req, HttpServletResponse rep, @RequestParam("file") MultipartFile file) {
        Map<String,Object> result = new HashMap<>();
        FileAttribute fileAttribute = new FileAttribute();
        String fullFileName = file.getOriginalFilename();
        fileAttribute.setType(FileType.typeFromFileName(fullFileName));
        fileAttribute.setName(fullFileName);
        fileAttribute.setSuffix(KkFileUtils.suffixFromFileName(fullFileName));
        try {
            String pdfName = fullFileName.substring(0, fullFileName.lastIndexOf(".") + 1) + "pdf";
            String outFilePath = FILE_DIR + pdfName;
            FileUtil.writeFromStream(file.getInputStream(),outFilePath);
            List<String> imageUrls = fileHandlerService.pdf2jpgBase64(outFilePath, fileAttribute);
            result.put("code",200);
            result.put("msg","转换成功");
            result.put("data",imageUrls);
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",500);
            result.put("msg","pdf转换图片异常:"+e.getMessage());
        }
        return result;
    }
}

2.3 接口测试

2.3.1 Pdf文件准备

在这里插入图片描述

2.3.2 pdf2Image

使用Apifox新建接口,按如下方式配置,并点击发送
注意:通过源码分析可知,在Pdf进行预览过程中,预览速度会随着pdf的大小不同而不同,pdf越大,则接口速度越慢,因为是一次性将对应的pdf全部转换后返回至前端的。
在这里插入图片描述
结果格式化效果(pdf文件有85页,所以data中有85条数据)
在这里插入图片描述
按如下方式,将生成的每一条数据写入到img标签中

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta charset="utf-8" />
	<title>New Document</title>
</head>
<body>
	<img src="data标签中的每一行数据" alt="" />
</body>
</html>

在浏览器中打开编写的html文件,如效果图所示,即为转换后的base64图片
在这里插入图片描述

3 部署

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


http://www.kler.cn/a/549984.html

相关文章:

  • Kafka 为什么会丢消息?如何保证消息不丢失?
  • Java GC 基础知识快速回顾
  • TK矩阵系统:全面提升TikTok运营效率的智能化工具
  • 【Vue3 入门到实战】16. Vue3 非兼容性改变
  • 最新智能优化算法:牛优化( Ox Optimizer,OX)算法求解经典23个函数测试集,MATLAB代码
  • 缓存穿透、缓存击穿、缓存雪崩的区别与解决方案
  • 【transformers.Trainer填坑】在自定义compute_metrics时logits和labels数据维度不一致问题
  • 基于LSTM的情感分析
  • Dockerfile 编写推荐
  • SQL-leetcode—1683. 无效的推文
  • Linux Mem -- AArch64 MTE功能Tag寄存器
  • Redis五种用途
  • 【LeetCode】15.三数之和
  • TDengine 数据备份/还原工具 taosdump
  • vue点击左边导航,右边出现页面步骤
  • 【GO】Golang/C++混合编程 - 初识
  • Linux 目录结构与基础命令学习记录
  • Spring AI发布!让Java紧跟AI赛道!
  • openEuler 22.03 LTS SP4源码编译部署OpenStack-Dalmatian
  • 云原生(五十五) | ECS中自建数据库迁移到RDS