--- src/main/java/itn/com/cmm/util/PdfUtil.java
+++ src/main/java/itn/com/cmm/util/PdfUtil.java
... | ... | @@ -1,11 +1,21 @@ |
| 1 | 1 |
package itn.com.cmm.util; |
| 2 | 2 |
|
| 3 |
+import java.awt.Image; |
|
| 3 | 4 |
import java.io.BufferedOutputStream; |
| 4 | 5 |
import java.io.File; |
| 5 | 6 |
import java.io.FileInputStream; |
| 6 | 7 |
import java.io.IOException; |
| 8 |
+import java.util.UUID; |
|
| 7 | 9 |
|
| 10 |
+import javax.imageio.ImageIO; |
|
| 8 | 11 |
import javax.servlet.http.HttpServletResponse; |
| 12 |
+ |
|
| 13 |
+import org.apache.commons.io.FileUtils; |
|
| 14 |
+import org.apache.pdfbox.pdmodel.PDDocument; |
|
| 15 |
+import org.apache.pdfbox.pdmodel.PDPage; |
|
| 16 |
+import org.apache.pdfbox.pdmodel.PDPageContentStream; |
|
| 17 |
+import org.apache.pdfbox.pdmodel.common.PDRectangle; |
|
| 18 |
+import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; |
|
| 9 | 19 |
|
| 10 | 20 |
/** |
| 11 | 21 |
* |
... | ... | @@ -88,4 +98,53 @@ |
| 88 | 98 |
} |
| 89 | 99 |
|
| 90 | 100 |
|
| 101 |
+ public static String makeImgPdf(String imgDir,String extsn) throws Exception {
|
|
| 102 |
+ PDDocument doc = new PDDocument(); |
|
| 103 |
+ String uuid = UUID.randomUUID().toString(); |
|
| 104 |
+ try {
|
|
| 105 |
+ File copy1 = new File(imgDir); |
|
| 106 |
+ File copy2 = new File(imgDir + "." +extsn); |
|
| 107 |
+ |
|
| 108 |
+ |
|
| 109 |
+ FileUtils.copyFile(copy1, copy2); |
|
| 110 |
+ File imgFiles = new File(imgDir + "." +extsn); |
|
| 111 |
+ Image img = ImageIO.read(imgFiles); |
|
| 112 |
+ |
|
| 113 |
+ PDPage page = new PDPage(PDRectangle.A4); |
|
| 114 |
+ doc.addPage(page); |
|
| 115 |
+ |
|
| 116 |
+ PDImageXObject pdImage = PDImageXObject.createFromFile(imgFiles.toString(), doc); |
|
| 117 |
+ int pageWidth = Math.round(page.getCropBox().getWidth()); |
|
| 118 |
+ int pageHeight = Math.round(page.getCropBox().getHeight()); |
|
| 119 |
+ |
|
| 120 |
+ float imgRatio = 1; |
|
| 121 |
+ if ( pageWidth < img.getWidth(null)) {
|
|
| 122 |
+ imgRatio = (float)img.getWidth(null) / (float)pageWidth; |
|
| 123 |
+ } |
|
| 124 |
+ |
|
| 125 |
+ int imgWidth = Math.round(img.getWidth(null) / imgRatio); |
|
| 126 |
+ int imgHeight = Math.round(img.getHeight(null) / imgRatio); |
|
| 127 |
+ |
|
| 128 |
+ int pageWidthPosition = (pageWidth - imgWidth) / 2; |
|
| 129 |
+ int pageHeightPosition = (pageWidth - imgWidth) / 2; |
|
| 130 |
+ |
|
| 131 |
+ PDPageContentStream contents = new PDPageContentStream(doc, page); |
|
| 132 |
+ contents.drawImage(pdImage, pageWidthPosition, pageHeightPosition, imgWidth, imgHeight); |
|
| 133 |
+ contents.close(); |
|
| 134 |
+ doc.save("/usr/local/tomcat/file/sht/pdf/" + uuid + ".pdf");
|
|
| 135 |
+ |
|
| 136 |
+ } catch (Exception e) {
|
|
| 137 |
+ System.out.println("Exception! : " + e.getMessage());
|
|
| 138 |
+ } |
|
| 139 |
+ |
|
| 140 |
+ try {
|
|
| 141 |
+ doc.close(); |
|
| 142 |
+ } catch (IOException e) {
|
|
| 143 |
+ e.printStackTrace(); |
|
| 144 |
+ } |
|
| 145 |
+ |
|
| 146 |
+ return "/usr/local/tomcat/file/sht/pdf/" + uuid + ".pdf"; |
|
| 147 |
+ } |
|
| 148 |
+ |
|
| 149 |
+ |
|
| 91 | 150 |
} |
--- src/main/java/itn/let/mjo/msg/web/MjonMsgController.java
+++ src/main/java/itn/let/mjo/msg/web/MjonMsgController.java
... | ... | @@ -1,5 +1,6 @@ |
| 1 | 1 |
package itn.let.mjo.msg.web; |
| 2 | 2 |
|
| 3 |
+import java.io.File; |
|
| 3 | 4 |
import java.io.OutputStream; |
| 4 | 5 |
import java.net.URL; |
| 5 | 6 |
import java.text.SimpleDateFormat; |
... | ... | @@ -13,7 +14,6 @@ |
| 13 | 14 |
import java.util.Locale; |
| 14 | 15 |
import java.util.Map; |
| 15 | 16 |
import java.util.Random; |
| 16 |
-import java.util.stream.Collector; |
|
| 17 | 17 |
import java.util.stream.Collectors; |
| 18 | 18 |
|
| 19 | 19 |
import javax.annotation.Resource; |
... | ... | @@ -52,6 +52,7 @@ |
| 52 | 52 |
import itn.com.cmm.service.EgovFileMngUtil; |
| 53 | 53 |
import itn.com.cmm.service.FileVO; |
| 54 | 54 |
import itn.com.cmm.util.MJUtil; |
| 55 |
+import itn.com.cmm.util.PdfUtil; |
|
| 55 | 56 |
import itn.com.cmm.util.RedirectUrlMaker; |
| 56 | 57 |
import itn.com.cmm.util.StringUtil; |
| 57 | 58 |
import itn.com.utl.fcc.service.EgovStringUtil; |
... | ... | @@ -157,6 +158,9 @@ |
| 157 | 158 |
|
| 158 | 159 |
@Resource(name = "egovMberCmpHstService") |
| 159 | 160 |
private EgovMberCmpHstService egovMberCmpHstService; |
| 161 |
+ |
|
| 162 |
+ @Resource(name = "EgovFileMngService") |
|
| 163 |
+ private EgovFileMngService fileService; |
|
| 160 | 164 |
|
| 161 | 165 |
//배열 정의{"컬럼순차번호, 컬럼이름, 컬럼내용, 컬럼이름에 붙여야할 내용(엑셀코드양식다운로드시 필요)"}
|
| 162 | 166 |
private String[][] sendMsgExcelValue ={
|
... | ... | @@ -5038,6 +5042,26 @@ |
| 5038 | 5042 |
|
| 5039 | 5043 |
return "/uss/ion/msg/weekendCsWork2"; |
| 5040 | 5044 |
} |
| 5045 |
+ |
|
| 5046 |
+ @RequestMapping(value = {"/uss/ion/msg/pdfView.do"})
|
|
| 5047 |
+ public String pdfView(FileVO fileVO, ModelMap model) throws Exception {
|
|
| 5048 |
+ |
|
| 5049 |
+ FileVO fvo = fileService.selectFileInf(fileVO); |
|
| 5050 |
+ String path = ""; |
|
| 5051 |
+ |
|
| 5052 |
+ if(fvo != null) {
|
|
| 5053 |
+ if("pdf".equals(fvo.getFileExtsn())) {
|
|
| 5054 |
+ path = "/cmm/fms/FileDown.do?atchFileId="+ fvo.getAtchFileId() + "&fileSn=" + fvo.getFileSn(); |
|
| 5055 |
+ }else {
|
|
| 5056 |
+ String storePath = fvo.getFileStreCours() + fvo.getStreFileNm(); |
|
| 5057 |
+ path = PdfUtil.makeImgPdf(storePath, fvo.getFileExtsn()); |
|
| 5058 |
+ } |
|
| 5059 |
+ } |
|
| 5060 |
+ |
|
| 5061 |
+ model.addAttribute("pdfPath", path);
|
|
| 5062 |
+ |
|
| 5063 |
+ return "/uss/ion/msg/pdfView"; |
|
| 5064 |
+ } |
|
| 5041 | 5065 |
|
| 5042 | 5066 |
|
| 5043 | 5067 |
|
--- src/main/webapp/WEB-INF/decorators.xml
+++ src/main/webapp/WEB-INF/decorators.xml
... | ... | @@ -151,6 +151,7 @@ |
| 151 | 151 |
|
| 152 | 152 |
<pattern>/uss/ion/msg/weekendCsWork.do</pattern> |
| 153 | 153 |
<pattern>/uss/ion/msg/weekendCsWork2.do</pattern> |
| 154 |
+ <pattern>/uss/ion/msg/pdfView.do</pattern> |
|
| 154 | 155 |
|
| 155 | 156 |
</decorator> |
| 156 | 157 |
|
+++ src/main/webapp/WEB-INF/jsp/uss/ion/msg/pdfView.jsp
... | ... | @@ -0,0 +1,134 @@ |
| 1 | +<%-- | |
| 2 | + Class Name : weekendCsWork.jsp | |
| 3 | + Description : 발신번호 리스트 조회 페이지 | |
| 4 | + Modification Information | |
| 5 | + | |
| 6 | + 수정일 수정자 수정내용 | |
| 7 | + ------- -------- --------------------------- | |
| 8 | + 2021.03.31 신명섭 최초 생성 | |
| 9 | + | |
| 10 | + Copyright (C) 2009 by ITN All right reserved. | |
| 11 | +--%> | |
| 12 | +<%@ page contentType="text/html; charset=utf-8"%> | |
| 13 | +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> | |
| 14 | +<%@ taglib prefix="ui" uri="http://egovframework.gov/ctl/ui"%> | |
| 15 | +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> | |
| 16 | +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> | |
| 17 | +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> | |
| 18 | +<%@ taglib prefix="ec" uri="/WEB-INF/tld/ecnet_tld.tld"%> | |
| 19 | +<% | |
| 20 | + response.setHeader("Cache-Control","no-store"); | |
| 21 | + response.setHeader("Pragma","no-cache"); | |
| 22 | + response.setDateHeader("Expires",0); | |
| 23 | + if (request.getProtocol().equals("HTTP/1.1")) response.setHeader("Cache-Control", "no-cache"); | |
| 24 | +%> | |
| 25 | +<!DOCTYPE html> | |
| 26 | +<html lang="ko"> | |
| 27 | + | |
| 28 | +<head> | |
| 29 | + <script type="text/javascript" src="<c:url value='/js/EgovMultiFile.js'/>"></script> | |
| 30 | + <script src="//mozilla.github.io/pdf.js/build/pdf.js"></script> | |
| 31 | +</head> | |
| 32 | +<body> | |
| 33 | + | |
| 34 | +<div> | |
| 35 | + <button id="prev">Previous</button> | |
| 36 | + <button id="next">Next</button> | |
| 37 | + | |
| 38 | + <span>Page: <span id="page_num"></span> / <span id="page_count"></span></span> | |
| 39 | +</div> | |
| 40 | +<canvas id="the-canvas" name="the-canvas"></canvas> | |
| 41 | +</body> | |
| 42 | +</html> | |
| 43 | + <script type="text/javaScript" language="javascript"> | |
| 44 | + var pdfDoc = null; | |
| 45 | + var pageNum = 1; | |
| 46 | + var pageRendering = false; | |
| 47 | + var pageNumPending = null; | |
| 48 | + var scale = 0.8; | |
| 49 | + var canvas = document.getElementById('the-canvas'); | |
| 50 | + var ctx = canvas.getContext('2d'); | |
| 51 | + /* var url = '/cmm/fms/FileDown.do?atchFileId=FILE_000000000019061&fileSn=0'; */ | |
| 52 | +// var url = '/usr/local/tomcat/file/sht/pdf/2ccbb16e-62df-48c0-bbb1-3b6559bd4c36.pdf'; | |
| 53 | + var url = '<c:url value="${pdfPath}"/>'; | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Get page info from document, resize canvas accordingly, and render page. | |
| 57 | + * @param num Page number. | |
| 58 | + */ | |
| 59 | + function renderPage(num) { | |
| 60 | + pageRendering = true; | |
| 61 | + // Using promise to fetch the page | |
| 62 | + pdfDoc.getPage(num).then(function(page) { | |
| 63 | + var viewport = page.getViewport({scale: scale}); | |
| 64 | + canvas.height = viewport.height; | |
| 65 | + canvas.width = viewport.width; | |
| 66 | + | |
| 67 | + // Render PDF page into canvas context | |
| 68 | + var renderContext = { | |
| 69 | + canvasContext: ctx, | |
| 70 | + viewport: viewport | |
| 71 | + }; | |
| 72 | + var renderTask = page.render(renderContext); | |
| 73 | + | |
| 74 | + // Wait for rendering to finish | |
| 75 | + renderTask.promise.then(function() { | |
| 76 | + pageRendering = false; | |
| 77 | + if (pageNumPending !== null) { | |
| 78 | + // New page rendering is pending | |
| 79 | + renderPage(pageNumPending); | |
| 80 | + pageNumPending = null; | |
| 81 | + } | |
| 82 | + }); | |
| 83 | + }); | |
| 84 | + | |
| 85 | + // Update page counters | |
| 86 | + document.getElementById('page_num').textContent = num; | |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * If another page rendering in progress, waits until the rendering is | |
| 91 | + * finised. Otherwise, executes rendering immediately. | |
| 92 | + */ | |
| 93 | + function queueRenderPage(num) { | |
| 94 | + if (pageRendering) { | |
| 95 | + pageNumPending = num; | |
| 96 | + } else { | |
| 97 | + renderPage(num); | |
| 98 | + } | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * Displays previous page. | |
| 103 | + */ | |
| 104 | + function onPrevPage() { | |
| 105 | + if (pageNum <= 1) { | |
| 106 | + return; | |
| 107 | + } | |
| 108 | + pageNum--; | |
| 109 | + queueRenderPage(pageNum); | |
| 110 | + } | |
| 111 | + document.getElementById('prev').addEventListener('click', onPrevPage); | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Displays next page. | |
| 115 | + */ | |
| 116 | + function onNextPage() { | |
| 117 | + if (pageNum >= pdfDoc.numPages) { | |
| 118 | + return; | |
| 119 | + } | |
| 120 | + pageNum++; | |
| 121 | + queueRenderPage(pageNum); | |
| 122 | + } | |
| 123 | + document.getElementById('next').addEventListener('click', onNextPage); | |
| 124 | + /** | |
| 125 | + * Asynchronously downloads PDF. | |
| 126 | + */ | |
| 127 | + pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) { | |
| 128 | + pdfDoc = pdfDoc_; | |
| 129 | + document.getElementById('page_count').textContent = pdfDoc.numPages; | |
| 130 | + | |
| 131 | + // Initial/first page rendering | |
| 132 | + renderPage(pageNum); | |
| 133 | + }); | |
| 134 | + </script>(No newline at end of file) |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?