http://blog.csdn.net/ol_beta/archive/2010/10/27/5968817.aspx
来源:互联网 发布:js模拟鼠标滚轮 编辑:程序博客网 时间:2024/05/07 16:45
PDF是目前为止最流行的文档格式之一。通常情况下,想要创建一个PDF文档,需要使用Adobe的API来生成,但这非常麻烦。为了能方便的将HTML(XHTML)生成PDF文档,下面介绍两个开源的java库:Flying Saucer和iText。
Flying Saucer和iText介绍:
Flying Saucer(或者叫xhtmlrender project on java.net)是一个基于iText的开源java库,能够轻松的将html(带css2.1)生成pdf。
iText是一个生成PDF文档的开源java库,能够动态从XML或者数据库生成PDF,同时它具备PDF文档的绝大多数属性(比如加密……),支持java,C#等。
生成简单的PDF
下面我们首先创建一个简单的带css的html,代码如下:
<?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>接下来生成一个pdfpackage oliver.itext.html2pdf;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
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。
package
oliver.itext.html2pdf;
import
java.io.FileOutputStream;
import
java.io.OutputStream;
import
java.io.StringBufferInputStream;
import
javax.xml.parsers.DocumentBuilder;
import
javax.xml.parsers.DocumentBuilderFactory;
import
org.w3c.dom.Document;
import
org.xhtmlrenderer.pdf.ITextRenderer;
@SuppressWarnings("deprecation
")
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
public
class
PDFServlet extends
HttpServlet {
protected
void
processRequest(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
response.setContentType("application/pdf
");
StringBuffer buf = new
StringBuffer();
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();
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ol_beta/archive/2010/10/27/5968817.aspx
- http://blog.csdn.net/ol_beta/archive/2010/10/27/5968817.aspx
- http://blog.csdn.net/Solstice/archive/2010/10/19/5950190.aspx
- http://blog.csdn.net/CYRTSOFT/archive/2004/12.aspx
- http://blog.csdn.net/hbuzhang/archive/2006/01.aspx
- http://blog.csdn.net/zhuky/archive/2009/12.aspx
- http://blog.csdn.net/mrunix/archive/2004/08.aspx
- Rose 2003在Vista安装(转:http://blog.csdn.net/qi890402/archive/2010/09/27/5910007.aspx)
- http://blog.csdn.net/arielxp/archive/2004/06/27/27929.aspx
- stL http://blog.csdn.net/solotony/archive/2004/09/27/118163.aspx
- http://blog.csdn.net/21aspnet/archive/2007/06/27/1667862.aspx
- http://blog.csdn.net/wingfeng19800215/archive/2007/02/27/1515925.aspx
- http://blog.csdn.net/chenyusiyuan/archive/2010/03/26/5418233.aspx
- Meego安装初体验 http://blog.csdn.net/tingsking18/archive/2010/06/03/5644390.aspx
- MTK task (原文 http://blog.csdn.net/yanwuxufeng/archive/2010/07/24/5762361.aspx)
- 我心目中的牛程序员---http://blog.csdn.net/jirigala/archive/2010/03/02/5340655.aspx
- http://blog.csdn.net/zhaocj/archive/2010/02/09/5302370.aspx
- http://blog.csdn.net/powerlly/archive/2010/03/05/5348521.aspx
- Nosql materials (http://blog.csdn.net/DL88250/archive/2010/01/14/5191092.aspx)
- 做的有点烦了
- 创建连接服务器
- 简单core问题的思考
- redo与undo
- Unix环境高级编程——第四章 目录和文件
- http://blog.csdn.net/ol_beta/archive/2010/10/27/5968817.aspx
- 离职了……
- UVa Problem 10213 How Many Pieces of Land? (土地分割)
- Eclipse RCP笔记
- U-Boot简介与嵌入式内核移植简介
- 随机显示广告的关键代码--4PM
- 共享内存不能使用指针《转载》
- Java软件低级错误: 定义equals方法的同时也要定义hashCode方法
- 最大K乘积问题: