在前面的课程中我们完成了将运营数据导出到Excel文件的功能。在企业开发中,除了常见的Excel形式报表,还有PDF形式的报表。那么如何导出PDF形式的报表呢?
- 常见的PDF报表生成方式
1.1 iText
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便,下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类库了。
maven坐标:
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
示例代码:
package com.itheima.app;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class ItextDemo {
public static void main(String[] args) {
try {
Document document = new Document();//文档对象
PdfWriter.getInstance(document, new FileOutputStream("D:\\test.pdf"));//写出数据
document.open();
document.add(new Paragraph("hello itext"));
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}