日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用velocity模板以及itext生成pdf

發布時間:2025/4/9 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用velocity模板以及itext生成pdf 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

利用velocity模板以及itext生成pdf

我整理的源碼:http://download.csdn.net/download/u012174571/8748897

首先是velocity的使用:

???????? 1.下載:http://velocity.apache.org/download.cgi

???????? 2.導入包:velocity-1.7.jar、commons-lang-2.4.jar、commons-collections-3.2.1.jar這三個包導入工程中。

???????? 3.用法演示:

???????? 新建一個文件: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>

?

?

?

新建一個測試類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();

?

????? //把數據填入上下文

????? 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 在給路勁時會比較麻煩,

???????????

??????????? //取得velocity的上下文context

??????????? VelocityContext context = new VelocityContext();

?

?

??????????? StringWriter writer = new StringWriter();

?

??????????? //把數據填入上下文

??????????? t.merge(context, writer);

?

??????????? //輸出流

??????????? String out = writer.toString();

??????????? return out;

?? }

}

?

?

?

?

4.運行輸出結果:

<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的使用

下載包:需要兩個包:(最好都下最新的,不然不支持中文)

???????? 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盤創建一個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中的內容了;

?

?

中文處理:itext對中文支持不是很好,但是高版本的jar包已經可以支持中文了。在vm中添加樣式:<style>

*{font-family: SimSun;}

</style>

把所有的文字都指定為宋體(最好這個字體,我試過有的字體會少一些字);字體記得要往服務器加哦,服務器一般是linux的沒有中文字體哦!

轉載于:https://www.cnblogs.com/taocong/p/5939443.html

總結

以上是生活随笔為你收集整理的利用velocity模板以及itext生成pdf的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。