根据html模板以另存为方式导出pdf

  对于html模板的要求
<!--?xml version="1.0" encoding="UTF-8"?--><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<html xmlns="http://www.w3.org/1999/xhtml ">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
上两个必须要有。
<p name="_sampleCode"></p>, map.put("p[name=_sampleCode]", sample.getSamplecode());
这样就可以了。 切记,参数不能用id来找,id找不到。


    用到的jsoup,itextpdf 。对html的样式保存完好,如果要添加富文本内容的话,可以尝试<pre></pre>包裹下数据再传进来。


package com.*;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletResponse;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;



public class ExportHtmlToSaveAs {

/**
*导出文件名
*/
private String fileName;

/**
* 模板文件路径
*/
private String path;

/**
* 输出流
*/
private OutputStream os;

/**
* jsoup工具的html对象
*/
private Document parse;

/**
* flying-saucer工具导出对象
*/
private ITextRenderer renderer;


public ExportHtmlToSaveAs() {
super();
fileName = null;
path = null;
parse = null;
os = null;
}

/**
* 解决生成文件名中的中文不显示
* @param fileName
* @return
*/
private String modificationFileName(String fileName){
try {
fileName = new String(fileName.getBytes(), "ISO-8859-1");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
return fileName;
}

/**
* 读取html模板文件为Document对象
* @param path
* @return
*/
private Document getParse(String path){
try {
parse = Jsoup.parse(new File(path),"UTF-8","");
} catch (IOException e) {
e.printStackTrace();
}
return parse;
}

/**
* 对html模板装填,数据Map参数限定为key为String,value为String
* @param parse
* @param map
* @return
*/
private void modificationParse(Map<String,String> map){
Iterator<Entry<String, String>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Entry<String, String> entry = (Entry<String, String>) iter.next();
String value = (String) entry.getValue();
parse.select(entry.getKey()).html(null==value? "":value);
}
}

/**
* 修饰输出流,添加输出文件名,自定义下载
* @param resp
* @return
*/
private HttpServletResponse modificationResp(HttpServletResponse resp){
resp.setContentType("application/x-download;charset=utf-8");
resp.addHeader("Content-Disposition", "attachment;filename="+ fileName);
return resp;
}

/**
* 获取输出流
* @param resp
* @return
* @throws IOException
*/
private OutputStream getOs(HttpServletResponse resp) throws IOException {
resp = modificationResp(resp);
return resp.getOutputStream();
}

/**
* 修饰renderer,添加中文字体(宋体)
* @param renderer
* @throws DocumentException
* @throws IOException
*/
private void modificationRenderer() throws DocumentException, IOException{
renderer.getFontResolver().addFont("C:/WINDOWS/Fonts/SIMSUN.TTC",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
}

/**
* flying-saucer方法根据html字符串导出pdf
* @param html
* @param os
* @throws DocumentException
* @throws IOException
*/
private void exportforRenderer() throws DocumentException, IOException{
this.renderer = new ITextRenderer();
modificationRenderer();
renderer.setDocumentFromString(parse.html(),null);
renderer.layout();
renderer.createPDF(os);
}

/**
* 导出 初始化
* @param fileName
* @param path
* @param map
* @param response
* @throws IOException
*/
private void initialize(String fileName,String path,Map<String,String> map,HttpServletResponse response) throws IOException{
this.fileName = modificationFileName(fileName);
this.path = path;
this.parse = getParse(this.path);
if (null!=map) {
modificationParse(map);
}
this.os = getOs(response);
}

/**
* 导出pdf,map为null时输出模板
* @param fileName
* @param path
* @param map
* @param response
* @throws IOException
* @throws DocumentException
*/
public void HtmlExportPdf(String fileName,String path,Map<String,String> map,HttpServletResponse response) throws IOException, DocumentException{
fileName = fileName + ".pdf";
initialize(fileName,path,map,response);
exportforRenderer();
close();
}

/**
* 导出html,map为null时输出模板
* @param fileName
* @param path
* @param map
* @param response
* @throws IOException
* @throws DocumentException
*/
public void HtmlExportHtml(String fileName,String path,Map<String,String> map,HttpServletResponse response) throws IOException, DocumentException{
fileName = fileName + ".html";
initialize(fileName,path,map,response);
exportforHtml();
close();
}

/**
* 直接将html导出
* @throws IOException
*/
private void exportforHtml() throws IOException {
os.write(parse.html().getBytes());
}

/**
* 关闭输出流
* @throws IOException
*/
private void close() throws IOException {
os.flush();
os.close();
}



}
posted @ 2017-08-04 14:24  dodream巡航30+  阅读(610)  评论(0编辑  收藏  举报