반응형

 

 

📝 Controller

package com.lsj.spring_study;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * Handles requests for the application home page.
 */
@Controller
public class PdfController {
	
	/**
	 * @why : 자바로 pdf 파일 생성
	 */
	@RequestMapping(value = "pdf", method = RequestMethod.PUT)
	public void makePdfFile() throws DocumentException, IOException {
		

		String filePath = "C://test.pdf";
		String contents = "자바로 pdf 파일 생성";
		
	    Document document = new Document();
	    PdfWriter.getInstance(document, new FileOutputStream(filePath));

	    document.open();
	    
	    /** 표 그리기 **/ 
//	     PdfPTable table = new PdfPTable(4); 
//	     for(int i = 0; i < 16; i++){
//	      	table.addCell("cellNumber:" + i); 
//	    }

	    /** 글자 넣기 (WEB-INF/classes/font에 폰트를 넣어주세요 (참고 : 한글이 안 나오는 글씨체도 있음) **/ 
	    // C:\Windows\Fonts\휴먼매직체 보통(HMKMMAG.TFF 파일 사용)
	    BaseFont bfKorean = BaseFont.createFont("font/HMKMMAG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 
	    
	    Font FontKorean = new Font(bfKorean, 10, Font.NORMAL);
	    document.add(new Paragraph(contents, FontKorean));
	    document.close();
		
	}
	
	/**
	 * @why : pdf 파일 다운로드
	 */
    @RequestMapping(value = "/download/pdf", method = RequestMethod.GET)
    public void downloadPdf(HttpServletResponse response) throws IOException {
    	
    	FileInputStream fis = null;
    	BufferedOutputStream bos = null;
    	
    	String pdfFileName = "C:/test.pdf";
    	
    	try {

    		/** Pdf Open **/
	    	File pdfFile = new File(pdfFileName);
	    	fis = new FileInputStream(pdfFile);

	    	/** Header 설정 (Download) **/
	    	response.addHeader("Content-Disposition", "attachment; filename=" + pdfFile.getName() + ".pdf");
	    	
	    	/** Pdf 내용을 Read [버퍼에 담기] **/
	    	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 {

	    	if (fis != null) fis.close();
	    	if (bos != null) bos.close();

    	}
    }


	/**
	 * @why : pdf 파일 웹에 띄우기
	 */
    @RequestMapping(value = "pdf", method = RequestMethod.GET)
    public void readPdf(HttpServletResponse response) throws IOException {
    	
    	FileInputStream fis = null;
    	BufferedOutputStream bos = null;
    	
    	String pdfFileName = "C:/test.pdf";
    	
    	try {

    		/** Pdf Open **/
	    	File pdfFile = new File(pdfFileName);
	    	fis = new FileInputStream(pdfFile);

	    	/** Content Type 설정 **/
	    	response.setContentType("application/pdf");

	    	/** Pdf 내용을 Read [버퍼에 담기] **/
	    	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 {

	    	if (fis != null) fis.close();
	    	if (bos != null) bos.close();

    	}
    }
    
    
	/**
	 * @why : pdf 파일 웹에 띄우기
	 */
    @RequestMapping(value = "board", method = RequestMethod.GET)
    public String pdf() {
    	
    	return "board";
    }
}

 

 

📝 Maven

<!-- make pdf lib -->
<dependencies>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.itextpdf.tool</groupId>
        <artifactId>xmlworker</artifactId>
        <version>5.5.7</version>
</dependency>

 

📝 View

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>

<body>
<h1> 웹화면에 PDF 출력해보기 </h1>
<embed src="/pdf" type="application/pdf" width=1500px height=1500px/>
</body>
</html>

 

 

📝 출력 화면

 

🔗 참고 및 출처

https://blog.naver.com/onandme/220529022550

https://lee-mandu.tistory.com/87

반응형