feat:下载PDF文件添加水印
This commit is contained in:
+66
-13
@@ -6,13 +6,12 @@ import com.adc.da.report.eo.FileEntity;
|
|||||||
import com.adc.da.report.eo.ReportEntity;
|
import com.adc.da.report.eo.ReportEntity;
|
||||||
import com.adc.da.report.service.IFileService;
|
import com.adc.da.report.service.IFileService;
|
||||||
import com.adc.da.report.service.IReportService;
|
import com.adc.da.report.service.IReportService;
|
||||||
import com.adc.da.report.util.LocalFileUtil;
|
import com.adc.da.report.util.*;
|
||||||
import com.adc.da.report.util.OSSClientUtil;
|
|
||||||
import com.adc.da.report.util.ObsBootUtil;
|
|
||||||
import com.adc.da.report.vo.ReportQueryVo;
|
import com.adc.da.report.vo.ReportQueryVo;
|
||||||
import com.adc.da.report.vo.ReportVo;
|
import com.adc.da.report.vo.ReportVo;
|
||||||
import com.adc.da.util.http.ResponseMessage;
|
import com.adc.da.util.http.ResponseMessage;
|
||||||
import com.adc.da.util.http.Result;
|
import com.adc.da.util.http.Result;
|
||||||
|
import com.adc.da.util.utils.FileUtil;
|
||||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
@@ -34,6 +33,8 @@ import java.text.ParseException;
|
|||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import static com.adc.da.report.constant.ReportConstants.FILE_EXTENSIONS_PDF;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author doudxw
|
* @Author doudxw
|
||||||
* @Date 2021/11/4 14:26
|
* @Date 2021/11/4 14:26
|
||||||
@@ -74,6 +75,9 @@ public class ReportManageConroller {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private ReportDao reportDao;
|
private ReportDao reportDao;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private WaterMarkUtil waterMarkUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 新增或编辑报告
|
* 新增或编辑报告
|
||||||
* @param vo
|
* @param vo
|
||||||
@@ -324,6 +328,40 @@ public class ReportManageConroller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// /**
|
||||||
|
// * 下载文件
|
||||||
|
// * @param response
|
||||||
|
// * @param fileId
|
||||||
|
// * @throws IOException
|
||||||
|
// */
|
||||||
|
// @ApiOperation(value = "|下载文件")
|
||||||
|
// @GetMapping("/download")
|
||||||
|
// public void downloadPic(HttpServletResponse response, String fileId) throws IOException {
|
||||||
|
//
|
||||||
|
// try {
|
||||||
|
// FileEntity entity = fileService.getFile(fileId);
|
||||||
|
// String fileName = entity.getFileName();
|
||||||
|
// String filename = URLEncoder.encode(fileName, "UTF-8");
|
||||||
|
// response.setHeader("Content-Disposition", "attachment;filename=" + filename);
|
||||||
|
// response.setContentType("application/x-download");
|
||||||
|
//// File file = new File(uploadFile, entity.getSavePath());
|
||||||
|
// // 上传方式:阿里云:alioss 华为云:hwobs
|
||||||
|
// if ("hwobs".equals(uploadType)){
|
||||||
|
// obsBootUtil.downFile(entity.getSavePath(), response);
|
||||||
|
// }else if ("alioss".equals(uploadType)){
|
||||||
|
// ossClientUtil.downloadFileFromOSS(entity.getSavePath(),response);
|
||||||
|
// }else if ("local".equals(uploadType)){
|
||||||
|
// localFileUtil.downFile(entity.getSavePath(), response);
|
||||||
|
// }
|
||||||
|
// if (FILE_EXTENSIONS_PDF.equalsIgnoreCase(FileUtil.getFileExtension(fileName))) {
|
||||||
|
// String text = "text";
|
||||||
|
// PdfUtil.addWatermarkToPdf(response, text);
|
||||||
|
// }
|
||||||
|
// } catch (Exception ex) {
|
||||||
|
// log.info(ex.getMessage(),ex);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 下载文件
|
* 下载文件
|
||||||
* @param response
|
* @param response
|
||||||
@@ -333,23 +371,38 @@ public class ReportManageConroller {
|
|||||||
@ApiOperation(value = "|下载文件")
|
@ApiOperation(value = "|下载文件")
|
||||||
@GetMapping("/download")
|
@GetMapping("/download")
|
||||||
public void downloadPic(HttpServletResponse response, String fileId) throws IOException {
|
public void downloadPic(HttpServletResponse response, String fileId) throws IOException {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FileEntity entity = fileService.getFile(fileId);
|
FileEntity entity = fileService.getFile(fileId);
|
||||||
String fileName = entity.getFileName();
|
String fileName = entity.getFileName();
|
||||||
String filename = URLEncoder.encode(fileName, "UTF-8");
|
String filename = URLEncoder.encode(fileName, "UTF-8");
|
||||||
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
|
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
|
||||||
response.setContentType("application/x-download");
|
response.setContentType("application/x-download");
|
||||||
// File file = new File(uploadFile, entity.getSavePath());
|
|
||||||
// 上传方式:阿里云:alioss 华为云:hwobs
|
|
||||||
if ("hwobs".equals(uploadType)){
|
|
||||||
obsBootUtil.downFile(entity.getSavePath(), response);
|
|
||||||
}else if ("alioss".equals(uploadType)){
|
|
||||||
ossClientUtil.downloadFileFromOSS(entity.getSavePath(),response);
|
|
||||||
}else if ("local".equals(uploadType)){
|
|
||||||
localFileUtil.downFile(entity.getSavePath(), response);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
//PDF文件需要加水印
|
||||||
|
if (FILE_EXTENSIONS_PDF.equalsIgnoreCase(FileUtil.getFileExtension(fileName))) {
|
||||||
|
String path = uploadFile + entity.getSavePath();
|
||||||
|
FileOutputStream outputStream = new FileOutputStream(path);
|
||||||
|
// 上传方式:阿里云:alioss 华为云:hwobs
|
||||||
|
if ("hwobs".equals(uploadType)) {
|
||||||
|
obsBootUtil.getFileFromOBS(entity.getSavePath(), outputStream);
|
||||||
|
} else if ("alioss".equals(uploadType)) {
|
||||||
|
ossClientUtil.getFileFromOSS(entity.getSavePath(), outputStream);
|
||||||
|
} else if ("local".equals(uploadType)) {
|
||||||
|
localFileUtil.getFileFromLocal(entity.getSavePath(), outputStream);
|
||||||
|
}
|
||||||
|
//给文件添加水印
|
||||||
|
waterMarkUtil.addWatermarkToPdf(new FileInputStream(path), response);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// 上传方式:阿里云:alioss 华为云:hwobs 本地:local
|
||||||
|
if ("hwobs".equals(uploadType)){
|
||||||
|
obsBootUtil.downFile(entity.getSavePath(), response);
|
||||||
|
}else if ("alioss".equals(uploadType)){
|
||||||
|
ossClientUtil.downloadFileFromOSS(entity.getSavePath(),response);
|
||||||
|
}else if ("local".equals(uploadType)){
|
||||||
|
localFileUtil.downFile(entity.getSavePath(), response);
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
log.info(ex.getMessage(),ex);
|
log.info(ex.getMessage(),ex);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,100 @@
|
|||||||
|
package com.adc.da.report.util;
|
||||||
|
|
||||||
|
|
||||||
|
import com.adc.da.login.util.UserUtils;
|
||||||
|
import com.adc.da.sys.constant.TsConfigKeyEnum;
|
||||||
|
import com.adc.da.sys.entity.UserEO;
|
||||||
|
import com.adc.da.sys.service.iservice.IConfigService;
|
||||||
|
import com.adc.da.sys.service.iservice.IUserEoService;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPage;
|
||||||
|
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||||
|
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||||
|
import org.apache.pdfbox.pdmodel.font.PDType0Font;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
|
||||||
|
import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode;
|
||||||
|
import org.apache.pdfbox.util.Matrix;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 水印工具类
|
||||||
|
* @author CaiHaohan
|
||||||
|
*/
|
||||||
|
@Component
|
||||||
|
public class WaterMarkUtil {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private IUserEoService userEoService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private IConfigService configService;
|
||||||
|
|
||||||
|
public String getWaterMarkConfig() {
|
||||||
|
LocalDate currentDate = LocalDate.now();
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
|
||||||
|
String formattedDate = currentDate.format(formatter);
|
||||||
|
|
||||||
|
UserEO user = userEoService.getUserById(UserUtils.getUserId());
|
||||||
|
String userName = user.getUsname();
|
||||||
|
|
||||||
|
String configValue = configService.findConfigValueByKey(TsConfigKeyEnum.WATERMARK_PREFIX.getValue());
|
||||||
|
|
||||||
|
return configValue + " " + "测试测试测试中文" + " " + formattedDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addWatermarkToPdf(FileInputStream inputStream, HttpServletResponse response) throws IOException {
|
||||||
|
String watermarkText = getWaterMarkConfig();
|
||||||
|
// 加载PDF文档
|
||||||
|
try (PDDocument document = PDDocument.load(inputStream)) {
|
||||||
|
// 设置水印字体、字体大小、颜色和透明度
|
||||||
|
ClassPathResource fontResource = new ClassPathResource("fonts/SourceHanSerif-VF.ttf");
|
||||||
|
PDType0Font font = PDType0Font.load(document, fontResource.getInputStream());
|
||||||
|
float fontSize = 13.0f;
|
||||||
|
Color color = new Color(100, 100, 100, 0);
|
||||||
|
PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
|
||||||
|
graphicsState.setNonStrokingAlphaConstant(0.4f);
|
||||||
|
graphicsState.setStrokingAlphaConstant(0.4f);
|
||||||
|
|
||||||
|
// 遍历每一页并添加水印
|
||||||
|
for (PDPage page : document.getPages()) {
|
||||||
|
// 获取页面尺寸以计算水印位置
|
||||||
|
PDRectangle pageSize = page.getMediaBox();
|
||||||
|
float xStep = pageSize.getWidth() / 4;
|
||||||
|
float yStep = pageSize.getHeight() / 4;
|
||||||
|
float rotationInRadians = (float) Math.toRadians(20);
|
||||||
|
|
||||||
|
for (float xPosition = pageSize.getLowerLeftX(); xPosition <= pageSize.getWidth(); xPosition += xStep) {
|
||||||
|
for (float yPosition = pageSize.getLowerLeftY(); yPosition <= pageSize.getHeight(); yPosition += yStep) {
|
||||||
|
// 创建内容流并设置图形状态参数
|
||||||
|
try (PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true)) {
|
||||||
|
contentStream.setGraphicsStateParameters(graphicsState);
|
||||||
|
contentStream.setFont(font, fontSize);
|
||||||
|
contentStream.setNonStrokingColor(color);
|
||||||
|
contentStream.beginText();
|
||||||
|
contentStream.setRenderingMode(RenderingMode.FILL);
|
||||||
|
// 设置水印文本的旋转和位置
|
||||||
|
contentStream.setTextMatrix(Matrix.getRotateInstance(rotationInRadians, xPosition, yPosition));
|
||||||
|
contentStream.showText(watermarkText);
|
||||||
|
contentStream.endText();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将处理后的PDF写入HTTP响应
|
||||||
|
try (OutputStream outputStream = response.getOutputStream()) {
|
||||||
|
document.save(outputStream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
@@ -77,6 +77,15 @@ public class ConfigServiceImpl extends ServiceImpl<ConfigDao, ConfigEntity>
|
|||||||
return Result.error("配置项Key不存在");
|
return Result.error("配置项Key不存在");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String findConfigValueByKey(String key) {
|
||||||
|
if (isKeyValid(key)) {
|
||||||
|
ConfigEntity configEntity = query().eq("config_key", key).one();
|
||||||
|
return configEntity.getConfigValue();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断传入的key是否是后端预设好的key
|
* 判断传入的key是否是后端预设好的key
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -36,4 +36,12 @@ public interface IConfigService extends IService<ConfigEntity> {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
ResponseMessage findConfig(String key);
|
ResponseMessage findConfig(String key);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回一个配置项
|
||||||
|
*
|
||||||
|
* @param key
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
String findConfigValueByKey(String key);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user