From 66e44dbe3c9ec672108e3c2d1fa2fce099963302 Mon Sep 17 00:00:00 2001 From: leebeomjun Date: Mon, 27 Nov 2023 17:46:49 +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/DefaultOtptStng.java | 85 ++++++++++++ .../cokr/xit/fims/cmmn/pdf/PDFColors.java | 43 ++++++ .../cokr/xit/fims/cmmn/pdf/PDFFormat.java | 28 ++++ .../cokr/xit/fims/cmmn/pdf/PDFPrintUtil.java | 124 +++++++++++++++++- .../xit/fims/cmmn/pdf/format/Advntce.java | 17 ++- .../cokr/xit/fims/cmmn/pdf/format/Pstn.java | 16 +++ .../sprt/service/bean/Sprt01ServiceBean.java | 82 +++++------- .../sql/mapper/fims/sprt/otptStng-mapper.xml | 2 +- 8 files changed, 338 insertions(+), 59 deletions(-) create mode 100644 src/main/java/cokr/xit/fims/cmmn/pdf/DefaultOtptStng.java create mode 100644 src/main/java/cokr/xit/fims/cmmn/pdf/PDFColors.java create mode 100644 src/main/java/cokr/xit/fims/cmmn/pdf/PDFFormat.java create mode 100644 src/main/java/cokr/xit/fims/cmmn/pdf/format/Pstn.java diff --git a/src/main/java/cokr/xit/fims/cmmn/pdf/DefaultOtptStng.java b/src/main/java/cokr/xit/fims/cmmn/pdf/DefaultOtptStng.java new file mode 100644 index 00000000..5e44670a --- /dev/null +++ b/src/main/java/cokr/xit/fims/cmmn/pdf/DefaultOtptStng.java @@ -0,0 +1,85 @@ +package cokr.xit.fims.cmmn.pdf; + +import cokr.xit.fims.cmmn.pdf.format.Pstn; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class DefaultOtptStng { + + public DefaultOtptStng(String contentType, String otptNm, String colNm, String otptBscVl){ + this.contentType = contentType; + this.otptNm=otptNm; + this.colNm=colNm; + this.otptBscVl=otptBscVl; + + this.widthSz=0; + this.heightSz=0; + + this.fontNm="gulim"; + this.fontSize=10; + this.fontColor="BLACK"; + this.fontStyle="FILL"; + } + + public DefaultOtptStng a4(float leftPstn, float topPstn) { + this.a4 = new Pstn(leftPstn, topPstn); + return this; + } + public DefaultOtptStng letter(float leftPstn, float topPstn) { + this.letter = new Pstn(leftPstn, topPstn); + return this; + } + + public DefaultOtptStng size(float width, float height) { + this.widthSz = width; + this.heightSz = height; + return this; + } + + public DefaultOtptStng font(String fontNm, int fontSize, String fontColor, String fontStyle) { + this.fontNm = fontNm; + this.fontSize = fontSize; + this.fontColor = fontColor; + this.fontStyle = fontStyle; + return this; + } + + + public float getLeftPstn(String paper) { + switch (paper) { + case "01": { + return this.getA4().getLeft(); + } + case "02": { + return this.getLetter().getLeft(); + } + } + return 0; + } + public float getTopPstn(String paper) { + switch (paper) { + case "01": { + return this.getA4().getTop(); + } + case "02": { + return this.getLetter().getTop(); + } + } + return 0; + } + + private String contentType; + private String otptNm; + private String colNm; + private String otptBscVl; + private Pstn a4; + private Pstn letter; + private float widthSz; + private float heightSz; + private String fontNm; + private int fontSize; + private String fontColor; + private String fontStyle; +} diff --git a/src/main/java/cokr/xit/fims/cmmn/pdf/PDFColors.java b/src/main/java/cokr/xit/fims/cmmn/pdf/PDFColors.java new file mode 100644 index 00000000..0e6950bb --- /dev/null +++ b/src/main/java/cokr/xit/fims/cmmn/pdf/PDFColors.java @@ -0,0 +1,43 @@ +package cokr.xit.fims.cmmn.pdf; + +import java.lang.reflect.Field; + +import org.apache.pdfbox.pdmodel.graphics.color.PDColor; +import org.apache.pdfbox.pdmodel.graphics.color.PDDeviceRGB; + +public class PDFColors { + public static final PDColor RED = new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE); + public static final PDColor ORANGE = new PDColor(new float[] { 1, 0.502f, 0 }, PDDeviceRGB.INSTANCE); + public static final PDColor YELLOW = new PDColor(new float[] { 1, 1, 0 }, PDDeviceRGB.INSTANCE); + public static final PDColor YELLOW_GREEN = new PDColor(new float[] { 0.502f, 1, 0 }, PDDeviceRGB.INSTANCE); + public static final PDColor GREEN = new PDColor(new float[] { 0, 1, 0 }, PDDeviceRGB.INSTANCE); + public static final PDColor GREEN_CYAN = new PDColor(new float[] { 0, 1, 0.502f }, PDDeviceRGB.INSTANCE); + public static final PDColor CYAN = new PDColor(new float[] { 0, 1, 1 }, PDDeviceRGB.INSTANCE); + public static final PDColor CYAN_BLUE = new PDColor(new float[] { 0, 0.502f, 1 }, PDDeviceRGB.INSTANCE); + public static final PDColor BLUE = new PDColor(new float[] { 0, 1, 0 }, PDDeviceRGB.INSTANCE); + public static final PDColor VIOLET = new PDColor(new float[] { 0.502f, 1, 0 }, PDDeviceRGB.INSTANCE); + public static final PDColor MAGENTA = new PDColor(new float[] { 1, 1, 0 }, PDDeviceRGB.INSTANCE); + public static final PDColor DEEP_PINK = new PDColor(new float[] { 1, 0.502f, 0 }, PDDeviceRGB.INSTANCE); + public static final PDColor WHITE = new PDColor(new float[] { 1, 1, 1 }, PDDeviceRGB.INSTANCE); + public static final PDColor LIGHT_GREY = new PDColor(new float[] { 0.753f, 0.753f, 0.753f }, PDDeviceRGB.INSTANCE); + public static final PDColor GREY = new PDColor(new float[] { 0.627f, 0.627f, 0.627f }, PDDeviceRGB.INSTANCE); + public static final PDColor DARK_GREY = new PDColor(new float[] { 0.376f, 0.376f, 0.376f }, PDDeviceRGB.INSTANCE); + public static final PDColor BLACK = new PDColor(new float[] { 0, 0, 0 }, PDDeviceRGB.INSTANCE); + + + public static PDColor getColor(String colorName) { + + try { + Field field = PDFColors.class.getDeclaredField(colorName); + if(field == null) { + return BLACK; + } + + field.setAccessible(true); + PDColor value = (PDColor)field.get(PDFColors.class); + return value; + } catch (Exception e) { + throw new RuntimeException("색상 설정 오류"); + } + } +} diff --git a/src/main/java/cokr/xit/fims/cmmn/pdf/PDFFormat.java b/src/main/java/cokr/xit/fims/cmmn/pdf/PDFFormat.java new file mode 100644 index 00000000..8fd11f36 --- /dev/null +++ b/src/main/java/cokr/xit/fims/cmmn/pdf/PDFFormat.java @@ -0,0 +1,28 @@ +package cokr.xit.fims.cmmn.pdf; + +import java.util.ArrayList; +import java.util.List; + +public class PDFFormat { + + protected List defaultOtptStng = new ArrayList(); + + public List getDefaultOtptStng(){ + return this.defaultOtptStng; + } + + public DefaultOtptStng text(String otptNm, String colNm, String otptBscVl){ + return new DefaultOtptStng("text", otptNm, colNm, otptBscVl); + } + + public DefaultOtptStng image(String otptNm, String colNm){ + return new DefaultOtptStng("image" ,otptNm, colNm, ""); + } + + public void add(DefaultOtptStng... defaultOtptStngs){ + for(DefaultOtptStng defaultOtptStng : defaultOtptStngs) { + this.defaultOtptStng.add(defaultOtptStng); + } + } + +} diff --git a/src/main/java/cokr/xit/fims/cmmn/pdf/PDFPrintUtil.java b/src/main/java/cokr/xit/fims/cmmn/pdf/PDFPrintUtil.java index ec16c5d5..45b19104 100644 --- a/src/main/java/cokr/xit/fims/cmmn/pdf/PDFPrintUtil.java +++ b/src/main/java/cokr/xit/fims/cmmn/pdf/PDFPrintUtil.java @@ -1,16 +1,20 @@ package cokr.xit.fims.cmmn.pdf; -import java.awt.Color; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.Map; +import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPageContentStream; 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 cokr.xit.fims.cmmn.PrintUtil; +import cokr.xit.foundation.data.DataObject; public class PDFPrintUtil extends PrintUtil { @@ -62,10 +66,11 @@ public class PDFPrintUtil extends PrintUtil { // public void writeText(PDPageContentStream contentStream, String text, float[] XYmm, - PDType0Font font, int fontSize, RenderingMode fontStyle, Color fontColor) { + PDType0Font font, int fontSize, RenderingMode fontStyle, PDColor fontColor) { try { - float[] xyOffset = this.toPDFCoordinate(XYmm).offset(); + float[] xyAbsolute = this.toPDFCoordinate(XYmm).absolute(); + contentStream.beginText(); contentStream.setFont(font, fontSize); @@ -73,7 +78,8 @@ public class PDFPrintUtil extends PrintUtil { contentStream.setNonStrokingColor(fontColor); - contentStream.newLineAtOffset(xyOffset[0], xyOffset[1]); + contentStream.newLineAtOffset(xyAbsolute[0], xyAbsolute[1]); + contentStream.showText(text); contentStream.endText(); @@ -83,13 +89,28 @@ public class PDFPrintUtil extends PrintUtil { } + public void insertImage(PDDocument doc, PDPageContentStream contentStream, String imagePath, float[] XYmm, + float[] SIZEmm) { + + float[] xyAbsolute = this.toPDFCoordinate(XYmm).absolute(); + float[] size = this.toPDFCoordinate(SIZEmm).offset(); + try { + if(!imagePath.equals("")) { + PDImageXObject image = PDImageXObject.createFromFile(imagePath, doc); + contentStream.drawImage(image, xyAbsolute[0], xyAbsolute[1], size[0], size[1]); + } + } catch (IOException e) { + throw new RuntimeException("PDF 파일 출력 중 오류가 발생하였습니다."); + } + } + public void renderFoldLine(PDPageContentStream contentStream, float paperWidth, float paperHeight) { float foldHeight = 712/3; float bottomFold = foldHeight; //하단접는곳 float topFold = foldHeight * 2; //상단접는곳 - if(paperHeight == 842f) { + if(paperHeight == 841.8898f) { bottomFold += 130f; topFold += 130f; } @@ -107,4 +128,97 @@ public class PDFPrintUtil extends PrintUtil { throw new RuntimeException("PDF 파일 출력 중 오류가 발생하였습니다."); } } + + public PDType0Font getFontStng(DefaultOtptStng defaultStng, DataObject sggStng, Map fontMap) { + if(sggStng != null && !sggStng.isEmpty() && !sggStng.string("FONT_NM").equals("")) { + return fontMap.get(sggStng.string("FONT_NM")); + } else { + return fontMap.get(defaultStng.getFontNm()); + } + } + + public float[] getPstnStng(DefaultOtptStng defaultStng, DataObject sggStng, String otptPaperSeCd) { + if(sggStng != null && !sggStng.isEmpty()) { + return new float[] { + sggStng.number("LEFT_PSTN").floatValue(), + sggStng.number("TOP_PSTN").floatValue() + }; + } else { + return new float[] { + defaultStng.getLeftPstn(otptPaperSeCd), + defaultStng.getTopPstn(otptPaperSeCd) + }; + } + } + + public int getFontSize(DefaultOtptStng defaultStng, DataObject sggStng) { + if(sggStng != null && !sggStng.isEmpty() && !sggStng.string("FONT_SIZE").equals("")) { + return sggStng.number("FONT_SIZE").intValue(); + } else { + return defaultStng.getFontSize(); + } + } + + public RenderingMode getFontStyle(DefaultOtptStng defaultStng, DataObject sggStng) { + if(sggStng != null && !sggStng.isEmpty() && !sggStng.string("FONT_STYLE").equals("")) { + return RenderingMode.valueOf(sggStng.string("FONT_STYLE")); + } else { + return RenderingMode.valueOf(defaultStng.getFontStyle()); + } + } + + public PDColor getFontColor(DefaultOtptStng defaultStng, DataObject sggStng) { + if(sggStng != null && !sggStng.isEmpty() && !sggStng.string("FONT_COLOR").equals("")) { + return PDFColors.getColor(sggStng.string("FONT_COLOR")); + } else { + return PDFColors.getColor(defaultStng.getFontColor()); + } + } + + public String getTextValue(DefaultOtptStng defaultStng, DataObject sggStng, DataObject dataObject) { + String textValue = ""; + if(sggStng != null && !sggStng.isEmpty() && !sggStng.string("COL_NM").equals("")) { + + textValue = dataObject.string(sggStng.string("COL_NM")); + if(textValue.equals("")) { + textValue = sggStng.string("OTPT_BSC_VL"); + } + + } else { + + textValue = dataObject.string(defaultStng.getColNm()); + if(textValue.equals("")) { + textValue = defaultStng.getOtptBscVl(); + } + + } + + return textValue; + } + + public String getImagePath(DefaultOtptStng defaultStng, DataObject sggStng, DataObject dataObject) { + if(sggStng != null && !sggStng.isEmpty() && !sggStng.string("COL_NM").equals("")) { + + return dataObject.string(sggStng.string("COL_NM")); + + } else { + + return dataObject.string(defaultStng.getColNm()); + + } + } + + public float[] getSize(DefaultOtptStng defaultStng, DataObject sggStng) { + if(sggStng != null && !sggStng.isEmpty()) { + return new float[] { + sggStng.number("LEFT_PSTN").floatValue(), + sggStng.number("TOP_PSTN").floatValue() + }; + } else { + return new float[] { + defaultStng.getWidthSz(), + defaultStng.getHeightSz() + }; + } + } } diff --git a/src/main/java/cokr/xit/fims/cmmn/pdf/format/Advntce.java b/src/main/java/cokr/xit/fims/cmmn/pdf/format/Advntce.java index fd8da20b..74cdb9c7 100644 --- a/src/main/java/cokr/xit/fims/cmmn/pdf/format/Advntce.java +++ b/src/main/java/cokr/xit/fims/cmmn/pdf/format/Advntce.java @@ -1,5 +1,20 @@ package cokr.xit.fims.cmmn.pdf.format; -public class Advntce { +import cokr.xit.fims.cmmn.pdf.PDFFormat; +/** + * 사전통지서 + */ +public class Advntce extends PDFFormat { + + public Advntce(){ + add( + text("CRDN_YMD","CRDN_YMD","").a4(105, 150).letter(0, 0), + text("VHRNO","VHRNO","").a4(30, 30).letter(0, 0) + + //image("OFFCS1","OFFCS").a4(0, 0).letter(0, 0).size(0, 0), + //image("OFFCS2","OFFCS").a4(0, 0).letter(0, 0).size(0, 0), + //image("OFFCS3","OFFCS").a4(0, 0).letter(0, 0).size(0, 0) + ); + } } diff --git a/src/main/java/cokr/xit/fims/cmmn/pdf/format/Pstn.java b/src/main/java/cokr/xit/fims/cmmn/pdf/format/Pstn.java new file mode 100644 index 00000000..4dc90e15 --- /dev/null +++ b/src/main/java/cokr/xit/fims/cmmn/pdf/format/Pstn.java @@ -0,0 +1,16 @@ +package cokr.xit.fims.cmmn.pdf.format; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class Pstn { + public Pstn(float left, float top){ + this.left = left; + this.top = top; + } + + private float left; + private float top; +} diff --git a/src/main/java/cokr/xit/fims/sprt/service/bean/Sprt01ServiceBean.java b/src/main/java/cokr/xit/fims/sprt/service/bean/Sprt01ServiceBean.java index 0bfbb820..1a170b5a 100644 --- a/src/main/java/cokr/xit/fims/sprt/service/bean/Sprt01ServiceBean.java +++ b/src/main/java/cokr/xit/fims/sprt/service/bean/Sprt01ServiceBean.java @@ -1,11 +1,11 @@ package cokr.xit.fims.sprt.service.bean; -import java.awt.Color; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import javax.annotation.Resource; @@ -14,6 +14,7 @@ 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.state.RenderingMode; import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Service; @@ -28,7 +29,10 @@ import cokr.xit.fims.cmmn.hwp.format.CrdnList; import cokr.xit.fims.cmmn.hwp.format.RcvmtConfirm; import cokr.xit.fims.cmmn.hwp.format.ReportOnClaims; import cokr.xit.fims.cmmn.hwp.format.RequestForDelivery; +import cokr.xit.fims.cmmn.pdf.DefaultOtptStng; +import cokr.xit.fims.cmmn.pdf.PDFFormat; import cokr.xit.fims.cmmn.pdf.PDFPrintUtil; +import cokr.xit.fims.cmmn.pdf.format.Advntce; import cokr.xit.fims.cmmn.service.bean.FactionBean; import cokr.xit.fims.cmmn.service.bean.StngBean; import cokr.xit.fims.crdn.Crdn; @@ -60,6 +64,8 @@ import kr.dogfoot.hwplib.writer.HWPWriter; @Service("sprt01Service") public class Sprt01ServiceBean extends AbstractServiceBean implements Sprt01Service { + private Object object; + /** 업무지원 Bean */ @Resource(name="sprt01Bean") protected Sprt01Bean sprt01Bean; @@ -318,10 +324,19 @@ public class Sprt01ServiceBean extends AbstractServiceBean implements Sprt01Serv if(formatName.equals("advntce")) { sndngSeCd = "02"; } + + PDFFormat pdfFormat = null; + if(formatName.equals("advntce")) { + pdfFormat = new Advntce(); + } + List predefinedList = pdfFormat.getDefaultOtptStng(); + otptStngQuery.setSndngSeCd(sndngSeCd); otptStngQuery.setOtptPaperSeCd(otptPaperSeCd); List otptStng = otptStngMapper.selectOtptStngList(otptStngQuery); + Map byOtptItemStng = otptStng.stream() + .collect(Collectors.toMap((item) -> item.string("OTPT_NM"), (item) -> item)); for(DataObject dataObject : dataObjectList) { //페이지생성(건별) @@ -333,72 +348,35 @@ public class Sprt01ServiceBean extends AbstractServiceBean implements Sprt01Serv // 컨텐츠 스트림 열기 PDPageContentStream contentStream = new PDPageContentStream(doc, page); - //접는곳 점선 그리기 pdfPrintUtil.renderFoldLine(contentStream, paperWidth, paperHeight); - //이미지 그리기 - //단속 이미지 로드(건별) - //PDImageXObject crdnPhotoImage = PDImageXObject.createFromFile("samplefiles/sample.JPG", doc); - - //float photosXstart; - //float photosYstart; - //float photosXend; - //float photosYend; - - //단속사진 - //contentStream.drawImage(crdnPhotoImage, 300, 280, 250, 280); - //사각형(모자이크) - //contentStream.addRect(0, 0, 0, 0); + for(DefaultOtptStng predefined : predefinedList) { + DataObject otptItemStng = byOtptItemStng.get(predefined.getOtptNm()); - //직인 - //float[] offcsLocation = {300, 50}; - //float offcsSize = 20; - //contentStream.drawImage(offcsImage, offcsLocation[0], offcsLocation[1], offcsSize, offcsSize); + float[] pstn = pdfPrintUtil.getPstnStng(predefined, otptItemStng, otptPaperSeCd); - //지자체로고 - //contentStream.drawImage(logoImage, ?, ?, logoWidth, logoHeight); - //배경 이미지 - //contentStream.drawImage(pdImage, 0, 0, paperWidth, paperHeight); + if(predefined.getContentType().equals("text")) { + String textValue = pdfPrintUtil.getTextValue(predefined, otptItemStng, dataObject); + PDType0Font font = pdfPrintUtil.getFontStng(predefined, otptItemStng, fontMap); + int fontSz = pdfPrintUtil.getFontSize(predefined, otptItemStng); + RenderingMode fontStyle = pdfPrintUtil.getFontStyle(predefined, otptItemStng); + PDColor fontColor = pdfPrintUtil.getFontColor(predefined, otptItemStng); + pdfPrintUtil.writeText(contentStream, textValue, pstn, font, fontSz, fontStyle, fontColor); - for(DataObject otptItemStng : otptStng) { + } else if(predefined.getContentType().equals("image")) { - String otptNm = otptItemStng.string("OTPT_NM"); + String imagePath = pdfPrintUtil.getImagePath(predefined, otptItemStng, dataObject); + float[] size = pdfPrintUtil.getSize(predefined, otptItemStng); + pdfPrintUtil.insertImage(doc, contentStream, imagePath, pstn, size); - String dataValue = dataObject.string(otptItemStng.string("COL_NM")); - if(dataValue.equals("")) { - dataValue = otptItemStng.string("OTPT_BSC_VL"); } - float[] pstn = new float[] { - otptItemStng.number("LEFT_PSTN").floatValue(), - otptItemStng.number("TOP_PSTN").floatValue() - }; - - PDType0Font font = fontMap.get("gulim"); - if(!otptItemStng.string("FONT_NM").equals("")) { - font = fontMap.get(otptItemStng.string("FONT_NM")); - } - int fontSz = 10; - if(otptItemStng.number("FONT_SIZE").intValue() != 0) { - fontSz = otptItemStng.number("FONT_SIZE").intValue(); - } - RenderingMode fontStyle = RenderingMode.valueOf("FILL"); - if(!otptItemStng.string("FONT_STYLE").equals("")) { - fontStyle = RenderingMode.valueOf(otptItemStng.string("FONT_STYLE")); - } - Color fontColor = Color.getColor("BLACK"); - if(!otptItemStng.string("FONT_COLOR").equals("")) { - fontColor = Color.getColor(otptItemStng.string("FONT_COLOR")); - } - - pdfPrintUtil.writeText(contentStream, dataValue, pstn, font, fontSz, fontStyle, fontColor); - } contentStream.close(); diff --git a/src/main/resources/sql/mapper/fims/sprt/otptStng-mapper.xml b/src/main/resources/sql/mapper/fims/sprt/otptStng-mapper.xml index 20f8c174..1340bc56 100644 --- a/src/main/resources/sql/mapper/fims/sprt/otptStng-mapper.xml +++ b/src/main/resources/sql/mapper/fims/sprt/otptStng-mapper.xml @@ -2,7 +2,7 @@ - /* 출력 설정 조회(otptStngMapper.selectOtptStngList) */ SELECT OTPT_FORM_ID , SGG_CD