PDF生成方案之iText

PDF生成方案之iText

前言

有一段时间没有更新了,最近有点懒了,在此立下flag,督促自己能够常更新吧,话不多说,进入主题,今天介绍的是电子凭证(pdf)生成的解决方案,会从几个常用的工具来介绍,也会对比一下几者之间的性能。

完整代码下载地址: http://download.csdn.net/download/u010695794/9855688

iText是什么?

在官网中 http://itextpdf.com/描述:

iText, the world’s preferred PDF library,iText is a software developer toolkit that allows users to integrate PDF functionalities within their applications, processes or products

iText,是世界上首选的PDF库,iText是一个软件开发人员工具包,允许用户将PDF功能集成到其他应用程序,流程或者产品中。
其特点有:

  1. 支持表格
  2. 图片,
  3. 定制字体
  4. 支持合并pdf 等。

准备工作

在使用iTex时,我们需要添加Maven依赖,如下:

1
2
3
4
5
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.11</version>
</dependency>

入门实例

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* 生成pdf文件
*/
@Test
public void testCreatePdf(){
try{
// 1. new Document
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(DEST));
// 2. 打开document
document.open();
// 3. 添加内容
document.add(new Paragraph("hello world!"));
// 4. 关闭 (如果未关闭则会生成无效的pdf文件)
document.close();
}catch(DocumentException ex){
ex.printStackTrace();
}catch (FileNotFoundException ex){
ex.printStackTrace();
}
}

效果:
helloWorld

iText字体

iText内置支持多种字体,我们可以通过FontFactory类找到iText的内置字体:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Courier;
Courier-Bold;
Courier-Oblique;
Courier-BoldOblique;
Helvetica;
Helvetica-Bold;
Helvetica-Oblique;
Helvetica-BoldOblique;
Symbol;
Times;
Times-Roman;
Times-Bold;
Times-Italic;
Times-BoldItalic;
ZapfDingbats;

同样,iText也允许自定义字体,itext默认字体不支持中文,这时,我们就可以通过自定义字体来解决这个问题,代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Test
public void testChineseFontPdf(){
try {
//1. new document
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("/tmp/pdf/2017/1.pdf"));
//2. open document
document.open();
BaseFont bf = BaseFont.createFont(path()+"fonts/SIMKAI.TTF", BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
//3. 注册字体
Font font = new Font(bf,30);
//3. 添加段落,并设置字体
document.add(new Paragraph("hello world(中文,)",font));
//4. close document
document.close();
}catch(FileNotFoundException ex){
ex.printStackTrace();
}catch(DocumentException ex){
ex.printStackTrace();
}catch(IOException ex){
ex.printStackTrace();
}
}

/**
* 获取资源的路径
* @return
*/
private String path(){
String path = this.getClass().getResource("/").getPath();
return path;
}

效果如下:
中文

表格

通常我们会在pdf文件中生成表格,来展示数据,在iText中也是非常方便的,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 生成表格
*/
@Test
public void testTablePdf(){
try {
//1.new document
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream("/tmp/pdf/2017/1.pdf"));
//2 open document
document.open();
//3.添加pdf tables 3表示列数,
PdfPTable pdfPTable = new PdfPTable(3);
// cell表示单元格,(12表示12个单元格,3列,12个单元格就形成来一个4行3列的表格)
for (int i = 0; i < 12; i++) {
pdfPTable.addCell("cell" + i);
}
document.add(pdfPTable);
//4. 关闭document
document.close();
}catch(DocumentException ex){
ex.printStackTrace();
}catch(FileNotFoundException ex){
ex.printStackTrace();
}
}

生成效果如下所示:
表格

图片

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 生成image
*/
@Test
public void testImagePdf(){
try {
//1.new document
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream("/tmp/pdf/2017/1.pdf"));
//2 open document
document.open();
document.add(new Paragraph("Hello world!"));
//3.添加image
Image image = Image.getInstance(path()+"/images/timg.jpg");
image.scaleAbsolute(PageSize.A4.rotate());
document.add(image);
//4. 关闭document
document.close();
}catch(DocumentException ex){
ex.printStackTrace();
}catch(FileNotFoundException ex){
ex.printStackTrace();
}catch (IOException ex){
ex.printStackTrace();
}
}

效果:
表格

html转换pdf

我们在实际项目中,需要将html格式的文档转换为pdf工具,在iText中,默认是不直接支持html格式文档的,我们可以借助com.itextpdf.tool来生成
首先添加maven

1
2
3
4
5
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.11</version>
</dependency>

代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* author: andy
* date: 17-5-30
* blog: www.andyqian.com
* version: 0.0.1
* description: 生成html格式內容的工具類
*/
public class CreateHtmlPdfTest {

/**
* html内容路径
*/
private static final String HTML_PATH= "/html/helloworld.html";
private static final String FONT_PATH="/fonts/SIMKAI.TTF";

/**
* 生成pdf格式的內容
*/
@Test
public void testHtml(){
try {
//1. new document
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/tmp/pdf/2017/1.pdf"));
//2.open document
document.open();
//3. 设置字体
XMLWorkerFontProvider xmlWorkerFontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
xmlWorkerFontProvider.register(getContextPath()+FONT_PATH);
//4. 文件
FileInputStream fileInputStream = new FileInputStream(getContextPath()+HTML_PATH);
XMLWorkerHelper.getInstance().parseXHtml(writer, document,fileInputStream, Charset.forName("UTF-8"),xmlWorkerFontProvider);
//3. close document
document.close();
}catch(Exception ex){
ex.printStackTrace();
}
}

/**
* 获取上下文路径
* @return
*/
private String getContextPath(){
return this.getClass().getResource("/").getPath();
}
}

html代码内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>标题</title>
</head>
<style>
*{
font-family: KaiTi_GB2312;
}
</style>
<body>
hello world! XMLWorkerHelper [这是带有中文的html格式内容]
</body>
</html>

代码结构为:
项目结构
效果如下所示:
带中文的html

7.iText+Freemarker

首先添加Freemarker Maven依赖如下所示:

1
2
3
4
5
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>

代码如下所示:

/**
 * author: andy
 * date: 17-5-30
 * blog: www.andyqian.com
 * version: 0.0.1
 * description: 通过feemarker工具类生成pdf
 */
public class CreateFeemarkerPdfTest {

    private static final String FONT_PATH="/fonts/SIMKAI.TTF";
    private static final String HTML_PATH="/html/feemarker.html";

    @Test
    public void testCreatePdf(){
            try {
                //1. new document
                Document document = new Document();
                PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("/tmp/pdf/2017/1.pdf"));
                //2.open document
                document.open();
                //3. 设置字体
                XMLWorkerFontProvider xmlWorkerFontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
                xmlWorkerFontProvider.register(getContextPath()+FONT_PATH);

                //4. 设置模板内容
                Map<String,Object> params = new HashMap<String,Object>();
                params.put("name","鞠骞");
                params.put("career","软件开发");
                params.put("blog","http://www.andyqian.com");
                String content = getFreeMarkerText(htmlContent(),params);
                //4. 文件
                InputStream inputStream = new ByteArrayInputStream(content.getBytes());
                XMLWorkerHelper.getInstance().parseXHtml(writer, document,inputStream, Charset.forName("UTF-8"),xmlWorkerFontProvider);
                //3. close document
                document.close();
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }

    /**
     * 获取上下文路径
     * @return
     */
    private String getContextPath(){
        return this.getClass().getResource("/").getPath();
    }

    /**
     * freemarker模板方法
     * @param templateTxt 模板文本
     * @param map  模板参数
     * @return
     * @throws Exception
     */
    public static String getFreeMarkerText(String templateTxt, Map<String, Object> map) throws Exception {
        String result = null;
        Configuration config = new Configuration(Configuration.VERSION_2_3_23);
        try {
            StringTemplateLoader e = new StringTemplateLoader();
            e.putTemplate("t", templateTxt);
            config.setTemplateLoader(e);
            config.setDefaultEncoding("UTF-8");
            Template template = config.getTemplate("t", "UTF-8");
            StringWriter out = new StringWriter();
            template.process(map, out);
            result = out.toString();
            return result;
        } catch (IOException iex) {
            throw new Exception("获取freemark模版出错", iex);
        } catch (TemplateException ex) {
            throw new Exception("freemark模版处理异常", ex);
        }
    }

    /**
     * 获取html内容
     * @return
     */
    private String htmlContent(){
        String result = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(getContextPath() + HTML_PATH);
            int len=0;
            byte[] array = new byte[1024];
            StringBuffer stringBuffer = new StringBuffer();
            while((len=fileInputStream.read(array))!=-1){
                stringBuffer.append(new String(array,0,len));
            }
            result = stringBuffer.toString();
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }
}

效果如下:
freemarker

参考资料

  1. http://developers.itextpdf.com/developers-home
  2. http://itextpdf.com/

小结

本文从iText最基本的用法,分别介绍从表格,图片,HTML,Feemarker来介绍iText,希望能够帮助到你。
完整代码下载地址: http://download.csdn.net/download/u010695794/9855688


这里写图片描述

扫码关注,一起进步
个人博客: http://www.andyqian.com