pdf下载添加水印

This commit is contained in:
zhn
2022-06-30 15:36:31 +08:00
parent 93033faac8
commit f0b7a5c054
2 changed files with 34 additions and 8 deletions
@@ -4,10 +4,10 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jero.common.api.vo.Result;
import com.jero.common.constant.CommonConstant;
import com.jero.common.constant.enums.CutEnum;
import com.jero.common.exception.JeroBootException;
import com.jero.common.system.api.ISysBaseAPI;
import com.jero.common.system.vo.LoginUser;
import com.jero.common.util.RestUtil;
import com.jero.common.util.TokenUtils;
import com.jero.common.util.oConvertUtils;
@@ -19,6 +19,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
@@ -47,6 +48,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
@@ -341,7 +344,7 @@ public class CommonController {
* @param response
*/
@GetMapping(value = "/downLoadFile")
public void downLoadFromCos(String id,HttpServletRequest request, HttpServletResponse response) {
public void downLoadFromCos(String id,HttpServletRequest request, HttpServletResponse response,String userName) {
if(StringUtils.isBlank(id)){
throw new JeroBootException("参数信息不全");
}
@@ -355,6 +358,7 @@ public class CommonController {
}
InputStream inputStream = null;
OutputStream outputStream = null;
File newFile =null;
try {
String fileName = "";
//本地下载
@@ -367,9 +371,18 @@ public class CommonController {
fileName = ossFile.getFileName();
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("UTF-8"),"iso-8859-1"));
InputStream download = CosBootUtil.download(filePath);
File file = new File(filePath);
if(file.getName().endsWith(".pdf") || file.getName().endsWith(".PDF")){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(new Date());
String waterContent = userName+" "+currentTime;
newFile = PDFUtils.PDFWatermark(download,uploadpath,file.getName(),waterContent);
inputStream = new FileInputStream(newFile.getPath());
}else{
inputStream = CosBootUtil.download(filePath);
}
outputStream = response.getOutputStream();
inputStream = CosBootUtil.download(filePath);
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
@@ -394,7 +407,17 @@ public class CommonController {
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
if (newFile != null) {
try {
newFile.delete();
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}
}
@@ -468,7 +491,7 @@ public class CommonController {
* @param response
*/
@GetMapping(value = "/pdf/viewFile")
public void viewFile(String id,HttpServletRequest request, HttpServletResponse response) {
public void viewFile(String id,HttpServletRequest request, HttpServletResponse response,String userName) {
// 查询数据表数据是否存在
LambdaQueryWrapper<OSSFile> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(OSSFile::getId,id);
@@ -490,7 +513,10 @@ public class CommonController {
InputStream download = CosBootUtil.download(filePath);
File file = new File(filePath);
//水印内容
String waterContent = "water mark";//临时定义(水印信息通过传过来)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(new Date());
String waterContent = userName+" "+currentTime;
// String waterContent = "water mark";//临时定义(水印信息通过传过来)
File newFile = PDFUtils.PDFWatermark(download,uploadpath,file.getName(),waterContent);
// 文件名称
fileName = file.getName();
@@ -73,9 +73,9 @@ public class PDFUtils {
PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
brush.getGraphics().setTransparency(0.4F);
brush.getGraphics().save();
brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 5, (float) brush.getSize().getHeight() / 5);
brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 1.5, (float) brush.getSize().getHeight() / 3);
brush.getGraphics().rotateTransform(-45);
brush.getGraphics().drawString(watermark, new PdfFont(PdfFontFamily.Helvetica, 15),
brush.getGraphics().drawString(watermark, new PdfFont(PdfFontFamily.Helvetica, 10),
PdfBrushes.getViolet(), 0 , 0 , new PdfStringFormat(PdfTextAlignment.Center));
brush.getGraphics().restore();
brush.getGraphics().setTransparency(1);