kkFileView二开之pdf转图片接口

news/2025/2/15 14:08:18/

kkFileView二开之Pdf转图片接口

  • 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 部署

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 中,新增转换方法:

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 文件

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;@Autowiredprivate OfficeToPdfService officeToPdfService;@Autowiredprivate FileHandlerService fileHandlerService;private static final String FILE_DIR = ConfigConstants.getFileDir();/*** pdf转换为图片* @param req* @param rep* @param file*/@PostMapping("/pdf2Image")@ResponseBodypublic 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文件准备

在这里插入图片描述

pdf2Image_123">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.ppmy.cn/news/1572257.html

相关文章

Dify 框架连接 PGSQL 数据库与 Sandbox 环境下的 Linux 系统调用权限问题

Dify 框架连接 PGSQL 数据库与 Sandbox 环境下的 Linux 系统调用权限问题 背景 在使用 Dify 框架进行开发时&#xff0c;遇到了两个主要的技术挑战&#xff1a; 代码节点连接到 PGSQL&#xff08;PostgreSQL&#xff09;数据库。解决沙盒环境中由于系统调用限制导致的“oper…

左移架构 -- 从攒批,湖仓到使用数据流的实时数据产品

编辑导读: 这篇文章翻译自 Kai Waehner的 《The Shift Left Architecture – From Batch and Lakehouse to Real-Time Data Products with Data Streaming》。文章通过数据产品的概念引出了如何创建可重复使用的数据产品使企业能够从当前和未来的数据中获得价值。基于构建数据产…

Hyperledger Fabric 入门笔记(十八)Fabric V2.5 测试网络部署补充 - 排序节点管理

文章目录 一、准备工作二、手动为通道加入排序节点2.1. 生成加密材料与启动容器2.1.1. 为新增排序节点生成加密材料2.1.2. 启动新增的排序节点 2.2. 加入排序节点2.2.1. 获取最新的配置区块2.2.2. 将新增的排序节点加入到通道中 2.3. 通道配置更新2.3.1. 将通道配置转换为JSON格…

IoTDB 集群重启某节点失败

问题现象 IoTDB 1.3.3.6 版本部署的 3C3D 集群&#xff0c;在重启某个节点服务时失败&#xff0c;报错信息为节点冲突&#xff0c;日志部分截图如下&#xff1a; 问题原因 当前 IoTDB 会根据 data/confignode/system 路径下的 confignode-system.properties 文件及 data/data…

【EXCEL】【VBA】处理GI Log获得Surf格式的CONTOUR DATA

【EXCEL】【VBA】处理GI Log获得Surf格式的CONTOUR DATA data source1: BH coordination tabledata source2:BH layer tableprocess 1:Collect BH List To Layer Tableprocess 2:match Reduced Level from "Layer"+"BH"data source1: BH coordination…

Nginx location 和 proxy_pass 配置详解

概述 Nginx 配置中 location 和 proxy_pass 指令的不同组合方式及其对请求转发路径的影响。 配置效果 1. location 和 proxy_pass 都带斜杠 / location /api/ {proxy_pass http://127.0.0.1:8080/; }访问地址&#xff1a;www.hw.com/api/upload转发地址&#xff1a;http://…

[ComfyUI]腾讯开源黑科技Sonic,插件更新,更加可控啦

一、Sonic更新介绍 大家还记得我前分享过腾讯开源的Sonic这个项目吧&#xff0c;通过照片声音就可以生成非常不错的数字人开口说话的视频。 当时我就挺满意的&#xff0c;不过那时候输出还只能输出正方形的视频&#xff0c;这点就让我留有遗憾。 今天我再去翻作者的项目官网…

Git标签管理:从基础到高阶自动化实践

引言 在软件发布过程中&#xff0c;88%的生产事故与版本标记错误相关。Git标签&#xff08;Tag&#xff09;作为版本控制的关键锚点&#xff0c;不仅是发布流程的里程碑&#xff0c;更是代码审计和问题追溯的重要依据。本文将深入Git标签的底层机制&#xff0c;揭示企业级标签…