Javaweb中运用 pd4ml 导出pdf 及jsoup操作html

来源:互联网 发布:windows安装mac os x 编辑:程序博客网 时间:2024/06/14 00:45


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("<!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\">").append("<head>").append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />").append("<style type=\"text/css\" mce_bogus=\"1\">*{font-family: SongTi_GB2312;}</style>").append("<style type=\"text/css\">img {width: 700px;}</style>").append("</head>").append("<body>").append(htmlCode);html.append("</body></html>");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("<style type=\"text/css\" mce_bogus=\"1\">*{font-family: SongTi_GB2312;}</style>")这个有关 所以要想引用字体  pd4ml.useTTF("java:com/wansin/contract/fonts", true); 就得在html中加入字体配置 com/wansin/contract/fonts为字体文件及配置文件的包名

相关jar包可以到

1 0
原创粉丝点击