1. Itext生成PDF文件
iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。通过iText
不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。 iText的安装非常方便,
下载iText.jar文件后,只需要在系统的CLASSPATH中加入iText.jar的路径,在程序中就可以使用iText类
库了。
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
public class ItextTest {
public static void main(String[] args) {
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("F:\\Itext\\test.pdf"));
document.open();
document.add(new Paragraph("hello itext"));
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
Itext是一个基础工具库,使用起来不太方便,JasperReports是在它基础上的封装
2. JasperReports 入门
2.1 引入类库
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
</dependency>
2.2 提供一个模板
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.9.0.final using JasperReports Library version 6.9.0-cb8f9004be492ccc537180b49c026951f4220bf3 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="demo" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="e3403475-74c8-4358-a0fa-fb5b703b1abe">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
<parameter name="company" class="java.lang.String"/>
<parameter name="reportDate" class="java.lang.String"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String"/>
<field name="address" class="java.lang.String"/>
<field name="email" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="90" splitType="Stretch">
<image>
<reportElement x="0" y="0" width="100" height="86" uuid="1696c855-f322-478c-aecb-12aab31c1743"/>
<imageExpression><![CDATA["https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1588691034872&di=c69fdbb8dd0f30a509e9a2c055480b3c&imgtype=0&src=http%3A%2F%2Fimg2.imgtn.bdimg.com%2Fit%2Fu%3D3291027462%2C3305585263%26fm%3D214%26gp%3D0.jpg"]]></imageExpression>
</image>
<staticText>
<reportElement x="210" y="20" width="200" height="40" uuid="2e246083-05c6-4dbd-9059-f7fe986d139e"/>
<textElement>
<font fontName="华文宋体" size="20"/>
</textElement>
<text><![CDATA[JasperReports Test]]></text>
</staticText>
<textField>
<reportElement x="468" y="50" width="100" height="30" uuid="14ac6dd3-7c52-457e-95b5-b8c2eebaae1b"/>
<textFieldExpression><![CDATA[$P{reportDate}]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="37" splitType="Stretch">
<textField>
<reportElement x="60" y="4" width="100" height="30" uuid="9fd8ea6a-722d-4c35-a4dc-74f3ed490709"/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="227" y="4" width="100" height="30" uuid="d2926cd3-c477-4801-98a6-8d9d7f43adec"/>
<textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="400" y="4" width="100" height="30" uuid="e7a64e8c-7c91-4c3a-9e1f-f9861353fd79"/>
<textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
</textField>
</band>
</detail>
<pageFooter>
<band height="37" splitType="Stretch">
<textField>
<reportElement x="230" y="5" width="100" height="30" uuid="0eaaff53-787f-4d02-a940-4fd8f249ad95"/>
<textElement textAlignment="Center"/>
<textFieldExpression><![CDATA[$P{company}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
</jasperReport>
这个demo.jrxml就是一个模板文件,jasperReports会填入对应的数据,生成最终PDF。
2.3 使用
public class JasperReportsTest {
@Test
public void test1() throws Exception{
String jrxmlPath =
"F:\\JasperReports\\jasperReportsDemo\\src\\main\\resources\\demo.jrxml";
String jasperPath =
"F:\\JasperReports\\jasperReportsDemo\\src\\main\\resources\\demo.jasper";
//编译模板
JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);
//构造数据
Map paramters = new HashMap();
paramters.put("reportDate","2019-10-10");
paramters.put("company","ittest");
List<Map> list = new ArrayList();
Map map1 = new HashMap();
map1.put("name","xiaoming");
map1.put("address","beijing");
map1.put("email","xiaoming@ittest.cn");
Map map2 = new HashMap();
map2.put("name","xiaoli");
map2.put("address","nanjing");
map2.put("email","xiaoli@ittest.cn");
list.add(map1);
list.add(map2);
//填充数据
JasperPrint jasperPrint =
JasperFillManager.fillReport(jasperPath,
paramters,
new JRBeanCollectionDataSource(list));
//输出文件
String pdfPath = "D:\\test.pdf";
JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
}
}
2.4 JasperReports原理
- JRXML : 报表填充模板,本质上是一个xml文件
- Jasper : 由模板生成的二进制文件,用于代码填充数据
- Jrprint: 当用数据填充完Jasper后生成的对象,用户输出报表
- Exporter: 报表数据管理类,可以指定要输出的报表为何种格式
- PDF/HTML/XML : 报表形式
比如要导出HTML,就可以这样写:
JasperExportManager.exportReportToHtmlFile(jasperPrint,pdfPath);