pdf增加水印目录,导出配置文件名

master
linjj 2 years ago
parent 2c327b1720
commit 3d2d880d1d

@ -1,6 +1,7 @@
package com.emr.controller.templateSearch;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.emr.annotation.OptionalLog;
import com.emr.controller.FormTokenFlagUtil;
import com.emr.dao.Archive_DetailMapper;
@ -39,6 +40,7 @@ import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
@ -50,6 +52,7 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.ZipEntry;
@ -104,6 +107,11 @@ public class TemplateSearchController {
private CommomService commomService;
@Autowired
private Archive_DetailMapper archiveDetailMapper;
@Value("${export_pdf_hospital_info}")
private String hospitaInfo;
@Value("${export_pdf_patient_info}")
private String patientInfo;
/**
* @MethodName templateList
@ -1441,29 +1449,15 @@ public class TemplateSearchController {
List<ScanPathVo> scanPathVos = scanPathMapper.selectScanFileByBloodPatientIds1(patientIds, assortIds, flag);
if (null != scanPathVos && !scanPathVos.isEmpty()) {
EmrPdfWaterSet emrPdfWaterSet = pdfWaterSetMapper.selectByPrimaryKey(1);
//文件路径集合
List<String> filePaths = new ArrayList<>();
for (ScanPathVo scanPathVo : scanPathVos) {
String fileRealPath = scanPathVo.getFileRealPath();
if (StringUtils.isNoneBlank(fileRealPath)) {
filePaths.add(fileRealPath);
}
}
//批量添加下载记录
printOrDownLoadInfoService.SimpleInsert(scanPathVos, typeId, Short.valueOf("2"));
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
ScanPathVo vo = scanPathVos.get(0);
String disDate = "";
if (null != vo.getDisDate()) {
disDate = fmt.format(vo.getDisDate());
}
//每个文件名
String pdfName = vo.getInpatientNo().trim() + "-" + vo.getName().trim() + "-" + disDate.trim() + "_" + fmt.format(new Date());
String pdfName = exportFlieName(scanPathVos);
//emrPdfWaterSet设置启用下载的状态
emrPdfWaterSet.setEffective(emrPdfWaterSet.getDownloadEffective());
emrPdfWaterSet.setIsImg(emrPdfWaterSet.getDownloadIsImg());
EmrPdfWaterSet emrPdfWaterSet1 = commomService.getEmrPdfWaterSet(emrPdfWaterSet);
img2PdfUtil.imageToPdf(response, filePaths, pdfName, emrPdfWaterSet,emrPdfWaterSet1);
imgToPdfUtil.imageToPdf(response, scanPathVos, pdfName, emrPdfWaterSet,emrPdfWaterSet1);
}else {
String fileRealPath=null;
List<CommomTree> commomTrees = archiveDetailMapper.selectPdfPathByPatient3(patientIds, assortIds);
@ -1493,6 +1487,28 @@ public class TemplateSearchController {
}
}
private String exportFlieName(List<ScanPathVo> scanPathVos) throws ParseException {
StringBuffer sb = new StringBuffer();
sb.append(hospitaInfo);
ScanPathVo scanPathVo = scanPathVos.get(0);
JSONObject jsonObject= (JSONObject) JSONObject.toJSON(scanPathVo);
List<String> patientInfoList = Arrays.asList(patientInfo.split(","));
for (String list:patientInfoList){
String contents = jsonObject.getString(list).trim();
if (list.equals("disDate")){
String dateString = jsonObject.getString(list);
SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat targetFormat = new SimpleDateFormat("yyyyMMdd");
Date date = originalFormat.parse(dateString);
String formattedDate = targetFormat.format(date);
sb.append("_"+formattedDate);
}else {
sb.append("_"+contents);
}
}
return sb.toString();
}
/**
*
*

@ -0,0 +1,82 @@
package com.emr.util;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
/***
*
*
* https://blog.csdn.net/qq_38749525/article/details/127362432
*/
public class ImageToPdfConverte1 {
public static void main(String[] args) throws Exception {
// 读取图片文件,此图片可以通过路径读取指定文件夹的所有图片进行指定。
java.util.List<String> imageFiles = new ArrayList<>();
imageFiles.add("D:\\test\\image1.jpeg");
imageFiles.add("D:\\test\\image2.jpeg");
imageFiles.add("D:\\test\\image3.jpeg");
imageFiles.add("D:\\test\\image4.jpeg");
imageFiles.add("D:\\test\\image5.jpeg");
String outputPath = "D:\\test\\" + System.currentTimeMillis() + ".pdf"; // 输出PDF文件名
try {
// 创建文档对象,四个角都设置从0开始,不然会前张图片会稍微覆盖36px象数 36.0F
//是此默认方法导致this(pageSize, 36.0F, 36.0F, 36.0F, 36.0F);
Document document =new Document(PageSize.A4, 0, 0, 0, 0);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputPath));
// 添加目录
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12);
cb.beginText();
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "目录", 300, 780, 0);
cb.endText();
// 创建目录
PdfOutline root = cb.getRootOutline();
// 添加图片到PDF
for (int i = 0; i < imageFiles.size(); i++) {
String imagePath = imageFiles.get(i);
Image image = Image.getInstance(imagePath);
image.setAlignment(Image.MIDDLE);image.scaleToFit(PageSize.A4.getWidth(),PageSize.A4.getHeight());
// 等比例缩放图片以填满PDF页面
image.scaleAbsolute(document.getPageSize());
// 创建新页面并添加图片
//document.newPage();
document.add(image);
// 换行,增加间距
//document.add(new Paragraph("\n"));
Integer pageNum = i + 1;
//目录跳转页面内容设置。
PdfAction action = PdfAction.gotoLocalPage(pageNum, new PdfDestination(PdfDestination.FIT), writer);
//标题目录可以通过自己的目的读取txt内容设置,我这里没要求练习使用,
String title = String.valueOf(pageNum);
new PdfOutline(root, action, title, false);
}
// 关闭文档
document.close();
System.out.println("PDF生成完成");
} catch (Exception e) {
e.printStackTrace();
}
}
}

@ -0,0 +1,121 @@
package com.emr.util;
import com.emr.entity.emrPdfWaterSet.EmrPdfWaterSet;
import com.emr.vo.commomSearch.ScanPathVo;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.twelvemonkeys.imageio.metadata.tiff.IFD;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static com.emr.util.img2PdfUtil.addWaterMark;
/**
* @ClassName imgToPdfUtil
* @Description
* @Author linjj
* @Date 2024/4/8 9:13
* @Version 1.0
*/
public class imgToPdfUtil {
public static void imageToPdf(HttpServletResponse response, List<ScanPathVo> scanPathVos, String pdfName, EmrPdfWaterSet pdfWaterSet, EmrPdfWaterSet pdfWaterSet1) {
Document document = new Document(PageSize.A4, 0, 0, 0, 0); //创建文档容器
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfWriter writer;
//是否启用水印
Short effective = pdfWaterSet.getEffective();
Short isImg = pdfWaterSet.getIsImg();
try {
if (effective == 1 || isImg == 1) {
writer = PdfWriter.getInstance(document, bos);//创建编写器PDF类型
} else {
writer = PdfWriter.getInstance(document, response.getOutputStream());//创建编写器PDF类型
}
pdfName = java.net.URLEncoder.encode(pdfName, "UTF-8");
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentType("x-www-form-urlencoded"); // word格式
response.setHeader("Content-Disposition", "attachment; filename=" + pdfName + ".pdf");
imgToPdf(document, writer, scanPathVos);
if (effective == 1 || isImg == 1) {
addWaterMark(bos, response, pdfWaterSet.getUpOrUnder(), pdfWaterSet.getTransparent(), pdfWaterSet.getText(), pdfWaterSet.getTextX(), pdfWaterSet.getTextY(), pdfWaterSet.getTextColor(), pdfWaterSet.getTextSize(), pdfWaterSet.getTextRotation(), pdfWaterSet.getEffective(), pdfWaterSet.getIsImg(), pdfWaterSet.getImgFile(), pdfWaterSet.getImgWidth(), pdfWaterSet.getImgHeight(), pdfWaterSet.getImgX(), pdfWaterSet.getImgY(), pdfWaterSet1.getText());
}
} catch (Exception e) {
ExceptionPrintUtil.printException(e);
e.printStackTrace();
} finally {
try {
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
document.close();
}
}
//图片合成pdf增加目录
public static void imgToPdf(Document document, PdfWriter writer, List<ScanPathVo> scanPathVos) throws Exception {
//上一个目录名称
String lastOutline = null;
//是否增加标签
boolean outFlag = true;
//标签顺序
Integer outNum = 1;
// 添加目录
document.open(); //打开容器
PdfContentByte cb = writer.getDirectContent();
cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 12);
cb.beginText();
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "目录", 300, 780, 0);
cb.endText();
// 创建目录
PdfOutline root = cb.getRootOutline();
// 添加图片到PDF
for (int i = 0; i < scanPathVos.size(); i++) {
String assortName = scanPathVos.get(i).getAssortName();
if (StringUtils.isNotBlank(lastOutline) && lastOutline.equals(assortName)) {
outFlag = false;
}
if (StringUtils.isBlank(lastOutline)) {
lastOutline = assortName;
outFlag=true;
}
if (!lastOutline.equals(assortName)){
lastOutline = assortName;
outFlag=true;
}
String imagePath = scanPathVos.get(i).getFileRealPath();
Image image = Image.getInstance(imagePath);
image.setAlignment(Image.MIDDLE);
image.scaleToFit(PageSize.A4.getWidth(), PageSize.A4.getHeight());
// 等比例缩放图片以填满PDF页面
image.scaleAbsolute(document.getPageSize());
// 创建新页面并添加图片
//document.newPage();
document.add(image);
if (outFlag) {
//目录跳转页面内容设置。
PdfAction action = PdfAction.gotoLocalPage(i+1, new PdfDestination(PdfDestination.FIT), writer);
//标题目录
String title = outNum + "." + scanPathVos.get(i).getAssortName();
new PdfOutline(root, action, title, false);
outNum++;
}
}
document.close(); //关闭容器
}
}

@ -27,13 +27,24 @@ public class ScanPathVo {
private String scanPage;
private Date disDate;
private String disDate;
public Date getDisDate() {
private String assortName;
public String getAssortName() {
return assortName;
}
public void setAssortName(String assortName) {
this.assortName = assortName;
}
public String getDisDate() {
return disDate;
}
public void setDisDate(Date disDate) {
public void setDisDate(String disDate) {
this.disDate = disDate;
}

@ -1,12 +1,12 @@
#power\u6743\u9650\u7CFB\u7EDF\u7684\u670D\u52A1\u5668\u5730\u5740\u5934
POWER_IP =172.16.11.90
POWER_URLHEAD = http://172.16.11.90:8081/power
POWER_IP =localhost
POWER_URLHEAD = http://localhost:8081/power
POWER_JSPHEAD = 172.16.11.90
POWER_JSP = http://172.16.11.90:8081/power
POWER_JSPHEAD = localhost
POWER_JSP = http://localhost:8081/power
#\u672C\u8EAB\u7CFB\u7EDF\u7684\u670D\u52A1\u5668\u5730\u5740\u5934
EMR_RECORD_JSP = http://172.16.11.90:8081/emr_record
EMR_RECORD_JSP = http://localhost:8081/emr_record
#webSocket\u670D\u52A1\u5668\u5730\u5740
@ -43,6 +43,10 @@ applyApproveFlag =0
#//?????
initialization =0
export_pdf_hospital_info = \u6e58\u96c5\u533b\u9662
export_pdf_patient_info = inpatientNo,disDate
#\u67E5\u8BE2\u5168\u6587\u68C0\u7D22\u7684\u5730\u5740
fullTextSearchUrl = http://172.16.11.90:57777/AppEngine3?wsdl
@ -55,3 +59,6 @@ fullTextDays = 90
NEW_EMR_RECORD_JSP : http://localhost:8081/emr_record

@ -3,10 +3,9 @@ jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
#jdbc.username=sa
#jdbc.password=docus@702
jdbc.url=jdbc\:sqlserver\://localhost:1433;databaseName=emr_record
jdbc.url=jdbc\:sqlserver\://localhost:1433;databaseName=qf_record
jdbc.username=sa
jdbc.password=docus@702
jdbc.password=admin123
#dataSource2
jdbc.url2=jdbc\:sqlserver\://localhost:1433;databaseName=blgd_java
jdbc.username2=jsuser

@ -6,8 +6,9 @@
<result column="inpatient_no" property="inpatientNo" jdbcType="VARCHAR"/>
<result column="assort_id" property="assortId" jdbcType="VARCHAR"/>
<result column="scan_page" property="scanPage" jdbcType="VARCHAR"/>
<result column="dis_date" property="disDate" jdbcType="TIMESTAMP"/>
<result column="dis_date" property="disDate" jdbcType="VARCHAR"/>
<result column="fileRealPath" property="fileRealPath" jdbcType="VARCHAR"/>
<result column="assort_name" property="assortName" jdbcType="VARCHAR"/>
</resultMap>
<!--湛江、英德根据patientId集合查询图片路径-->
<select id="selectScanFileByPatientIds" resultMap="BaseResultMap" parameterType="java.lang.String">
@ -78,6 +79,7 @@
commomtable.dis_date,
dbo.t_scan_assort.assort_id,
dbo.t_scan_assort.scan_page,
dbo.zd_assort.assort_name,
case
when t_scan_assort.source = 1
then convert(nvarchar(255),dbo.commomtable.${flag})+'\'+dbo.t_scan_assort.scan_page

Loading…
Cancel
Save