From 300b6e2efde684b8e7180cc3b033c236e400ed53 Mon Sep 17 00:00:00 2001 From: leebeomjun Date: Thu, 25 Apr 2024 13:36:04 +0900 Subject: [PATCH] =?UTF-8?q?pdf=EC=B6=9C=EB=A0=A5=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xit/fims/cmmn/pdf/print/PDFWriter.java | 33 +++++++++++++++---- .../fims/cmmn/pdf/print/format/Advntce.java | 4 +-- .../xit/fims/cmmn/pdf/print/format/Nht.java | 4 +-- .../cmmn/pdf/print/format/PDFPrintFormat.java | 22 +++++-------- .../xit/fims/sndb/web/Sndb01Controller.java | 6 ++-- .../xit/fims/sprt/web/Sprt01Controller.java | 12 +++---- 6 files changed, 45 insertions(+), 36 deletions(-) diff --git a/src/main/java/cokr/xit/fims/cmmn/pdf/print/PDFWriter.java b/src/main/java/cokr/xit/fims/cmmn/pdf/print/PDFWriter.java index 14c94097..11e7270e 100644 --- a/src/main/java/cokr/xit/fims/cmmn/pdf/print/PDFWriter.java +++ b/src/main/java/cokr/xit/fims/cmmn/pdf/print/PDFWriter.java @@ -1,7 +1,10 @@ package cokr.xit.fims.cmmn.pdf.print; +import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; @@ -13,6 +16,8 @@ import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode; import org.springframework.core.io.ClassPathResource; +import cokr.xit.foundation.Downloadable; + public class PDFWriter extends PDF { public PDFWriter(){ @@ -105,15 +110,29 @@ public class PDFWriter extends PDF { return this; } - public PDFWriter saveDocument(String path) throws IOException{ - doc.save(path); - return this; - } - public PDFWriter closeDocument() throws IOException{ - doc.close(); - return this; + public void write(OutputStream out) { + try { + doc.save(out); + doc.close(); + } catch (Exception e) { + throw runtimeException(e); + } } + public File write(String filepath) { + File file = new File(filepath); + try (FileOutputStream out = new FileOutputStream(file)) { + write(out); + return file; + } catch (Exception e) { + throw runtimeException(e); + } + } + public Downloadable getDownloadable() { + return new Downloadable() + .setContentType(MIME_TYPE) + .setWriter(this::write); + } } diff --git a/src/main/java/cokr/xit/fims/cmmn/pdf/print/format/Advntce.java b/src/main/java/cokr/xit/fims/cmmn/pdf/print/format/Advntce.java index ba3d530a..39c1e9b4 100644 --- a/src/main/java/cokr/xit/fims/cmmn/pdf/print/format/Advntce.java +++ b/src/main/java/cokr/xit/fims/cmmn/pdf/print/format/Advntce.java @@ -85,8 +85,8 @@ public class Advntce extends PDFPrintFormat { super(); } public Advntce(Print print, PrintOption printOption, - DataObject otptBscStng, List otptArtclStngList, String paperSeCd){ - super(print,printOption,otptBscStng,otptArtclStngList,paperSeCd); + DataObject otptBscStng, List otptArtclStngList, String paperSeCd, List data){ + super(print,printOption,otptBscStng,otptArtclStngList,paperSeCd, data); this.print.setFormatKorName("사전통지서"); this.sndngSeCd = "02"; } diff --git a/src/main/java/cokr/xit/fims/cmmn/pdf/print/format/Nht.java b/src/main/java/cokr/xit/fims/cmmn/pdf/print/format/Nht.java index cf9d5149..d2b2bc64 100644 --- a/src/main/java/cokr/xit/fims/cmmn/pdf/print/format/Nht.java +++ b/src/main/java/cokr/xit/fims/cmmn/pdf/print/format/Nht.java @@ -95,8 +95,8 @@ public class Nht extends PDFPrintFormat { } public Nht(Print print, PrintOption printOption, - DataObject otptBscStng, List otptArtclStngList, String paperSeCd) { - super(print,printOption,otptBscStng,otptArtclStngList,paperSeCd); + DataObject otptBscStng, List otptArtclStngList, String paperSeCd, List data) { + super(print,printOption,otptBscStng,otptArtclStngList,paperSeCd, data); this.print.setFormatKorName("고지서"); this.sndngSeCd = "03"; } diff --git a/src/main/java/cokr/xit/fims/cmmn/pdf/print/format/PDFPrintFormat.java b/src/main/java/cokr/xit/fims/cmmn/pdf/print/format/PDFPrintFormat.java index cdccacbd..8d3c39c1 100644 --- a/src/main/java/cokr/xit/fims/cmmn/pdf/print/format/PDFPrintFormat.java +++ b/src/main/java/cokr/xit/fims/cmmn/pdf/print/format/PDFPrintFormat.java @@ -36,6 +36,8 @@ public abstract class PDFPrintFormat { protected Print print; protected PrintOption printOption; + protected List data; + protected DataObject otptBscStng; protected List otptArtclStngList; protected String paperSeCd; @@ -48,9 +50,9 @@ public abstract class PDFPrintFormat { } public PDFPrintFormat(Print print, PrintOption printOption, - DataObject otptBscStng, List otptArtclStngList, String paperSeCd) { + DataObject otptBscStng, List otptArtclStngList, String paperSeCd, List data) { initDefaultStng(); - + this.data = data; this.print = print; this.print.setExtension("pdf"); this.printOption = printOption; @@ -225,14 +227,11 @@ public abstract class PDFPrintFormat { return result; } - public Map makePdfFile(List dataObjectList) { + public Map makeFile() { Map result = new HashMap<>(); PDFWriter writer = new PDFWriter(); - String resultFilePath = this.print.pathForNewFile(); - - //폰트 로드 Map fontMap = this.getFontMap(writer); @@ -274,7 +273,7 @@ public abstract class PDFPrintFormat { rprsCrdnPhotoYn = true; } - for(DataObject dataObject : dataObjectList) { + for(DataObject dataObject : data) { writer.beginPage(); boolean completeFoldLine = false; @@ -448,13 +447,10 @@ public abstract class PDFPrintFormat { } //출력자료건수 loop 끝 - writer.saveDocument(resultFilePath); - writer.closeDocument(); - String downloadFileName = this.print.getFormatKorName()+"_"+this.print.getPrintRequestDt()+".pdf"; - result.put("filename", downloadFileName); - result.put("filePath", resultFilePath); - result.put("file", new File(resultFilePath)); + result.put("download", writer.getDownloadable().setFilename(downloadFileName)); + result.put("downloadData", data); + result.put("dataNames", this.filterDownloadDataNames()); } catch (Exception e) { throw new RuntimeException("출력오류."+e); diff --git a/src/main/java/cokr/xit/fims/sndb/web/Sndb01Controller.java b/src/main/java/cokr/xit/fims/sndb/web/Sndb01Controller.java index b8909591..1bd18189 100644 --- a/src/main/java/cokr/xit/fims/sndb/web/Sndb01Controller.java +++ b/src/main/java/cokr/xit/fims/sndb/web/Sndb01Controller.java @@ -1783,12 +1783,10 @@ public class Sndb01Controller extends ApplicationController { } printOption.setGlobalVariable(globalVariable); - Advntce advntce = new Advntce(print,printOption,otptBscStng,otptArtclStngList,paperSeCd); - Map result = advntce.makePdfFile(dataObjectList); + Advntce pdfFormat = new Advntce(print,printOption,otptBscStng,otptArtclStngList,paperSeCd,dataObjectList); + Map result = pdfFormat.makeFile(); mav.addAllObjects(result); - mav.addObject("downloadData", dataObjectList); - mav.addObject("dataNames", advntce.filterDownloadDataNames()); return mav; } diff --git a/src/main/java/cokr/xit/fims/sprt/web/Sprt01Controller.java b/src/main/java/cokr/xit/fims/sprt/web/Sprt01Controller.java index 3a7d3042..3cc06156 100644 --- a/src/main/java/cokr/xit/fims/sprt/web/Sprt01Controller.java +++ b/src/main/java/cokr/xit/fims/sprt/web/Sprt01Controller.java @@ -773,12 +773,10 @@ public class Sprt01Controller extends ApplicationController { } printOption.setGlobalVariable(globalVariable); - Advntce advntce = new Advntce(print,printOption,otptBscStng,otptArtclStngList,paperSeCd); + Advntce pdfFormat = new Advntce(print,printOption,otptBscStng,otptArtclStngList,paperSeCd,dataObjectList); - Map result = advntce.makePdfFile(dataObjectList); + Map result = pdfFormat.makeFile(); mav.addAllObjects(result); - mav.addObject("downloadData", dataObjectList); - mav.addObject("dataNames", advntce.filterDownloadDataNames()); return mav; } @@ -1007,12 +1005,10 @@ public class Sprt01Controller extends ApplicationController { } printOption.setGlobalVariable(globalVariable); - Nht nht = new Nht(print,printOption,otptBscStng,otptArtclStngList,paperSeCd); + Nht pdfFormat = new Nht(print,printOption,otptBscStng,otptArtclStngList,paperSeCd,dataObjectList); - Map result = nht.makePdfFile(dataObjectList); + Map result = pdfFormat.makeFile(); mav.addAllObjects(result); - mav.addObject("downloadData", dataObjectList); - mav.addObject("dataNames", nht.filterDownloadDataNames()); return mav; }