package itn.com.cmm.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * @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();

			}

		}
	}
    
    
}
