☆ 전체소스 : spring304.zip
■ iText API를 이용해서 PDF문서를 생성할 수 있는 AbstractPdfView 클래스를 제공
● 별도의 LIB가 필요 (itext-2.1.7.jar)
FileDownloadController.java
package sp.mvc.controller;
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView;
@Controller public class FileDownloadController {
@RequestMapping(value="/filedown.sp") public ModelAndView fileDownload(){ System.out.println("----- FileDownloadController.fileDownload() -----"); ModelAndView modelAndView = new ModelAndView("filedownload"); return modelAndView; }
@RequestMapping("/exceldown.sp") public String excelDownload(Model model){ System.out.println("----- FileDownloadController.excelDownload() -----"); List<String> list = new ArrayList<String>(); list.add("사이트 관리"); list.add("관리자 관리"); list.add("공통 코드 관리"); list.add("접속 이력"); model.addAttribute("menuList", list); return "excelDownload"; } @RequestMapping("/pdfdown.sp") public String pdfDownload(Model model){ System.out.println("----- FileDownloadController.pdfDownload() -----"); Map<String, String> map = new HashMap<String, String>(); map.put("1", "cat"); map.put("2", "dog"); map.put("3", "tiger"); model.addAttribute("animal", map); return "pdfDownload"; } }
|
PdfDownloadView.java
package sp.mvc.view;
import java.net.URLEncoder; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.view.document.AbstractPdfView;
import com.lowagie.text.Cell; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.Paragraph; import com.lowagie.text.Table; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter;
public class PdfDownloadView extends AbstractPdfView {
@Override protected void buildPdfDocument(Map<String, Object> model, Document doc, PdfWriter writer, HttpServletRequest req, HttpServletResponse res) throws Exception { // TODO Auto-generated method stub System.out.println("---- PdfDownloadView.buildPdfDocument() ----"); String userAgent = req.getHeader("User-Agent"); String fileName = "test.pdf"; if(userAgent.indexOf("MSIE") > -1){ fileName = URLEncoder.encode(fileName, "utf-8"); }else{ fileName = new String(fileName.getBytes("utf-8"), "iso-8859-1"); } res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\";"); res.setHeader("Content-Transfer-Encoding", "binary"); Map<String, String> map = (HashMap<String, String>)model.get("animal"); Table table = new Table(2, map.size()+1); table.setPadding(5); BaseFont baseFont = BaseFont.createFont("c:\\windows\\fonts\\batang.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font font = new Font(baseFont); Cell cell = new Cell(new Paragraph("order", font)); cell.setHeader(true); table.addCell(cell); cell = new Cell(new Paragraph("page", font)); cell.setHeader(true); table.addCell(cell); table.endHeaders(); Set<String> set = map.keySet(); Iterator<String> it = set.iterator(); while(it.hasNext()){ String key = it.next(); System.out.println("key : " + key); System.out.println("value : " + map.get(key)); table.addCell(key); table.addCell(map.get(key)); } doc.add(table); } } |
main_config.xml
결과
[참고자료] Spring 3.0 프로그래밍-최범균