iText에서 html을 pdf로 변환

4742 단어 itext
1 다음과 같은jar 패키지를 프로젝트로 가져옵니다.
core-renderer.jar
core-renderer-minimal.jar
iText-2.0.8.jar
iTextAsian.jar
xml-apis-xerces-2.9.1.jar
2 Code:

// html        
public String reportPdf(MessageResources resources,
			Locale locale, HttpSession session, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		response.reset();
		response.setContentType("application/pdf");
		response.setHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode("itextConvertHtml","UTF-8")+".pdf");
		OutputStream os = response.getOutputStream();
		ITextRenderer renderer = new ITextRenderer();
		ITextFontResolver fontResolver = renderer.getFontResolver();
//  window
fontResolver.addFont("C:/Windows/fonts/simsun.ttc",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //      
//Linux     simsun.ttc(         ),window      c:/windows/font/simsun.ttc
//fontResolver.addFont(this.getServlet().getServletContext().getRealPath("/")+"/images/simsun.ttc",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
		StringBuffer html = new StringBuffer();
		// DOCTYPE                      
		html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
		html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
		html.append("<head>");
		html.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
		html.append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>");
		html.append("</head>").append("<body>");
		html.append("<img src='itextConvertHtml1.jpg'/><img src='itextConvertHtml2.jpg'/>");
		html.append("</body></html>");
		try{
			renderer.setDocumentFromString(html.toString());
			//            (    images/upload   itextConvertHtml1.jpg itextConvertHtml1.jpg)
			renderer.getSharedContext().setBaseURL("file:/"+this.getServlet().getServletContext().getRealPath("/")+"images/upload/");
			renderer.layout();
			renderer.createPDF(os);
			os.flush();   
			os.close();
		}catch(Exception ex){}
		return null;
}

// html          
public String outPutPdf(MessageResources resources,
			Locale locale, HttpSession session, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String outputFile = "E:/itextConvertHtml.pdf";
		OutputStream os = new FileOutputStream(outputFile);
		ITextRenderer renderer = new ITextRenderer();
		ITextFontResolver fontResolver = renderer.getFontResolver();
		fontResolver.addFont("C:/Windows/fonts/simsun.ttc",
				BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //      
		StringBuffer html = new StringBuffer();
		// DOCTYPE                      
		html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
		html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
		html.append("<head>");
		html.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
		html.append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>");
		html.append("</head>").append("<body>");
		html.append("<img src='itextConvertHtml1.jpg'/><img src='itextConvertHtml2.jpg'/>");
		html.append("</body></html>");
		try{
			renderer.setDocumentFromString(html.toString());
			//            (    images/upload   itextConvertHtml1.jpg itextConvertHtml1.jpg)
			renderer.getSharedContext().setBaseURL("file:/"+this.getServlet().getServletContext().getRealPath("/")+"images/upload/");
			renderer.layout();
			renderer.createPDF(os);
			os.flush();   
			os.close();
		}catch(Exception ex){}
		return null;
}

3itext를 사용하여 html을 pdf로 전환할 때 html의 라벨은 반드시 시작-끝이어야 한다. 또한itext 중 일부는 html을 지원하지 않는 라벨이 있다.

좋은 웹페이지 즐겨찾기