栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

(二)java项目中的文档转换案例实战——PDF转换为JPG图片压缩包

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

(二)java项目中的文档转换案例实战——PDF转换为JPG图片压缩包

前言

由于在开发中需要适配不同的多端应用,在文件相关处理中也会存在相同的问题需要将文档转换为不同的格式展示,本节我们主要通过 一个小案例实现在 java环境下实现 PDF转换为JPG图片压缩包。

正文
  • 引入转换pdf的pom工具包

	net.sf.cssbox
	pdf2dom
	1.7


	org.apache.pdfbox
	pdfbox
	2.0.12


	org.apache.pdfbox
	pdfbox-tools
	2.0.12

  •  后端转换代码
    @ApiOperation(value = "pdf转换为jpg")
    @PostMapping(value = "pdfToJpg")
    public void pdfToJpg(HttpServletResponse response, @RequestPart("file") MultipartFile file) {
        try {
            PDdocument doc = PDdocument.load(file.getInputStream());
            //遍历处理pdf附件
            int size = doc.getNumberOfPages();
            PDFRenderer reader = new PDFRenderer(doc);
            //文件临时存储目录
            String location = System.getProperty("user.dir") + "/" + dirTmp;
            for (int i = 0; i < size; i++) {
                BufferedImage bufferedImage = reader.renderImage(i, 3f);
                //生成图片,保存位置
                if (!FileUtil.exist(location + "/out/")) {
                    File fileOut = new File(location + "/out/");
                    if (!fileOut.exists()) {
                        fileOut.mkdirs();
                    }
                }
                FileOutputStream out = new FileOutputStream(location + "/out/" + i + ".jpg");
                ImageIO.write(bufferedImage, "jpg", out);
                out.close();
            }
            String destination = location + "/in/" + UUID.randomUUID() + ".zip";
            String source = location + "/out";
            ZipUtil.zip(source, destination);
            //需要编码否则中文乱码
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(IdWorker.getIdStr() + ".zip", "UTF-8"));
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setContentType("application/zip;charset=utf-8");
            response.setCharacterEncoding("UTF-8");
            //将打包后的文件写到客户端,输出的方法同上,使用缓冲流输出
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(destination));
            byte[] buff = new byte[bis.available()];
            bis.read(buff);
            bis.close();
            OutputStream out = response.getOutputStream();
            //输出数据文件
            out.write(buff);
            //释放缓存
            out.flush();
            //关闭输出流
            out.close();
            FileUtil.del(source);
            FileUtil.del(destination);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }

  •   vue前端代码





.container {
  padding: 10px;

  .title {
    font-size: 20px;
    font-weight: bold;
  }
}

  • 验证结果

 

结语

ok,本节内容到这里就结束了,我们下期见。。。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/683521.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号