利用Flying Saucer 和 java iText 实现HTMl转PDF
2013-11-05 11:22
375 查看
一直对java iText 感兴趣,今天终于找到了一篇好文章,原文地址: http://blog.lichengwu.cn/java/2010/10/27/using-flying-saucer-and-itext-convert-html-to-pdf/
文章内容如下:
Flying Saucer(或者叫xhtmlrender project on java.net)是一个基于iText的开源java库,能够轻松的将html(带css2.1)生成pdf。
iText是一个生成PDF文档的开源java库,能够动态从XML或者数据库生成PDF,同时它具备PDF文档的绝大多数属性(比如加密……),支持java,C#等。
下面我们首先创建一个简单的带css的html,代码如下:
接下来生成一个pdf
结果如下:
下面的我们先用StringBuilder生成一个HTML的字符串,然后用DOM解析,生成PDF。
效果如下:
下面展示在servlet里面创建pdf
原文地址:http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html
文章内容如下:
Flying Saucer(或者叫xhtmlrender project on java.net)是一个基于iText的开源java库,能够轻松的将html(带css2.1)生成pdf。
iText是一个生成PDF文档的开源java库,能够动态从XML或者数据库生成PDF,同时它具备PDF文档的绝大多数属性(比如加密……),支持java,C#等。
生成简单的PDF
下面我们首先创建一个简单的带css的html,代码如下:[code]<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>My First Document</title> <style type="text/css"> b { color: green; } </style> </head> <body> <p> <b>Greetings Earthlings!</b> We've come for your Java. </p> </body> </html>
接下来生成一个pdf
[code]public class FirstDoc { public static void main(String[] args) throws DocumentException, IOException { String path = System.getProperty("user.dir") + "/src/"; String inputFile = path + "samples/firstdoc.html"; String url = new File(inputFile).toURI().toURL().toString(); String outputFile = path + "outputs/fistdoc.pdf"; OutputStream os = new FileOutputStream(outputFile); ITextRenderer render = new ITextRenderer(); render.setDocument(url); render.layout(); render.createPDF(os); os.close(); } }
结果如下:
用Fly生成内容
下面的我们先用StringBuilder生成一个HTML的字符串,然后用DOM解析,生成PDF。[code]public class OneHundredBottles{ public static void main(String[] args) throws Exception { String path =System.getProperty("user.dir")+"/src/"; StringBuffer buf = new StringBuffer(); buf.append("<html>"); // put in some style buf.append("<head><style language='text/css'>"); buf.append("h3 { border: 1px solid #aaaaff; background: #ccccff; "); buf.append("padding: 1em; text-transform: capitalize; font-family: sansserif; font-weight: normal;}"); buf.append("p { margin: 1em 1em 4em 3em; } p:first-letter { color: red; font-size: 150%; }"); buf.append("h2 { background: #5555ff; color: white; border: 10px solid black; padding: 3em; font-size: 200%; }"); buf.append("</style></head>"); // generate the body buf.append("<body>"); buf.append("<p><img src='"+path+"samples/100bottles.jpg'/></p>"); for(int i=99; i>0; i--) { buf.append("<h3>"+i+" bottles of beer on the wall, " + i + " bottles of beer!</h3>"); buf.append("<p>Take one down and pass it around, " + (i-1) + " bottles of beer on the wall</p>/n"); } buf.append("<h2>No more bottles of beer on the wall, no more bottles of beer. "); buf.append("Go to the store and buy some more, 99 bottles of beer on the wall.</h2>"); buf.append("</body>"); buf.append("</html>"); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(new StringBufferInputStream(buf.toString())); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(doc, null); String outputFile = path+"outputs/100bottles.pdf"; OutputStream os = new FileOutputStream(outputFile); renderer.layout(); renderer.createPDF(os); os.close(); } }
效果如下:
在服务端创建PDF
下面展示在servlet里面创建pdf [code]public class PDFServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/pdf"); StringBuffer buf = newStringBuffer(); buf.append("<html>"); String css = getServletContext().getRealPath("/PDFservlet.css"); // put in some style buf.append("<head><link rel='stylesheet' type='text/css' "+"href='"+css+"' media='print'/></head>"); ... //generate the rest of the HTML // parse our markup into an xml Document try{ DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(new StringBufferInputStream(buf.toString())); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(doc, null); renderer.layout(); OutputStream os = response.getOutputStream(); renderer.createPDF(os); os.close(); } catch(Exception ex) { ex.printStackTrace(); } } }
原文地址:http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html
相关文章推荐
- 利用Flying Saucer 和 iText 实现HTMl转PDF(java)
- java中利用iText实现导入数据到pdf模板中
- java利用wkhtmltopdf实现html转化pdf
- 【JAVA】使用 iText XMLWorker实现HTML转PDF
- 利用Flying Saucer 和 iText 实现生成PDF报表
- iText的使用例子说明(利用Itext画模板导出word、pdf,纯java实现)
- Java使用Flying Saucer实现HTML代码生成PDF文档
- Flying Saucer实现html转pdf(一些问题,持续更新)
- java实现爬虫技术,读取txt,word,excel,ppt,pdf,html等格式的文件
- 使用html2canvas截屏,利用itextpdf转为pdf保存到数据库
- JAVA实现HTML文档转PDF格式
- java利用itext编辑pdf
- Java实现HTML转换为PDF的常见方法
- java 实现word 转PDF (采用第三方技术 IText、Poi、Jsoup)
- 使用 itext、flying-saucer 实现html转PDF(转)
- java利用itext编辑pdf
- Java 利用itext填写pdf模板并导出(亲自试过,没问题,并附上itextpdf-5.5.8.jar包和itext-asian.jar包))
- 使用 itext、flying-saucer 实现html转PDF
- java利用joup,imageio,base64来实现简历下载为html格式。
- iText 实现 html 转换 pdf