JavaToPdfHtmlFreeMarkerCss
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.springframework.core.io.ClassPathResource;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;
import freemarker.template.Configuration;
import freemarker.template.Template;
import javax.servlet.http.HttpServletResponse;
public class JavaToPdfHtmlFreeMarkerCss {
// 本地测试PDF导出地址
private static final String DEST = "D:/pdf/templateFCss.pdf";
// 模板所在目录
private static final ClassPathResource FTL = new ClassPathResource("templates/");
// 模板文件
private static final String HTML = "templateFCss.html";
// 字体文件
private static final ClassPathResource FONT = new ClassPathResource("templates/simhei.ttf");
private static final ClassPathResource FONT_C = new ClassPathResource("templates/calibri.ttf");
private static final ClassPathResource FONT_S = new ClassPathResource("templates/simsun.ttc");
// 图片文件
private static final ClassPathResource LOGO_PATH = new ClassPathResource("templates/logo.png");
private static Configuration freemarkerCfg = null;
static {
freemarkerCfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
//freemarker的模板目录
try {
freemarkerCfg.setDirectoryForTemplateLoading(new File(FTL.getURI().getPath()));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 本地测试
* @param args
* @throws IOException
* @throws DocumentException
* @throws com.lowagie.text.DocumentException
*/
public static void main(String[] args) throws IOException, DocumentException, com.lowagie.text.DocumentException {
Map data = new HashMap();
data.put("name", "路奇.D.艾尼路 儸傑");
File file = new File(LOGO_PATH.getURL().getPath());
data.put("fileType", "image/jpeg");
data.put("file64Str", fileToBase64Str(file));
String content = freeMarkerRender(data, HTML);
System.out.println(content);
createPdf2(content, DEST);
}
/**
* freemarker渲染html
*/
public static String freeMarkerRender(Map data, String htmlTmp) {
Writer out = new StringWriter();
try {
// 获取模板,并设置编码方式
Template template = freemarkerCfg.getTemplate(htmlTmp);
template.setEncoding("UTF-8");
// 合并数据模型与模板
template.process(data, out); //将合并后的数据和模板写入到流中,这里使用的字符流
out.flush();
return out.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
/**
* 本地测试用
* @param content
* @param dest
* @throws IOException
* @throws DocumentException
* @throws com.lowagie.text.DocumentException
*/
public static void createPdf2(String content, String dest) throws IOException, DocumentException, com.lowagie.text.DocumentException {
ITextRenderer render = new ITextRenderer();
//设置字体
ITextFontResolver fontResolver = render.getFontResolver();
fontResolver.addFont(FONT_S.getURL().getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
fontResolver.addFont(FONT.getURL().getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
fontResolver.addFont(FONT_C.getURL().getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// 解析html生成pdf
render.setDocumentFromString(content);
render.layout();
render.createPDF(new FileOutputStream(dest));
render.finishPDF();
}
/**
* 接口调用,网页端下载
* @param content
* @param response
* @throws IOException
* @throws DocumentException
* @throws com.lowagie.text.DocumentException
*/
public static void createPdf(String content, HttpServletResponse response) throws IOException, DocumentException, com.lowagie.text.DocumentException {
ITextRenderer render = new ITextRenderer();
//设置字体
ITextFontResolver fontResolver = render.getFontResolver();
fontResolver.addFont(FONT_S.getURL().getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
fontResolver.addFont(FONT.getURL().getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
fontResolver.addFont(FONT_C.getURL().getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// 解析html生成pdf
render.setDocumentFromString(content);
render.layout();
render.createPDF(response.getOutputStream());
render.finishPDF();
}
/**
* File to 64bit Str
*
* @param file
* @return
*/
public static String fileToBase64Str(File file) {
byte[] data = null;
InputStream inputStream = null;
if (file != null) {
try {
inputStream = new FileInputStream(file);
data = new byte[inputStream.available()];
inputStream.read(data);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return Base64.encodeBase64String(data);
}
return null;
}
/**
* PDF导出的通用接口
*
* @param data
* @param templateName
* @param response
*/
public static void exportPdf(Map data, String templateName, String pdfName, HttpServletResponse response) throws UnsupportedEncodingException {
String content = freeMarkerRender(data, templateName);
response.setContentType("application/pdf");
// 文件名设置支持中文
response.setHeader("Content-Disposition", "attachment; filename=" URLEncoder.encode(pdfName, "UTF-8") ".pdf");
try {
createPdf(content, response);
} catch (IOException e) {
throw new RuntimeException(e);
} catch (DocumentException e) {
throw new RuntimeException(e);
} catch (com.lowagie.text.DocumentException e) {
throw new RuntimeException(e);
}
}
}