首页 > 代码库 > Java-No.07 SpringMVC下HTML内容导出PDF并下载到本地
Java-No.07 SpringMVC下HTML内容导出PDF并下载到本地
1、通过iText生成pdf文件(包含中文)参照:http://my.oschina.net/sanji/blog/277704
1)下载jar包:
CORE 包:http://sourceforge.net/projects/itext/files/
XML 包:http://sourceforge.net/projects/xmlworker/files/
2)前端页面
<input type="button" style="position:absolute;top:55px;right:20px;background:url(resources/images/20150203052448221_easyicon_net_48.png) no-repeat;width:48px;height:48px;cursor:pointer;border:0px;" onclick="download(‘ + id + ‘)"/>
function download(id) { window.open("service.do?method=download&id=" + id); }
3)后台代码
@RequestMapping(params = "method=download") public void download(@RequestParam("id") Integer id, HttpServletRequest req, HttpServletResponse response) { ServiceInfoEntity serviceItem = configCategoryService.getServiceItem(id); String newsContent = "<div id=‘pano_camera‘ class=‘servie_info1‘><h3>" + serviceItem.getNewsTitle() + "</h3>" + serviceItem.getNewsContent() + "</div>"; newsContent = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" + newsContent; // newsContent = newsContent.replaceAll("\240$", ""); // newsContent = "<html><body>123</body></html>"; String path = req.getRealPath("/") + "resources"+ File.separator + "pdf"; String filename = serviceItem.getNewsTitle(); FileOutputStream fos; try { fos = new FileOutputStream(path + File.separator + filename + ".html"); OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); osw.write(newsContent); osw.flush(); fos.close(); osw.close(); ParseHtmlAsian.createPdf(path, filename); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } File file = new File(path + File.separator + filename + ".pdf"); // try { // // PDFReport.exportPdfFile(newsContent, req.getRealPath("/"), serviceItem.getNewsTitle()); // } catch (IOException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } byte[] buf = new byte[1024 * 1024 * 10]; int len = 0; ServletOutputStream ut = null; BufferedInputStream br = null; response.reset(); response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "must-revalidate, no-transform"); response.setDateHeader("Expires", 0L); String userAgent = req.getHeader("User-Agent"); boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1); String displayFilename = file.getName(); response.setContentType("application/x-download"); if (isIE) { try { displayFilename = URLEncoder.encode(displayFilename, "UTF-8"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\""); } else { try { displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1"); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename); } try { br = new BufferedInputStream(new FileInputStream(file)); ut = response.getOutputStream(); while ((len = br.read(buf)) != -1) ut.write(buf, 0, len); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
package com.superscene.vhscene.base; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.Charset; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerHelper; public class ParseHtmlAsian { /** * Creates a PDF with the words "Hello World" * @param file * @throws IOException * @throws DocumentException */ public static void createPdf(String path, String fileName) throws IOException, DocumentException { // step 1 Document document = new Document(); // step 2 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(new File(path + File.separator + fileName + ".pdf"))); // step 3 document.open(); // step 4 XMLWorkerHelper.getInstance().parseXHtml(writer, document, new FileInputStream(path + File.separator + fileName + ".html"), Charset.forName("UTF-8")); // step 5 document.close(); } }
Java-No.07 SpringMVC下HTML内容导出PDF并下载到本地
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。