diff --git a/src/main/java/cokr/xit/fims/cmmn/pdf/print/PDF.java b/src/main/java/cokr/xit/fims/cmmn/pdf/print/PDF.java new file mode 100644 index 00000000..b49636bd --- /dev/null +++ b/src/main/java/cokr/xit/fims/cmmn/pdf/print/PDF.java @@ -0,0 +1,16 @@ +package cokr.xit.fims.cmmn.pdf.print; + +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPageContentStream; +import org.apache.pdfbox.pdmodel.common.PDRectangle; + +import cokr.xit.foundation.AbstractComponent; + +public abstract class PDF extends AbstractComponent { + /**pdf 파일(*.pdf)의 mime type */ + public static final String MIME_TYPE = "application/pdf"; + + protected PDDocument doc; + protected PDPageContentStream contentStream; + protected PDRectangle paperSize; +} 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 4ff2f2b7..14c94097 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,5 +1,119 @@ package cokr.xit.fims.cmmn.pdf.print; -public class PDFWriter { +import java.io.IOException; +import java.io.InputStream; + +import org.apache.pdfbox.pdmodel.PDDocument; +import org.apache.pdfbox.pdmodel.PDPage; +import org.apache.pdfbox.pdmodel.PDPageContentStream; +import org.apache.pdfbox.pdmodel.common.PDRectangle; +import org.apache.pdfbox.pdmodel.font.PDType0Font; +import org.apache.pdfbox.pdmodel.graphics.color.PDColor; +import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; +import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode; +import org.springframework.core.io.ClassPathResource; + +public class PDFWriter extends PDF { + + public PDFWriter(){ + this.doc = new PDDocument(); + } + + public PDFWriter paper(String type){ + if(type.equals("A4")) { + this.paperSize = PDRectangle.A4; + } else if(type.equals("LETTER")) { + this.paperSize = PDRectangle.LETTER; + } + return this; + } + + public PDRectangle getPaperSize(){ + return this.paperSize; + } + + public PDType0Font font(String path) throws IOException{ + return PDType0Font.load(doc, new ClassPathResource(path).getInputStream()); + } + + public PDFWriter beginPage() throws IOException { + PDPage blankPage = new PDPage(paperSize); + doc.addPage(blankPage); + + // 작업 페이지 설정 + PDPage page = doc.getPage(doc.getNumberOfPages()-1); + // 컨텐츠 스트림 열기 + contentStream = new PDPageContentStream(doc, page); + return this; + } + + public PDFWriter setLineDashPattern(float[] pattern, float phase) throws IOException{ + contentStream.setLineDashPattern(pattern, phase); + return this; + } + public PDFWriter moveTo(float x, float y) throws IOException { + contentStream.moveTo(x, y); + return this; + } + + public PDFWriter lineTo(float x, float y) throws IOException { + contentStream.lineTo(x, y); + return this; + } + public PDFWriter stroke() throws IOException { + contentStream.stroke(); + return this; + } + public PDFWriter beginText() throws IOException { + contentStream.beginText(); + return this; + } + public PDFWriter setFont(PDType0Font font, int fontSize) throws IOException { + contentStream.setFont(font, fontSize); + return this; + } + public PDFWriter setRenderingMode(RenderingMode fontStyle) throws IOException { + contentStream.setRenderingMode(fontStyle); + return this; + } + public PDFWriter setNonStrokingColor(PDColor fontColor) throws IOException { + contentStream.setNonStrokingColor(fontColor); + return this; + } + public PDFWriter newLineAtOffset(float tx, float ty) throws IOException { + contentStream.newLineAtOffset(tx, ty); + return this; + } + public PDFWriter showText(String text) throws IOException { + contentStream.showText(text); + return this; + } + public PDFWriter endText() throws IOException { + contentStream.endText(); + return this; + } + + public PDFWriter image(InputStream imageStream, float x, float y, float width, float height) throws IOException { + PDImageXObject image = PDImageXObject.createFromByteArray(doc, imageStream.readAllBytes(), "temp"); + contentStream.drawImage(image, x, y, width, height); + return this; + } + + public PDFWriter endPage() throws IOException { + contentStream.close(); + contentStream = null; + return this; + } + + public PDFWriter saveDocument(String path) throws IOException{ + doc.save(path); + return this; + } + + public PDFWriter closeDocument() throws IOException{ + doc.close(); + return this; + } + } 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 af6651b8..595e8714 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 @@ -13,13 +13,9 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import org.apache.pdfbox.pdmodel.PDDocument; -import org.apache.pdfbox.pdmodel.PDPage; -import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.font.PDType0Font; import org.apache.pdfbox.pdmodel.graphics.color.PDColor; -import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; import org.apache.pdfbox.pdmodel.graphics.state.RenderingMode; import org.egovframe.rte.fdl.string.EgovStringUtil; import org.springframework.core.io.ClassPathResource; @@ -33,6 +29,7 @@ import cokr.xit.fims.cmmn.Size; import cokr.xit.fims.cmmn.pdf.print.DefaultOtptArtclStng; import cokr.xit.fims.cmmn.pdf.print.PDFColors; import cokr.xit.fims.cmmn.pdf.print.PDFCoordinate; +import cokr.xit.fims.cmmn.pdf.print.PDFWriter; import cokr.xit.fims.sprt.MediaUtil; import cokr.xit.foundation.data.DataObject; @@ -232,20 +229,20 @@ public abstract class PDFPrintFormat { public Map makePdfFile(List dataObjectList) { Map result = new HashMap<>(); - + PDFWriter writer = new PDFWriter(); String resultFilePath = this.filePathForNewFile(); - PDDocument doc = new PDDocument();//pdf문서 생성 + //폰트 로드 - Map fontMap = this.getFontMap(doc); + Map fontMap = this.getFontMap(writer); //용지 크기 설정 - PDRectangle paper = null; + //PDRectangle paper = null; if(this.paperSeCd.equals("01")) { - paper = PDRectangle.A4; + writer.paper("A4"); } else { - paper = PDRectangle.LETTER; + writer.paper("LETTER"); } @@ -279,14 +276,7 @@ public abstract class PDFPrintFormat { } for(DataObject dataObject : dataObjectList) { - //페이지생성(건별) - PDPage blankPage = new PDPage(paper); - doc.addPage(blankPage); - - // 작업 페이지 설정 - PDPage page = doc.getPage(doc.getNumberOfPages()-1); - // 컨텐츠 스트림 열기 - PDPageContentStream contentStream = new PDPageContentStream(doc, page); + writer.beginPage(); boolean completeFoldLine = false; @@ -296,7 +286,7 @@ public abstract class PDFPrintFormat { if(!otptArtclNm.equals("background") && !completeFoldLine) { //접는곳 점선 그리기 if(!otptBscStng.string("FOLD_LINE_PSTN_SE_CD").equals("")) { - this.renderFoldLine(contentStream, paper, otptBscStng.string("FOLD_LINE_PSTN_SE_CD")); + this.renderFoldLine(writer, otptBscStng.string("FOLD_LINE_PSTN_SE_CD")); } completeFoldLine = true; } @@ -320,7 +310,7 @@ public abstract class PDFPrintFormat { String textValue = this.getMappingValue(otptArtclNm,defaultValue,forPost,dataObject,printOption, this.print); - this.writeText(contentStream, textValue, pstn, size[0], align, lineChgYn + this.writeText(writer, textValue, pstn, size[0], align, lineChgYn , font, fontSz, fontStyle, fontColor); } else if(prototypeStng.getComponentType().equals("image")) { @@ -344,7 +334,7 @@ public abstract class PDFPrintFormat { imageKey = this.getMappingValue(otptArtclNm+"Key",defaultValue,forPost,dataObject,printOption,this.print); mosSet = this.getMosSet(imageKey, dataObject); } - this.insertImage(doc, contentStream, is, pstn, size); + this.insertImage(writer, is, pstn, size); if(otptArtclNm.equals("rprsCrdnPhoto") && !imageKey.equals("") && mosSet != null) { @@ -359,7 +349,7 @@ public abstract class PDFPrintFormat { PstnAndSize mosaicInfo = transformedMosaicInfos.get(k); InputStream blackImageStream = new ClassPathResource("samplefiles/black.png").getInputStream(); - this.insertImage(doc, contentStream, blackImageStream, + this.insertImage(writer, blackImageStream, mosaicInfo.getPstn().to2Float(), mosaicInfo.getSize().to2Float()); } @@ -412,7 +402,7 @@ public abstract class PDFPrintFormat { File imageFile = new File(imagePath); InputStream is = new FileInputStream(imageFile); - this.insertImage(doc, contentStream, is, + this.insertImage(writer, is, devideInfo.getPstn().to2Float(), devideInfo.getSize().to2Float()); @@ -431,7 +421,7 @@ public abstract class PDFPrintFormat { for(int k=0; k < transformedMosaicInfos.size(); k++) { PstnAndSize mosaicInfo = transformedMosaicInfos.get(k); InputStream blackImageStream = new ClassPathResource("samplefiles/black.png").getInputStream(); - this.insertImage(doc, contentStream, blackImageStream, + this.insertImage(writer, blackImageStream, mosaicInfo.getPstn().to2Float(), mosaicInfo.getSize().to2Float()); } } @@ -450,17 +440,17 @@ public abstract class PDFPrintFormat { if(!completeFoldLine) { if(!otptBscStng.string("FOLD_LINE_PSTN_SE_CD").equals("")) { - this.renderFoldLine(contentStream, paper, otptBscStng.string("FOLD_LINE_PSTN_SE_CD")); + this.renderFoldLine(writer, otptBscStng.string("FOLD_LINE_PSTN_SE_CD")); } } - contentStream.close(); + writer.endPage(); } //출력자료건수 loop 끝 - doc.save(resultFilePath); - doc.close(); + writer.saveDocument(resultFilePath); + writer.closeDocument(); String downloadFileName = this.print.getFormatKorName()+"_"+this.print.getPrintRequestDt()+".pdf"; result.put("filename", downloadFileName); @@ -524,7 +514,7 @@ public abstract class PDFPrintFormat { /** * 텍스트를 pdf에 입력한다. */ - public void writeText(PDPageContentStream contentStream, String allText, + public void writeText(PDFWriter writer, String allText, float[] XYmm, float textAreaWidth_mm, String align, String newLineYn, PDType0Font font, int fontSize, RenderingMode fontStyle, PDColor fontColor) { try { @@ -557,10 +547,10 @@ public abstract class PDFPrintFormat { String firstLine = textArr[0]; float firstLineWidth = calcTextWidth(font, fontSize, firstLine); - contentStream.beginText(); - contentStream.setFont(font, fontSize); - contentStream.setRenderingMode(fontStyle); - contentStream.setNonStrokingColor(fontColor); + writer.beginText(); + writer.setFont(font, fontSize); + writer.setRenderingMode(fontStyle); + writer.setNonStrokingColor(fontColor); float resultY = xyAbsolute[1] - fontSize; @@ -573,8 +563,8 @@ public abstract class PDFPrintFormat { float resultX = xyAbsolute[0] + firstLineAlignCorrection; - contentStream.newLineAtOffset(resultX, resultY); - contentStream.showText(textArr[0]); + writer.newLineAtOffset(resultX, resultY); + writer.showText(textArr[0]); if(textArr.length > 1) { float beforeLineWidth = firstLineWidth; @@ -590,8 +580,8 @@ public abstract class PDFPrintFormat { alignCorrection = (beforeLineWidth - thisLineWidth)/2.0f; } - contentStream.newLineAtOffset(alignCorrection, -(fontSize + 1)); - contentStream.showText(thisLine); + writer.newLineAtOffset(alignCorrection, -(fontSize + 1)); + writer.showText(thisLine); beforeLineWidth = thisLineWidth; } @@ -599,7 +589,7 @@ public abstract class PDFPrintFormat { } - contentStream.endText(); + writer.endText(); } catch (IOException e) { throw new RuntimeException("PDF 파일 출력 중 오류가 발생하였습니다."+e); @@ -669,7 +659,7 @@ public abstract class PDFPrintFormat { /** * pdf에 이미지를 삽입한다. */ - public void insertImage(PDDocument doc, PDPageContentStream contentStream, InputStream imageStream, float[] XYmm, + public void insertImage(PDFWriter writer, InputStream imageStream, float[] XYmm, float[] SIZEmm) { float[] xyAbsolute = this.toPDFCoordinate(XYmm).absolute(); @@ -678,9 +668,7 @@ public abstract class PDFPrintFormat { CmmnUtil.mmToPt(SIZEmm[0]),CmmnUtil.mmToPt(SIZEmm[1]) }; try { - - PDImageXObject image = PDImageXObject.createFromByteArray(doc, imageStream.readAllBytes(), "temp"); - contentStream.drawImage(image, xyAbsolute[0], xyAbsolute[1]-size[1], size[0], size[1]); + writer.image(imageStream, xyAbsolute[0], xyAbsolute[1]-size[1], size[0], size[1]); } catch (IOException e) { throw new RuntimeException("PDF 파일 출력 중 오류가 발생하였습니다."+e); @@ -818,16 +806,16 @@ public abstract class PDFPrintFormat { /** * pdf에 봉합선을 넣는다. */ - public void renderFoldLine(PDPageContentStream contentStream, PDRectangle paper, String foldCode) { + public void renderFoldLine(PDFWriter writer, String foldCode) { if(foldCode.equals("") && !foldCode.equals("01") && !foldCode.equals("02")) { return; } - float paperWidth = paper.getWidth(); + float paperWidth = writer.getPaperSize().getWidth(); float partHeight; if(foldCode.equals("01")) { - if(PDRectangle.A4.equals(paper)) { + if(PDRectangle.A4.equals(writer.getPaperSize())) { partHeight = 99; } else { partHeight = 93; @@ -848,14 +836,14 @@ public abstract class PDFPrintFormat { float topFold_pt = CmmnUtil.mmToPt(topFold); try { - contentStream.setLineDashPattern(new float[]{3,1}, 0); - contentStream.moveTo(0, topFold_pt); - contentStream.lineTo(paperWidth, topFold_pt); - contentStream.stroke(); - contentStream.moveTo(0, bottomFold_pt); - contentStream.lineTo(paperWidth, bottomFold_pt); - contentStream.stroke(); - contentStream.setLineDashPattern(new float[]{}, 0); + writer.setLineDashPattern(new float[]{3,1}, 0); + writer.moveTo(0, topFold_pt); + writer.lineTo(paperWidth, topFold_pt); + writer.stroke(); + writer.moveTo(0, bottomFold_pt); + writer.lineTo(paperWidth, bottomFold_pt); + writer.stroke(); + writer.setLineDashPattern(new float[]{}, 0); } catch (IOException e) { throw new RuntimeException("PDF 파일 출력 중 오류가 발생하였습니다."+e); } @@ -937,17 +925,18 @@ public abstract class PDFPrintFormat { } } - public Map getFontMap(PDDocument doc) { + public Map getFontMap(PDFWriter writer) { try { + Map fontMap = Map.of( - "gulim", PDType0Font.load(doc, new ClassPathResource("fonts/gulim.ttf").getInputStream()), - "gulimche", PDType0Font.load(doc, new ClassPathResource("fonts/gulimche.ttf").getInputStream()), - "batang", PDType0Font.load(doc, new ClassPathResource("fonts/batang.ttf").getInputStream()), - "batangche", PDType0Font.load(doc, new ClassPathResource("fonts/batangche.ttf").getInputStream()), - "dotum", PDType0Font.load(doc, new ClassPathResource("fonts/dotum.ttf").getInputStream()), - "dotumche", PDType0Font.load(doc, new ClassPathResource("fonts/dotumche.ttf").getInputStream()), - "gungsuh", PDType0Font.load(doc, new ClassPathResource("fonts/gungsuh.ttf").getInputStream()), - "gungsuhche", PDType0Font.load(doc, new ClassPathResource("fonts/gungsuhche.ttf").getInputStream()) + "gulim", writer.font("fonts/gulim.ttf"), + "gulimche", writer.font("fonts/gulimche.ttf"), + "batang", writer.font("fonts/batang.ttf"), + "batangche", writer.font("fonts/batangche.ttf"), + "dotum", writer.font("fonts/dotum.ttf"), + "dotumche", writer.font("fonts/dotumche.ttf"), + "gungsuh", writer.font("fonts/gungsuh.ttf"), + "gungsuhche", writer.font("fonts/gungsuhche.ttf") ); return fontMap; } catch (IOException e) {