diff --git a/src/main/java/com/emr/controller/inHospitalController.java b/src/main/java/com/emr/controller/inHospitalController.java index a8568bf..52b0214 100644 --- a/src/main/java/com/emr/controller/inHospitalController.java +++ b/src/main/java/com/emr/controller/inHospitalController.java @@ -54,6 +54,9 @@ public class inHospitalController { @Autowired private Emr_PictureService emrPictureService; + @Autowired + private Zd_AssortService zd_assortService; + @Resource private LogService logService; @@ -209,6 +212,56 @@ public class inHospitalController { return result; } + //2021-03-15更新优化 + private String GeneratePDFFileByAssortID(String masterId, String assortID, String realPath ) + { + List pdfList = new ArrayList(); + Archive_Detail archiveDetail = new Archive_Detail(); + archiveDetail.setFlag("0"); + archiveDetail.setMasterid(masterId.trim()); + archiveDetail.setTitle(assortID); + + List arList = archiveDetailService.selectByCol(archiveDetail); + + List errorPdfList = new ArrayList<>(); + int size = 0; + if (arList == null || arList.size()==0) { + return ""; + } + for (int m = 0; m < arList.size(); m++) { + String str = arList.get(m).getPdfPath(); + if (str != "" && str != null) { + if (str.indexOf("..\\static\\img\\pdfFiles\\") != -1) { + str = str.split("pdfFiles")[1]; + str = realPath + "pdfFiles" + str; + } + if (!PrintToPdfUtil.checkPdfFile(str)) { + arList.get(m).setPdfPath(str); + errorPdfList.add(arList.get(m)); + } else { + pdfList.add(str); + } + } + } + + String pdfFileName = String.valueOf(System.currentTimeMillis()); + String pdfPath = autoPdfUrl.trim() + "/" + pdfFileName + ".pdf"; + + if (pdfList.size() > 0) { + //对所有地址的pdf进行合成 + //if (errorPdfList.size() != size) { + PrintToPdfUtil.mergePdfFiles(pdfList, pdfPath); + //} else { + // result = ""; + // } + if (errorPdfList.size() > 0) { + ExportTxtPaper.exportTxtPaper(pdfErrorUrl, "不存在或损坏PDF文件列表" + pdfFileName + ".txt", errorPdfList); + } + } + return pdfPath; + } + + /** * 根据病案号或类别或图片id获取图片列表,并生成PDF并返回pdf文件名 @@ -229,85 +282,123 @@ public class inHospitalController { // 项目在容器中实际发布运行的根路径 String realPath = request.getSession().getServletContext().getRealPath("/"); //realPath = realPath.split("target")[0] - realPath = pictureUrl + "\\static\\img\\"; + realPath = pictureUrl + "\\static\\img\\"; //pdf名称 String pdfFileName = String.valueOf(System.currentTimeMillis()); //病案号文件夹 - if (masterId != "" && masterId != null) { - //判断文件夹是否存在 - File fileDic = new File(realPath + masterId + "\\"); - if (!fileDic.exists() && !fileDic.isDirectory()) { - fileDic.mkdirs(); - } + if (masterId == "" && masterId == null) { + result = ""; + return result; + } + //判断文件夹是否存在 + File fileDic = new File(realPath + masterId + "\\"); + if (!fileDic.exists() && !fileDic.isDirectory()) { + fileDic.mkdirs(); + } - //病案号/类别/id文件夹(图片路径下的子文件夹名称) - //生成pdf(图片路径,pdf生成路径,图片路径文件夹列表) - List imgList = new ArrayList(); - if (imgStr != "" && imgStr != null) { - String[] imgArr = imgStr.split(","); - if (imgArr.length > 0) { - for (int i = 0; i < imgArr.length; i++) { - imgList.add(imgArr[i]); - } + List zds = zd_assortService.selectByColm(new Zd_Assort()); //from db + + //from args + //病案号/类别/id文件夹(图片路径下的子文件夹名称) + //生成pdf(图片路径,pdf生成路径,图片路径文件夹列表) + List imgList = new ArrayList(); + if (imgStr != "" && imgStr != null) { + String[] imgArr = imgStr.split(","); + if (imgArr.length > 0) { + for (int i = 0; i < imgArr.length; i++) { + imgList.add(imgArr[i]); } } + } + + for (int i = zds.size()-1; i >=0 ; i--) { + String a = zds.get(i).getAssortId(); + if(imgList.indexOf(a)==-1) + { + zds.remove(i); + } + + } + + ArrayList fileList = new ArrayList<>(); + + for (int i = 0; i < zds.size(); i++) { + String asordID = zds.get(i).getAssortId(); + String pdfA = GeneratePDFFileByAssortID(masterId, asordID, realPath); + if(pdfA=="" || pdfA.length()==0) + { + continue; + } + fileList.add(pdfA); + } + if(fileList.size()==0) + { + return ""; + } + + String pdfPath = autoPdfUrl.trim() + "/" + String.valueOf(System.currentTimeMillis()) + ".pdf"; + + PrintToPdfUtil.mergePdfFiles(fileList, pdfPath); + return pdfPath; // if(imgList.size()==1){ // result= masterId.trim() + "/"+ imgList.get(0) +"/"+ imgList.get(0)+".pdf"; // }else{ - String pdfPath = autoPdfUrl.trim() + "/" + pdfFileName + ".pdf"; - String imgPath = realPath + "pdfFiles\\" + masterId.trim() + "\\"; - //获取废除的pdf文件名列表archive_detail - List pdfList = new ArrayList(); - Archive_Detail archiveDetail = new Archive_Detail(); - archiveDetail.setFlag("0"); - archiveDetail.setMasterid(masterId.trim()); - archiveDetail.setTitle(imgStr); - List arList = archiveDetailService.selectByCol(archiveDetail); - //损坏PDF文件列表 - List errorPdfList = new ArrayList(); - int size = 0; - if (arList != null) { - for (int m = 0; m < arList.size(); m++) { - String str = arList.get(m).getPdfPath(); - if (str != "" && str != null) { - if (str.indexOf("..\\static\\img\\pdfFiles\\") != -1) { - str = str.split("pdfFiles")[1]; - str = realPath + "pdfFiles" + str; - } - if (!PrintToPdfUtil.checkPdfFile(str)) { - arList.get(m).setPdfPath(str); - errorPdfList.add(arList.get(m)); - } else { - pdfList.add(str); - } - } - // str= str.substring(str.lastIndexOf("\\") + 1); - - } - } - if (pdfList.size() > 0) { - //对所有地址的pdf进行合成 - //if (errorPdfList.size() != size) { - PrintToPdfUtil.mergePdfFiles(pdfList, pdfPath); - result = pdfPath; - //} else { - // result = ""; - // } - if (errorPdfList.size() > 0) { - ExportTxtPaper.exportTxtPaper(pdfErrorUrl, "不存在或损坏PDF文件列表" + pdfFileName + ".txt", errorPdfList); - } - } - //PrintToPdfUtil.pdtToPdf(imgPath,pdfPath, imgList,pdfList); - //PDFUtils.getPdf(pdfPath, pdfList); - //result = "typesPdf/" + pdfFileName + ".pdf"; +// String pdfPath = autoPdfUrl.trim() + "/" + pdfFileName + ".pdf"; +// String imgPath = realPath + "pdfFiles\\" + masterId.trim() + "\\"; +// //获取废除的pdf文件名列表archive_detail +// List pdfList = new ArrayList(); +// Archive_Detail archiveDetail = new Archive_Detail(); +// archiveDetail.setFlag("0"); +// archiveDetail.setMasterid(masterId.trim()); +// archiveDetail.setTitle(imgStr); +// +// List arList = archiveDetailService.selectByCol(archiveDetail); +// //损坏PDF文件列表 +// List errorPdfList = new ArrayList(); +// int size = 0; +// if (arList == null || arList.size()==0) { +// } +// else { +// for (int m = 0; m < arList.size(); m++) { +// String str = arList.get(m).getPdfPath(); +// if (str != "" && str != null) { +// if (str.indexOf("..\\static\\img\\pdfFiles\\") != -1) { +// str = str.split("pdfFiles")[1]; +// str = realPath + "pdfFiles" + str; +// } +// if (!PrintToPdfUtil.checkPdfFile(str)) { +// arList.get(m).setPdfPath(str); +// errorPdfList.add(arList.get(m)); +// } else { +// pdfList.add(str); +// } +// } +// // str= str.substring(str.lastIndexOf("\\") + 1); +// // } - } else { - result = ""; - } - return result; +// } +// +// if (pdfList.size() > 0) { +// //对所有地址的pdf进行合成 +// //if (errorPdfList.size() != size) { +// PrintToPdfUtil.mergePdfFiles(pdfList, pdfPath); +// result = pdfPath; +// //} else { +// // result = ""; +// // } +// if (errorPdfList.size() > 0) { +// ExportTxtPaper.exportTxtPaper(pdfErrorUrl, "不存在或损坏PDF文件列表" + pdfFileName + ".txt", errorPdfList); +// } +// } + //PrintToPdfUtil.pdtToPdf(imgPath,pdfPath, imgList,pdfList); + //PDFUtils.getPdf(pdfPath, pdfList); + //result = "typesPdf/" + pdfFileName + ".pdf"; +// } + +// return result; } /** diff --git a/src/main/java/com/emr/util/PrintToPdfUtil.java b/src/main/java/com/emr/util/PrintToPdfUtil.java index 37de3fa..1e44a62 100644 --- a/src/main/java/com/emr/util/PrintToPdfUtil.java +++ b/src/main/java/com/emr/util/PrintToPdfUtil.java @@ -8,11 +8,9 @@ package com.emr.util; import com.lowagie.text.Document; import com.lowagie.text.Image; +import com.lowagie.text.PageSize; import com.lowagie.text.Rectangle; -import com.lowagie.text.pdf.PdfCopy; -import com.lowagie.text.pdf.PdfImportedPage; -import com.lowagie.text.pdf.PdfReader; -import com.lowagie.text.pdf.PdfWriter; +import com.lowagie.text.pdf.*; import org.apache.commons.io.FileUtils; import org.apache.pdfbox.io.MemoryUsageSetting; import org.apache.pdfbox.multipdf.PDFMergerUtility; @@ -196,6 +194,7 @@ public class PrintToPdfUtil { // FileOutputStream fos = new FileOutputStream(pdfPath); // // 创建文档 Document doc = new Document(null, 0, 0, 0, 0); + // //doc.open(); // // 写入PDF文档 // PdfWriter.getInstance(doc, fos); @@ -334,10 +333,31 @@ public class PrintToPdfUtil { // // 定义一页,大小A4 // PDPage page = new PDPage(PDRectangle.A4); // document2.addPage(page); +// document = new Document(new PdfReader(files.get(0).toString()).getPageSize(1)); + document = new Document(PageSize.A4); + PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream(newfile)); + document.open(); + PdfContentByte cb = pdfWriter.getDirectContent(); + for (int i = 0; i < files.size(); i++) { + String file = files.get(i).toString(); + PdfReader pdfReader = new PdfReader(file); + + // 循环PDF的每一页 pdfWriter.getImportedPage(pdfReader, index) idnex从1开始 + for (int j = 1; j <= pdfReader.getNumberOfPages(); j++) { + // 新增一页,不然多页的PDF会重合到一页 + document.newPage(); + PdfImportedPage page = pdfWriter.getImportedPage(pdfReader, j); + // 计算缩小比例 以最大缩小比例为主 + // pdfReader.getPageSize(index) index是从1开始不是0 + float w = PageSize.A4.getWidth() / pdfReader.getPageSize(1).getWidth(); + float h = PageSize.A4.getHeight() / pdfReader.getPageSize(1).getHeight(); + float Scale = w > h ? h : w; + cb.addTemplate(page, Scale, 0, 0, Scale, 0, 0); + } + } + document.close(); - document = new Document(new PdfReader(files.get(0).toString()).getPageSize(1)); - - PdfCopy copy = new PdfCopy(document, new FileOutputStream(newfile)); + /*PdfCopy copy = new PdfCopy(document, new FileOutputStream(newfile)); document.open(); for (int i = 0; i < files.size(); i++) { boolean bol = checkPdfFile(files.get(i).toString()); @@ -353,7 +373,7 @@ public class PrintToPdfUtil { }else{ } - } + }*/ retValue = true; } catch (Exception e) { e.printStackTrace();