From 3b539ddd2bc228a856bc6c808680bfc72772b3ea Mon Sep 17 00:00:00 2001 From: linjj <850658129@qq.com> Date: Tue, 7 May 2024 14:32:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9B=BD=E6=A0=87=E7=97=85?= =?UTF-8?q?=E5=8E=86=E5=AF=BC=E5=87=BA=E5=9B=BE=E7=89=87=E6=AF=94=E4=BE=8B?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../batchExport/BatchExportServiceImpl.java | 143 ++++++++++-------- 1 file changed, 78 insertions(+), 65 deletions(-) diff --git a/src/main/java/com/emr/service/batchExport/BatchExportServiceImpl.java b/src/main/java/com/emr/service/batchExport/BatchExportServiceImpl.java index 1d1cda0..9afdf76 100644 --- a/src/main/java/com/emr/service/batchExport/BatchExportServiceImpl.java +++ b/src/main/java/com/emr/service/batchExport/BatchExportServiceImpl.java @@ -50,9 +50,9 @@ public class BatchExportServiceImpl implements BatchExportService { @Override public String batchExportPdf(int taskId) throws Exception { //是否存在目录不存在创建 - File file = new File (exportPdfPath); - if(!file.isDirectory ()){ - file.mkdirs (); + File file = new File(exportPdfPath); + if (!file.isDirectory()) { + file.mkdirs(); } //导出文件名 String fileName = null; @@ -74,7 +74,7 @@ public class BatchExportServiceImpl implements BatchExportService { } catch (ParseException e) { throw new RuntimeException(e); } - Boolean aBoolean = imageToPdf(filePathList, fileName,exportPdfPath); + Boolean aBoolean = imageToPdf(filePathList, fileName, exportPdfPath); //为true时修改任务状态1完成2失败 if (aBoolean) { exportTaskDetailsMapper.upStatc(1, list.getId()); @@ -88,83 +88,96 @@ public class BatchExportServiceImpl implements BatchExportService { } - - public static Boolean imageToPdf(List scanPathVos, String fileName,String exportPdfPath) { - Document document = new Document(PageSize.A4, 0, 0, 0, 0); //创建文档容器 - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - PdfWriter writer; + public static Boolean imageToPdf(List scanPathVos, String fileName, String exportPdfPath) { + ByteArrayOutputStream bos = null; + FileOutputStream fileOutputStream = null; + PdfWriter writer = null; try { - FileOutputStream fileOutputStream = new FileOutputStream(exportPdfPath+fileName+".pdf"); - writer = PdfWriter.getInstance(document,fileOutputStream);//创建编写器(PDF类型) - imgToPdf(document, writer, scanPathVos); + bos = new ByteArrayOutputStream(); + Document document = new Document(PageSize.A4, 0, 0, 0, 0); //创建文档容器 + fileOutputStream = new FileOutputStream(exportPdfPath + fileName + ".pdf"); + writer = PdfWriter.getInstance(document, fileOutputStream);//创建编写器(PDF类型) + //上一个目录名称 + String lastOutline = null; + //是否增加标签 + boolean outFlag = true; + //标签顺序 + Integer outNum = 1; + // 添加目录 + document.open(); //打开容器 + float w, h; + 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).getFilePath(); + Image image = Image.getInstance(imagePath); + /*处理图片缩放比例*/ + w = image.getWidth(); + h = image.getHeight(); + image.setAlignment(com.lowagie.text.Image.ALIGN_CENTER); + image.scaleAbsolute(PageSize.A4.getWidth(), PageSize.A4.getWidth() * (h / w)); + // 创建新页面并添加图片 + if (i != 0) { + 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(); //关闭容器 } catch (Exception e) { ExceptionPrintUtil.printException(e); e.printStackTrace(); return false; } finally { try { - bos.flush(); - bos.close(); + if (writer != null) { + writer.close(); + } + + if (fileOutputStream != null) { + fileOutputStream.close(); + } + + if (bos != null) { + bos.flush(); + bos.close(); + } + } catch (IOException e) { e.printStackTrace(); } - document.close(); } return true; } - public static void imgToPdf(Document document, PdfWriter writer, List 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).getFilePath(); - 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(); //关闭容器 + }