From 9ee29819dd3efb296ebe3d63afbe6a8575d1a22f Mon Sep 17 00:00:00 2001 From: "DESKTOP-6ARNG1Q\\dlsgk" Date: Thu, 14 Nov 2024 11:41:39 +0900 Subject: [PATCH] =?UTF-8?q?Pdf=20Utill=20section=EC=9D=B4=20=EC=97=86?= =?UTF-8?q?=EB=8A=94=20=EA=B2=BD=EC=9A=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../qcast/biz/estimate/EstimateService.java | 5 +- .../com/interplug/qcast/util/PdfUtil.java | 110 ++++++++++-------- 2 files changed, 64 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/interplug/qcast/biz/estimate/EstimateService.java b/src/main/java/com/interplug/qcast/biz/estimate/EstimateService.java index 270ac867..f5ac681f 100644 --- a/src/main/java/com/interplug/qcast/biz/estimate/EstimateService.java +++ b/src/main/java/com/interplug/qcast/biz/estimate/EstimateService.java @@ -793,17 +793,18 @@ public class EstimateService { estimateResponse.setPwrGnrSim(pwrGnrSimResponse); - if (true) { + if (true) { // PDF 다운로드 String[] arrSection = new String[3]; // TODO Section 갯수 넣기 int iSection = 0; - // PDF 다운로드 String pdfFileName = "Quation_Detail_" + new SimpleDateFormat("yyyyMMdd").format(new Date()); String templateFilePath = "pdf_download_quotation_detail_template.html"; // 템플릿 html 조회 Document doc = PdfUtil.getPdfDoc(request, templateFilePath); + + // 삭제하려는 element Element elm; // 발전시뮬레이션 pdf Html 생성 diff --git a/src/main/java/com/interplug/qcast/util/PdfUtil.java b/src/main/java/com/interplug/qcast/util/PdfUtil.java index cc6697f8..ae25eb62 100644 --- a/src/main/java/com/interplug/qcast/util/PdfUtil.java +++ b/src/main/java/com/interplug/qcast/util/PdfUtil.java @@ -78,68 +78,57 @@ public class PdfUtil { PdfWriter writer = new PdfWriter(os); PdfDocument pdfDoc = new PdfDocument(writer)) { - // ConverterProperties 생성 - ConverterProperties properties = new ConverterProperties(); - // 폰트 설정 - DefaultFontProvider fontProvider = new DefaultFontProvider(false, false, false); + ConverterProperties properties = new ConverterProperties(); String fontPath = "template/pdf/BIZUDPGothic-Regular.ttf"; - InputStream fontStream = PdfUtil.class.getClassLoader().getResourceAsStream(fontPath); + properties.setFontProvider(setFontProvider(fontPath)); - if (fontStream != null) { - File tempFontFile = Files.createTempFile("BIZUDPGothic-Regular", ".ttf").toFile(); - Files.copy( - fontStream, tempFontFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); - fontProvider.addFont(tempFontFile.getAbsolutePath()); - fontStream.close(); - tempFontFile.deleteOnExit(); + // arrSection이 비어 있는 경우 HTML 전체를 변환 + if (arrSection == null || arrSection.length == 0) { + // HTML 전체를 pdfDoc에 직접 변환 + HtmlConverter.convertToPdf( + new ByteArrayInputStream(doc.html().getBytes(StandardCharsets.UTF_8)), os, properties); + os.flush(); } else { - log.error("폰트 파일을 찾을 수 없습니다: " + fontPath); - } - properties.setFontProvider(fontProvider); + // arrSection이 있는 경우 각 섹션을 개별적으로 변환 후 병합 + String headHtml = doc.select("head").outerHtml(); - // 원본 HTML에서 내용을 추출 - String headHtml = doc.select("head").outerHtml(); // 의 전체 내용을 가져옵니다. + PdfMerger merger = new PdfMerger(pdfDoc); + for (String section : arrSection) { + if (section != null && !"".equals(section)) { + Elements eSections = doc.select(section); + if (eSections != null && eSections.size() > 0) { + for (Element eSection : eSections) { - PdfMerger merger = new PdfMerger(pdfDoc); // 최종 문서에 병합할 PdfMerger 생성 - // arrSection의 각 섹션을 PDF에 추가 - for (String section : arrSection) { - if (section != null && !"".equals(section)) { - Elements eSections = doc.select(section); // 섹션 선택 - if (eSections != null && eSections.size() > 0) { - for (Element eSection : eSections) { + String sectionHtml = + "" + headHtml + "" + eSection.outerHtml() + ""; - String sectionHtml = - "" + headHtml + "" + eSection.outerHtml() + ""; + try (ByteArrayOutputStream tempOutputStream = new ByteArrayOutputStream(); + PdfWriter tempWriter = new PdfWriter(tempOutputStream); + PdfDocument tempPdfDoc = new PdfDocument(tempWriter)) { - // 각 섹션을 임시 PDF 파일에 변환 - try (ByteArrayOutputStream tempOutputStream = new ByteArrayOutputStream(); - PdfWriter tempWriter = new PdfWriter(tempOutputStream); - PdfDocument tempPdfDoc = new PdfDocument(tempWriter)) { + HtmlConverter.convertToPdf( + new ByteArrayInputStream(sectionHtml.getBytes(StandardCharsets.UTF_8)), + tempPdfDoc, + properties); - HtmlConverter.convertToPdf( - new ByteArrayInputStream(sectionHtml.getBytes(StandardCharsets.UTF_8)), - tempPdfDoc, - properties); + PdfDocument tempDoc = + new PdfDocument( + new PdfReader(new ByteArrayInputStream(tempOutputStream.toByteArray()))); + merger.merge(tempDoc, 1, tempDoc.getNumberOfPages()); + tempDoc.close(); - // tempPdfDoc을 메인 pdfDoc에 병합 - PdfDocument tempDoc = - new PdfDocument( - new PdfReader(new ByteArrayInputStream(tempOutputStream.toByteArray()))); - merger.merge(tempDoc, 1, tempDoc.getNumberOfPages()); - tempDoc.close(); - - // 마지막 섹션이 아닐 경우 새 페이지 추가 - if (!eSection.equals(eSections.last())) { - pdfDoc.addNewPage(); + if (!eSection.equals(eSections.last())) { + pdfDoc.addNewPage(); + } } } - } // for - } // if - } // if + } + } + } + pdfDoc.close(); + os.flush(); } - pdfDoc.close(); // 전체 PDF 문서 작성이 끝난 후에 한번만 닫기 - os.flush(); } catch (Exception e) { log.error("PDF 생성 중 오류 발생: " + e.getMessage(), e); if (!response.isCommitted()) { @@ -148,4 +137,27 @@ public class PdfUtil { } } } + + /** + * 폰트 설정 메서드 + * + * @param fontPath 폰트 파일 경로 + * @return 설정된 FontProvider + */ + private static DefaultFontProvider setFontProvider(String fontPath) throws IOException { + DefaultFontProvider fontProvider = new DefaultFontProvider(false, false, false); + InputStream fontStream = PdfUtil.class.getClassLoader().getResourceAsStream(fontPath); + + if (fontStream != null) { + File tempFontFile = Files.createTempFile("BIZUDPGothic-Regular", ".ttf").toFile(); + Files.copy( + fontStream, tempFontFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING); + fontProvider.addFont(tempFontFile.getAbsolutePath()); + fontStream.close(); + tempFontFile.deleteOnExit(); + } else { + log.error("폰트 파일을 찾을 수 없습니다: " + fontPath); + } + return fontProvider; + } }