利用velocity模板以及itext生成pdf
利用velocity模板以及itext生成pdf
我整理的源碼:http://download.csdn.net/download/u012174571/8748897
首先是velocity的使用:
???????? 1.下載:http://velocity.apache.org/download.cgi
???????? 2.導(dǎo)入包:velocity-1.7.jar、commons-lang-2.4.jar、commons-collections-3.2.1.jar這三個(gè)包導(dǎo)入工程中。
???????? 3.用法演示:
???????? 新建一個(gè)文件:hello.vm放在根目錄下,
| <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <style> *{font-family: SimSun;} </style> </head> <body> ??? <p> ?????? ${name} ??? </p> ?? ?? ${date} </body> </html> ? |
?
?
新建一個(gè)測(cè)試類TestVelocity
| import java.io.StringWriter; import java.util.Date; ? import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; ? ? public class TestVelocity { ? ?? public static void main(String[] args) throws Exception { ????? //初始化并取得Velocity引擎 ????? VelocityEngine ve = new VelocityEngine(); ????? ve.init(); ? ????? //取得velocity的模版 ????? Template t = ve.getTemplate("src/hello.vm"); ? ????? //取得velocity的上下文context ????? VelocityContext context = new VelocityContext(); ? ????? //往vm中寫入信息 ????? context.put("name", "Liang"); ????? context.put("date", (new Date()).toString()); ? ? ????? StringWriter writer = new StringWriter(); ? ????? //把數(shù)據(jù)填入上下文 ????? t.merge(context, writer); ? ????? ????? String out = writer.toString(); ????? System.out.println(writer.toString()); ? ?? } ?? public static String get() throws Exception{ ????? //初始化并取得Velocity引擎 ??????????? VelocityEngine ve = new VelocityEngine(); ??????????? ve.init(); ? ??????????? //取得velocity的模版 ??????????? Template t = ve.getTemplate("src/hello.vm","UTF-8"); ??????????? //velocity 在給路勁時(shí)會(huì)比較麻煩, ??????????? ??????????? //取得velocity的上下文context ??????????? VelocityContext context = new VelocityContext(); ? ? ??????????? StringWriter writer = new StringWriter(); ? ??????????? //把數(shù)據(jù)填入上下文 ??????????? t.merge(context, writer); ? ??????????? //輸出流 ??????????? String out = writer.toString(); ??????????? return out; ?? } } |
?
?
?
?
4.運(yùn)行輸出結(jié)果:
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8" />
<style>
*{font-family:SimSun;}
</style>
</head>
<body>
??? <p>
?????? Liang
??? </p>
???????? Thu May 28 14:23:22 CST 2015
</body>
</html>
?
?
其次itext的使用:
下載包:需要兩個(gè)包:(最好都下最新的,不然不支持中文)
???????? 1.itext核心包: http://sourceforge.net/projects/itext/files/
???????? 2.xml包:http://sourceforge.net/projects/xmlworker/files/
其中有用的是:itext下的itextpdf-5.5.6.jar
??????????????????????????? ? xml下的xmlworker-5.5.6.jar
在E盤創(chuàng)建一個(gè)html;寫上些東西(先不要寫中文)
| import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.nio.charset.Charset; ? ? import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfWriter; import com.itextpdf.tool.xml.XMLWorkerHelper; ? public class Test? { ? ?? public static final String HTML = "E:/MyHtml.html"; ??? public static final String DEST = "E:/hero.pdf"; ? ??? /** ???? * Creates a PDF with the words "Hello World" ???? * @param file ???? * @throws IOException ???? * @throws DocumentException ???? */ ??? public void createPdf(String file) throws Exception { ??????? // step 1 ??????? Document document = new Document(); ??????? // step 2 ??????? PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); ??????? // step 3 ??????? document.open(); ??????? String value = TestVelocity.get(); ??????? @SuppressWarnings("deprecation") ??????? Reader reader = null; ??????? reader = new StringReader(value); ??????? ??????? // step 4 //??????? XMLWorkerHelper.getInstance().parseXHtml(writer, document, reader); ??????? XMLWorkerHelper.getInstance().parseXHtml(writer, document, ??????? ???? new FileInputStream(HTML) , Charset.forName("UTF-8")); ??????? // step 5 ??????? document.close(); ??? } ? ??? /** ???? * Main method ???? */ ??? public static void main(String[] args) throws Exception{ ??????? File file = new File(DEST); ??????? file.getParentFile().mkdirs(); ??????? new Test().createPdf(DEST); ??? } } |
?
?????
ok可以去e盤找pdf了。
?
?
?
?
兩者合并:
???????? 上邊代碼中的
?String value = TestVelocity.get();
??????? @SuppressWarnings("deprecation")
??????? Reader reader = null;
??????? reader = new StringReader(value);
???????
??????? // step 4
//???????XMLWorkerHelper.getInstance().parseXHtml(writer, document, reader);
就是去找velocity并交給itex生成pdf;將注解放開,把這段? XMLWorkerHelper.getInstance().parseXHtml(writer,document,
??????? ???? new FileInputStream(HTML) , Charset.forName("UTF-8"));
注解掉,ok再生成的pdf就是hello.vm中的內(nèi)容了;
?
?
中文處理:itext對(duì)中文支持不是很好,但是高版本的jar包已經(jīng)可以支持中文了。在vm中添加樣式:<style>
*{font-family: SimSun;}
</style>
把所有的文字都指定為宋體(最好這個(gè)字體,我試過有的字體會(huì)少一些字);字體記得要往服務(wù)器加哦,服務(wù)器一般是linux的沒有中文字體哦!
轉(zhuǎn)載于:https://www.cnblogs.com/taocong/p/5939443.html
總結(jié)
以上是生活随笔為你收集整理的利用velocity模板以及itext生成pdf的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 项目中的经验及教训
- 下一篇: 浅谈runtime运行时机制