html页面转换成PDF文件
2016-01-18 14:30
555 查看
package pdftest; import java.io.BufferedReader; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import org.xhtmlrenderer.pdf.ITextFontResolver; import org.xhtmlrenderer.pdf.ITextRenderer; import com.lowagie.text.pdf.BaseFont; /** * 将html页面转换成PDF文件 * 应用包 itext-2.0.8.jar core-renderer-R8.jar * 不支持中文,若支持中文需对内容进行改动 * */ public class RendererTest { public static void main(String[] args) { // TODO Auto-generated method stub try{ ITextRenderer renderer = new ITextRenderer(); // String blogURL = "http://; String blogURL = "http://"; //指定模板地址 // renderer.setDocument("http://"); //根据网址,获取网页内容 String htmlBody = getHtmlContent(blogURL); //将网页内容进行格式校验及调整,以便于renderer能够解析,根据内容调整 htmlBody = htmlBody.replaceAll("<br>", "<br/>"); htmlBody = "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"zh-CN\"><head>"+htmlBody.substring(htmlBody.indexOf("-->")+3); htmlBody = htmlBody.substring(0, htmlBody.indexOf("</html>")+7); htmlBody = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" + htmlBody; System.out.println(htmlBody); //将调整完内容设置到renderer中 renderer.setDocumentFromString(htmlBody); //字符处理,本处需要处理中文 ITextFontResolver fontResolver = renderer.getFontResolver(); // if (StringUtils.isOSWindow()) // fontResolver.addFont("C:/Windows/Fonts/ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); // else fontResolver.addFont("/leo-work/workspace/mytest/lib/msyh.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); renderer.layout(); String pdfFile2 = "/Users/leo/Desktop/a6.pdf"; // OutputStream os = response.getOutputStream(); //输出到网页 OutputStream os = new FileOutputStream(pdfFile2); //输出到文件 //根据renderer内容,创建PDF文件 renderer.createPDF(os); //关闭输出柳 os.close(); }catch(Exception e){ System.out.println(e.getMessage()); } } /** * 通过网址获取网页内容 * @param htmlurl * @return */ public static String getHtmlContent(String htmlurl) { URL url; String temp; StringBuffer sb = new StringBuffer(); try { url = new URL(htmlurl); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));// 读取网页全部内容 while ((temp = in.readLine()) != null) { sb.append(temp); } in.close(); } catch (final MalformedURLException me) { System.out.println("你输入的URL格式有问题!"); me.getMessage(); } catch (final IOException e) { e.printStackTrace(); } return sb.toString(); } }
相关文章推荐
- html xml
- 中文乱码?不,是 HTML 实体编码!(转)
- TextView通过Html显示图片
- 细说HTML元素的ID和Name属性的区别
- MVC4.0 如何设置默认静态首页index.shtml
- HTML disabled属性的启用和禁止
- html注意事项
- .net中单选按钮RadioButton,RadioButtonList 以及纯Html中radio的用法实例?
- html iframe和frame
- 基本的 html 代码
- 加密html
- 怎样使用PDF Transformer+将PDF转换为HTML
- MVC HtmlHelper用法大全
- @Html.DropDownListFor默认选中项
- 中文乱码?不,是 HTML 实体编码!
- MVC 中@Html.DropDownListFor() 设置选中项 这么不好使 ? [问题点数:40分,结帖人lkf181]
- HTML学习1
- HTML基础01
- 将Visual Studio的单元测试结果生成HTML报表
- html适配Anroid手机