private void pd4mlToPdf(String htmlCode, HttpServletResponse response,
String str) {
try {
String date = StringUtil.getstrFdate(new Date(), "yyyy-MM-dd");
String title = str + date;
OutputStream out = response.getOutputStream();
response.setHeader("Content-disposition", "attachment; filename="
+ new String(title.getBytes("gbk"), "iso8859-1") + ".pdf");
response.setContentType("application/download");
StringBuffer html = new StringBuffer();
// 組裝成符合W3C標准的html文件
html.append(""-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
html.append("")
.append("
").append("")
.append("")
.append("")
.append("").append("
").append(htmlCode);
html.append("");
org.jsoup.nodes.Document doc = Jsoup.parse(html.toString());
Element el = doc.getElementById("ordertitle");
el.removeAttr("style");
el.attr("style", " FONT-SIZE: 33px");
// System.out.println(doc.toString());
StringReader sr = new StringReader(doc.toString());
PD4ML pd4ml = new PD4ML();
pd4ml.setPageInsets(new Insets(20, 10, 10, 10));
// pd4ml.setHtmlWidth(950);
// pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
pd4ml.setPageSize(PD4Constants.A4);
pd4ml.useTTF("java:com/wansin/contract/fonts", true);
// pd4ml.setDefaultTTFs("KaiTi_GB2312", "KaiTi_GB2312", "KaiTi_GB2312");
pd4ml.enableDebugInfo();
pd4ml.render(sr, out);
out.flush();
out.close();
} catch (ExceptionConverter e1) {
System.out.println("ie 關閉連接");
} catch (Exception e) {
e.printStackTrace();
}
}
用到的相關jar包 pd4ml_demo.jar ss_css2.jar jsoup-1.6.1.jar
中文字體問題 需要字體文件 SONGTI.TTF 和配置文件 pd4fonts.properties
文件內容為
KaiTi_GB2312=SIMKAI.TTF
SongTi_GB2312=SONGTI.TTF
配置文件的讀取 跟.append("")這個有關 所以要想引用字體 pd4ml.useTTF("java:com/wansin/contract/fonts", true); 就得在html中加入字體配置 com/wansin/contract/fonts為字體文件及配置文件的包名
相關jar包可以到