更新代码:

解决临时生成pdf大小不一问题
————2021/03/17 wzq
master
wzqgit 4 years ago
parent 76cc294a3e
commit d97ba4e3b2

@ -54,6 +54,9 @@ public class inHospitalController {
@Autowired @Autowired
private Emr_PictureService emrPictureService; private Emr_PictureService emrPictureService;
@Autowired
private Zd_AssortService zd_assortService;
@Resource @Resource
private LogService logService; private LogService logService;
@ -209,6 +212,56 @@ public class inHospitalController {
return result; 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<Archive_Detail> arList = archiveDetailService.selectByCol(archiveDetail);
List<Archive_Detail> 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;
}
/** /**
* idPDFpdf * idPDFpdf
@ -229,85 +282,123 @@ public class inHospitalController {
// 项目在容器中实际发布运行的根路径 // 项目在容器中实际发布运行的根路径
String realPath = request.getSession().getServletContext().getRealPath("/"); String realPath = request.getSession().getServletContext().getRealPath("/");
//realPath = realPath.split("target")[0] //realPath = realPath.split("target")[0]
realPath = pictureUrl + "\\static\\img\\"; realPath = pictureUrl + "\\static\\img\\";
//pdf名称 //pdf名称
String pdfFileName = String.valueOf(System.currentTimeMillis()); String pdfFileName = String.valueOf(System.currentTimeMillis());
//病案号文件夹 //病案号文件夹
if (masterId != "" && masterId != null) { if (masterId == "" && masterId == null) {
//判断文件夹是否存在 result = "";
File fileDic = new File(realPath + masterId + "\\"); return result;
if (!fileDic.exists() && !fileDic.isDirectory()) { }
fileDic.mkdirs();
}
//判断文件夹是否存在
File fileDic = new File(realPath + masterId + "\\");
if (!fileDic.exists() && !fileDic.isDirectory()) {
fileDic.mkdirs();
}
//病案号/类别/id文件夹(图片路径下的子文件夹名称) List<Zd_Assort> zds = zd_assortService.selectByColm(new Zd_Assort()); //from db
//生成pdf(图片路径pdf生成路径图片路径文件夹列表)
List imgList = new ArrayList(); //from args
if (imgStr != "" && imgStr != null) { //病案号/类别/id文件夹(图片路径下的子文件夹名称)
String[] imgArr = imgStr.split(","); //生成pdf(图片路径pdf生成路径图片路径文件夹列表)
if (imgArr.length > 0) { List imgList = new ArrayList();
for (int i = 0; i < imgArr.length; i++) { if (imgStr != "" && imgStr != null) {
imgList.add(imgArr[i]); 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<String> 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){ // if(imgList.size()==1){
// result= masterId.trim() + "/"+ imgList.get(0) +"/"+ imgList.get(0)+".pdf"; // result= masterId.trim() + "/"+ imgList.get(0) +"/"+ imgList.get(0)+".pdf";
// }else{ // }else{
String pdfPath = autoPdfUrl.trim() + "/" + pdfFileName + ".pdf"; // String pdfPath = autoPdfUrl.trim() + "/" + pdfFileName + ".pdf";
String imgPath = realPath + "pdfFiles\\" + masterId.trim() + "\\"; // String imgPath = realPath + "pdfFiles\\" + masterId.trim() + "\\";
//获取废除的pdf文件名列表archive_detail // //获取废除的pdf文件名列表archive_detail
List pdfList = new ArrayList(); // List pdfList = new ArrayList();
Archive_Detail archiveDetail = new Archive_Detail(); // Archive_Detail archiveDetail = new Archive_Detail();
archiveDetail.setFlag("0"); // archiveDetail.setFlag("0");
archiveDetail.setMasterid(masterId.trim()); // archiveDetail.setMasterid(masterId.trim());
archiveDetail.setTitle(imgStr); // archiveDetail.setTitle(imgStr);
List<Archive_Detail> arList = archiveDetailService.selectByCol(archiveDetail); //
//损坏PDF文件列表 // List<Archive_Detail> arList = archiveDetailService.selectByCol(archiveDetail);
List<Archive_Detail> errorPdfList = new ArrayList<Archive_Detail>(); // //损坏PDF文件列表
int size = 0; // List<Archive_Detail> errorPdfList = new ArrayList<Archive_Detail>();
if (arList != null) { // int size = 0;
for (int m = 0; m < arList.size(); m++) { // if (arList == null || arList.size()==0) {
String str = arList.get(m).getPdfPath(); // }
if (str != "" && str != null) { // else {
if (str.indexOf("..\\static\\img\\pdfFiles\\") != -1) { // for (int m = 0; m < arList.size(); m++) {
str = str.split("pdfFiles")[1]; // String str = arList.get(m).getPdfPath();
str = realPath + "pdfFiles" + str; // if (str != "" && str != null) {
} // if (str.indexOf("..\\static\\img\\pdfFiles\\") != -1) {
if (!PrintToPdfUtil.checkPdfFile(str)) { // str = str.split("pdfFiles")[1];
arList.get(m).setPdfPath(str); // str = realPath + "pdfFiles" + str;
errorPdfList.add(arList.get(m)); // }
} else { // if (!PrintToPdfUtil.checkPdfFile(str)) {
pdfList.add(str); // arList.get(m).setPdfPath(str);
} // errorPdfList.add(arList.get(m));
} // } else {
// str= str.substring(str.lastIndexOf("\\") + 1); // 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";
// } // }
} else { // }
result = ""; //
} // if (pdfList.size() > 0) {
return result; // //对所有地址的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;
} }
/** /**

@ -8,11 +8,9 @@ package com.emr.util;
import com.lowagie.text.Document; import com.lowagie.text.Document;
import com.lowagie.text.Image; import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Rectangle; import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfCopy; import com.lowagie.text.pdf.*;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.io.MemoryUsageSetting; import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility; import org.apache.pdfbox.multipdf.PDFMergerUtility;
@ -196,6 +194,7 @@ public class PrintToPdfUtil {
// FileOutputStream fos = new FileOutputStream(pdfPath); // FileOutputStream fos = new FileOutputStream(pdfPath);
// // 创建文档 // // 创建文档
Document doc = new Document(null, 0, 0, 0, 0); Document doc = new Document(null, 0, 0, 0, 0);
// //doc.open(); // //doc.open();
// // 写入PDF文档 // // 写入PDF文档
// PdfWriter.getInstance(doc, fos); // PdfWriter.getInstance(doc, fos);
@ -334,10 +333,31 @@ public class PrintToPdfUtil {
// // 定义一页大小A4 // // 定义一页大小A4
// PDPage page = new PDPage(PDRectangle.A4); // PDPage page = new PDPage(PDRectangle.A4);
// document2.addPage(page); // 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(); document.open();
for (int i = 0; i < files.size(); i++) { for (int i = 0; i < files.size(); i++) {
boolean bol = checkPdfFile(files.get(i).toString()); boolean bol = checkPdfFile(files.get(i).toString());
@ -353,7 +373,7 @@ public class PrintToPdfUtil {
}else{ }else{
} }
} }*/
retValue = true; retValue = true;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();

Loading…
Cancel
Save