更新代码:

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

@ -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<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
@ -234,14 +287,20 @@ public class inHospitalController {
String pdfFileName = String.valueOf(System.currentTimeMillis());
//病案号文件夹
if (masterId != "" && masterId != null) {
if (masterId == "" && masterId == null) {
result = "";
return result;
}
//判断文件夹是否存在
File fileDic = new File(realPath + masterId + "\\");
if (!fileDic.exists() && !fileDic.isDirectory()) {
fileDic.mkdirs();
}
List<Zd_Assort> zds = zd_assortService.selectByColm(new Zd_Assort()); //from db
//from args
//病案号/类别/id文件夹(图片路径下的子文件夹名称)
//生成pdf(图片路径pdf生成路径图片路径文件夹列表)
List imgList = new ArrayList();
@ -254,60 +313,92 @@ public class inHospitalController {
}
}
// 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<Archive_Detail> arList = archiveDetailService.selectByCol(archiveDetail);
//损坏PDF文件列表
List<Archive_Detail> errorPdfList = new ArrayList<Archive_Detail>();
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);
for (int i = zds.size()-1; i >=0 ; i--) {
String a = zds.get(i).getAssortId();
if(imgList.indexOf(a)==-1)
{
zds.remove(i);
}
}
// str= str.substring(str.lastIndexOf("\\") + 1);
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 (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);
}
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<Archive_Detail> arList = archiveDetailService.selectByCol(archiveDetail);
// //损坏PDF文件列表
// List<Archive_Detail> errorPdfList = new ArrayList<Archive_Detail>();
// 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);
//
// }
// }
//
// 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 = "";
}
return result;
// return result;
}
/**

@ -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);
document = new Document(new PdfReader(files.get(0).toString()).getPageSize(1));
// 循环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();
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();

Loading…
Cancel
Save