package itn.com.cmm.util; import java.awt.Image; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.UUID; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; 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.graphics.image.PDImageXObject; /** * * @author : 이호영 * @fileName : PdfUtil.java * @date : 2023.03.16 * @description : PDF를 다루는 Utils * =========================================================== * DATE AUTHOR NOTE * ----------------------------------------------------------- * * 2023.04.06 이호영 최초 생성 * * * */ public final class PdfUtil { /** * 휴대폰번호 대시('-') 추가 * 대시 유무 상관없음 * 유효성 맞지 않을시 변환안됨. * @param response */ public static void showPdf(HttpServletResponse response, String pdfFileName) { FileInputStream fis = null; BufferedOutputStream bos = null; try { File pdfFile = new File(pdfFileName); // 클라이언트 브라우져에서 바로 보는 방법(헤더 변경) response.setContentType("application/pdf"); // ★ 이 구문이 있으면 [다운로드], 이 구문이 없다면 바로 target 지정된 곳에 view 해줍니다. // response.addHeader("Content-Disposition", "attachment; filename=" + pdfFile.getName() + ".pdf"); // 파일 읽고 쓰는 건 일반적인 Write방식이랑 동일합니다. 다만 reponse 출력 스트림 객체에 write. fis = new FileInputStream(pdfFile); int size = fis.available(); // 지정 파일에서 읽을 수 있는 바이트 수를 반환 byte[] buf = new byte[size]; // 버퍼설정 int readCount = fis.read(buf); response.flushBuffer(); bos = new BufferedOutputStream(response.getOutputStream()); bos.write(buf, 0, readCount); bos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fis != null) fis.close(); // close는 꼭! 반드시! if (bos != null) bos.close(); } catch (IOException e) { e.printStackTrace(); } } } public static String makeImgPdf(String imgDir,String extsn) throws Exception { PDDocument doc = new PDDocument(); String uuid = UUID.randomUUID().toString(); try { File copy1 = new File(imgDir); File copy2 = new File(imgDir + "." +extsn); FileUtils.copyFile(copy1, copy2); File imgFiles = new File(imgDir + "." +extsn); Image img = ImageIO.read(imgFiles); PDPage page = new PDPage(PDRectangle.A4); doc.addPage(page); PDImageXObject pdImage = PDImageXObject.createFromFile(imgFiles.toString(), doc); int pageWidth = Math.round(page.getCropBox().getWidth()); int pageHeight = Math.round(page.getCropBox().getHeight()); float imgRatio = 1; if ( pageWidth < img.getWidth(null)) { imgRatio = (float)img.getWidth(null) / (float)pageWidth; } int imgWidth = Math.round(img.getWidth(null) / imgRatio); int imgHeight = Math.round(img.getHeight(null) / imgRatio); int pageWidthPosition = (pageWidth - imgWidth) / 2; int pageHeightPosition = (pageWidth - imgWidth) / 2; PDPageContentStream contents = new PDPageContentStream(doc, page); contents.drawImage(pdImage, pageWidthPosition, pageHeightPosition, imgWidth, imgHeight); contents.close(); doc.save("/usr/local/tomcat/file/sht/pdf/" + uuid + ".pdf"); } catch (Exception e) { System.out.println("Exception! : " + e.getMessage()); } try { doc.close(); } catch (IOException e) { e.printStackTrace(); } return uuid + ".pdf"; } }