From 3de6b0f4a3a6c21c8f89ce818aa53057a4db1c03 Mon Sep 17 00:00:00 2001 From: zhn <947514737@qq.com> Date: Thu, 9 Jun 2022 17:35:37 +0800 Subject: [PATCH] =?UTF-8?q?pdf=E9=A2=84=E8=A7=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/controller/CommonController.java | 88 ++++++++++++++++--- .../jero/modules/system/util/PDFUtils.java | 82 ++++++++++++----- 2 files changed, 137 insertions(+), 33 deletions(-) diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java index 9293b3018..96ade5cb4 100644 --- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/controller/CommonController.java @@ -481,24 +481,23 @@ public class CommonController { } InputStream inputStream = null; OutputStream outputStream = null; + String fileName = ""; try { - String fileName = ""; //本地下载 String filePath = ossFile.getUrl(); - File file = new File(filePath); - //水印内容 - String waterContent = "water mark";//临时定义(水印信息通过传过来) - File newFile = PDFUtils.PDFWatermark(file,waterContent); - String path = newFile.getPath(); -// String substring = path.substring(filePath.length(), path.length()); - - if(!file.exists()){ + boolean b = CosBootUtil.doesObjectExist(filePath); + if (!b) { response.setStatus(404); throw new RuntimeException("文件不存在.."); } + InputStream download = CosBootUtil.download(filePath); + File file = new File(filePath); + //水印内容 + String waterContent = "water mark";//临时定义(水印信息通过传过来) + File newFile = PDFUtils.PDFWatermark(download,uploadpath,file.getName(),waterContent); // 文件名称 fileName = file.getName(); - inputStream = new BufferedInputStream(new FileInputStream(path)); + inputStream = new BufferedInputStream(new FileInputStream(newFile)); response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1")); response.setContentType("application/force-download");// 设置强制下载不打开 outputStream = response.getOutputStream(); @@ -527,9 +526,78 @@ public class CommonController { log.error(e.getMessage(), e); } } + File fileTemp = new File(uploadpath + File.separator +fileName); + fileTemp.delete(); } } +// /** +// * 文件预览PDF +// * +// * @param id 传入文件id +// * @param request +// * @param response +// */ +// @GetMapping(value = "/pdf/viewFile") +// public void viewFile(String id,HttpServletRequest request, HttpServletResponse response) { +// // 查询数据表数据是否存在 +// LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); +// queryWrapper.eq(OSSFile::getId,id); +// OSSFile ossFile = ossFileService.getOne(queryWrapper); +// if( null == ossFile){ +// throw new JeroBootException("文件不存在.."); +// } +// InputStream inputStream = null; +// OutputStream outputStream = null; +// try { +// String fileName = ""; +// //本地下载 +// String filePath = ossFile.getUrl(); +// File file = new File(filePath); +// //水印内容 +// String waterContent = "water mark";//临时定义(水印信息通过传过来) +// File newFile = PDFUtils.PDFWatermark(file,waterContent); +// String path = newFile.getPath(); +//// String substring = path.substring(filePath.length(), path.length()); +// +// if(!file.exists()){ +// response.setStatus(404); +// throw new RuntimeException("文件不存在.."); +// } +// // 文件名称 +// fileName = file.getName(); +// inputStream = new BufferedInputStream(new FileInputStream(path)); +// response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1")); +// response.setContentType("application/force-download");// 设置强制下载不打开 +// outputStream = response.getOutputStream(); +// byte[] buf = new byte[1024]; +// int len; +// while ((len = inputStream.read(buf)) > 0) { +// outputStream.write(buf, 0, len); +// } +// response.flushBuffer(); +// } catch (IOException e) { +// log.error("文件下载失败" + e.getMessage()); +// response.setStatus(404); +// e.printStackTrace(); +// } finally { +// if (inputStream != null) { +// try { +// inputStream.close(); +// } catch (IOException e) { +// log.error(e.getMessage(), e); +// } +// } +// if (outputStream != null) { +// try { +// outputStream.close(); +// } catch (IOException e) { +// log.error(e.getMessage(), e); +// } +// } +// } +// +// } /** * @功能:pdf预览Iframe diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/util/PDFUtils.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/util/PDFUtils.java index 8b974fb56..223724870 100644 --- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/util/PDFUtils.java +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/util/PDFUtils.java @@ -1,10 +1,25 @@ package com.jero.modules.system.util; -import com.spire.pdf.*; -import com.spire.pdf.graphics.*; +import com.spire.pdf.PdfDocument; +import com.spire.pdf.PdfPageBase; +import com.spire.pdf.graphics.PdfBrushes; +import com.spire.pdf.graphics.PdfFont; +import com.spire.pdf.graphics.PdfFontFamily; +import com.spire.pdf.graphics.PdfStringFormat; +import com.spire.pdf.graphics.PdfTextAlignment; +import com.spire.pdf.graphics.PdfTilingBrush; + import java.awt.*; -import java.awt.geom.*; -import java.io.*; +import java.awt.geom.Dimension2D; +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; public class PDFUtils { public static void main(String[] args) throws IOException { @@ -69,43 +84,64 @@ public class PDFUtils { page.getCanvas().drawRectangle(brush, loRect); } - public static File PDFWatermark(File file,String waterMark) throws FileNotFoundException { - InputStream in = new FileInputStream(file) ; -// byte[] byteArray=File2byte(new File("f:ceshi/1.pdf")); + public static File PDFWatermark(InputStream in,String uploadpath, String fileName,String waterMark) throws FileNotFoundException { //创建PdfDocument类的对象 PdfDocument pdf = new PdfDocument(); //加载测试文档 pdf.loadFromStream(in); -// pdf.loadFromFile(file.getPath()); //为了去掉在第一页多余的水印,加一页,后边删除 pdf.getPages().add(); - //获取测试文档中的第一页 for(int i=0; i