常见PDF报表生成使用及详解!

1、常见的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.itehima.itext;

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;

/**
 * @Author panghl
 * @Date 2021/5/4 17:53
 * @Description Itext生成PDF文件
 **/
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();
        }
    }
}

注意:太过繁杂,很少使用!

 

1.2 JasperReports

JasperReports是一个强大、灵活的报表生成工具,能够展示丰富的页面内容,并将之转换成PDF,HTML,或者XML格式。该库完全由java写成,可以用于在各种Java应用程序,包括J2EE,Web应用程序中生成动态内容。一般情况下, JasperReports会结合 Jaspersoft Studio(模板设计器)使用导出PDF报表。

 

maven坐标:

<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
</dependency>

2. JasperReports概述

2.1 JasperReports快速体验

本小节我们先通过一个快速体验来感受一下JasperReports的开发过程。

第一步:创建maven工程,导入JasperReports的maven坐标

<dependencies>
<dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.16.0</version>
            <exclusions>
                <exclusion>
                    <groupId>com.lowagie</groupId>
                    <artifactId>itext</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

第二步:将提前准备好的jrxml文件复制到maven工程中(后面会详细讲解如何创建jrxml文件)

<?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["http://www.itcast.cn/2018czgw/images/logo.png"]]></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>

 

第三步:编写单元测试,输出PDF报表

import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author panghl
 * @Date 2021/5/4 18:36
 * @Description TODO
 **/
public class JasperReportsTest {
    @Test
    public void testJasperReports() throws Exception {
        String jrxmlPath =
                "D:\\JavaStudy\\RealProject\\ssm_heima_health\\health_parent\\JasperReports\\src\\main\\resources\\demo.jrxml";
        String jasperPath =
                "D:\\JavaStudy\\RealProject\\ssm_heima_health\\health_parent\\JasperReports\\src\\main\\resources\\demo.jasper";
        //编译模板
        JasperCompileManager.compileReportToFile(jrxmlPath, jasperPath);
        Map paramters = new HashMap<>();
        paramters.put("reportDate","2019-10-10");
        paramters.put("company","itcast");
        List<Map> list = new ArrayList();
        Map map1 = new HashMap();
        map1.put("name","xiaoming");
        map1.put("address","beijing");
        map1.put("email","xiaoming@itcast.cn");
        Map map2 = new HashMap();
        map2.put("name","xiaoli");
        map2.put("address","nanjing");
        map2.put("email","xiaoli@itcast.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);
    }

}

 

测试会发现报错信息为:

net.sf.jasperreports.engine.util.JRFontNotFoundException: Font "华文宋体" is not available to the JVM. See the Javadoc for more details.

 

我的解决办法:

导入对应的字体文件以及配置文件

链接:https://pan.baidu.com/s/1VaYdMnt01iS6MnAIxxLvFg 
提取码:tsx3 

 

下面解决办法由其他播客拷贝:

这是由于没有对应字体导致的, 因为有的japserperreport.jar中的default.jasperreports.properties文件中

net.sf.jasperreports.awt.ignore.missing.font=false

如果字体不存在的话就会出现异常了。

简单的解决方法 :在classpath 中加一个jasperreports.properties文件,加一行

net.sf.jasperreports.awt.ignore.missing.font=true

重新编译。

或是将jasperreport.jar 中的properties文件内容修改,重新打包。

 

2.2 JasperReports原理

  • JRXML:报表填充模板,本质是一个xml文件
  • Jasper:由JRXML模板编译成的二进制文件,用于代码填充数据
  • Jrprint:当用数据填充完Jasper后生成的对象,用于输出报表
  • Exporter:报表输出的管理类,可以指定要输出的报表为何种格式
  • PDF/HTML/XML:报表形式

2.3 开发流程

使用JasperReports导出pdf报表,开发流程如下:

1. 制作报表模板

2. 模板编译

3. 构造数据

4. 填充数据

5. 输出文件

 

3. 模板设计器Jaspersoft Studio

Jaspersoft Studio是一个图形化的报表设计工具,可以非常方便的设计出PDF报表模板文件(其实就是一 个xml文件),再结合JasperReports使用,就可以渲染出PDF文件。

下载地址:https://community.jaspersoft.com/community-download

3.1 Jaspersoft Studio面板介绍

3.2 创建工程和模板文件

打开Jaspersoft Studio工具,首先需要创建一个工程,创建过程如下:

 

 

创建完工程后,可以在工程上点击右键,创建模板文件:

 

可以看到创建处理的模板文件后缀为jrxml,从设计区面板可以看到如下效果:

 

可以看到整个文件是可视化的,分为几大区域(Title、Page Header、Column Header等),如果某些 区域不需要也可以删除。

 

在面板左下角可以看到有三种视图方式:

Design(设计模式)、Source(源码模式)、Preview(预览 模式):

  • 通过Design视图可以看到模板的直观结构和样式
  • 通过Source视图可以看到文件xml源码
  • 通过Preview视图可以预览PDF文件输出后的效果

 

通过右侧Palette窗口可以看到常用的元素:

 

 

3.3 设计模板文件

3.3.1 增减Band

可以根据情况删除或者增加模板文件中的区域(称为Band),例如在Page Header区域上点击右键, 选择删除菜单:

其中Detail区域可以添加多个,其他区域只能有一个。

 

3.3.2 将元素应用到模板中

3.3.2.1 Image元素

从右侧Palette面板中选择Image元素(图片元素),拖动到Title区域:

3.3.2.2 Static Text元素

Static Text元素就是静态文本元素,用于在PDF文件上展示静态文本信息:

3.3.2.3 Current Date元素

Current Date元素用于在报表中输出当前系统日期,将改元素拖动到Title区域:

3.3.3 动态数据填充

上面我们在PDF文件中展示的都是一些静态数据,那么如果需要动态展示一些数据应该如何实现呢?我 们可以使用Outline面板中的Parameters和Fields来实现。

 

3.3.3.2 Fields

使用Fields方式进行数据填充,既可以使用jdbc数据源方式也可以使用JavaBean数据源方式。

  • jdbc数据源数据填充

第一步:在Repository Explorer面板中,在Data Adapters点击右键,创建一个数据适配器

第二步:选择Database JDBC Connection

第三步:选择mysql数据库,并完善jdbc连接信息

为了能够在Jaspersoft Studio中预览到数据库中的数据,需要加入MySQL的驱动包

第四步:在Outline视图中,右键点击工程名,选择Database and Query菜单

第五步:在弹出的对话框中选择刚刚创建的JDBC数据库连接选项

第六步:在弹出对话框中Language选择sql,在右侧区域输入SQL语句并点击Read Fields按钮

可以看到通过点击上面的Read Fields按钮,已经读取到了t_setmeal表中的所有字段信息并展示在了下 面,这些字段可以根据需要进行删除或者调整位置

第七步:在Outline视图中的Fields下可以看到t_setmeal表中相关字段信息,拖动某个字段到设计区的 Detail区域并调整位置

可以看到,在拖动Fields到设计区时,同时会产生两个元素,一个是静态文本,一个是动态元素。静态 文本相当于表格的表头,可以根据需要修改文本内容。最终设计完的效果如下:

第八步:使用Preview预览视图进行预览


通过上图可以看到,虽然列表数据展示出来了。

 

JavaBean数据源数据填充

第一步:复制上面的demo1.jrxml文件,名称改为demo2.jrxml

第二步:打开demo2.jrxml文件,将按照之前的方式将Title的内容创建出来

第三步:在Fields处点击右键创建新的Field

 

第四步:将创建的Fields拖动到Detail区域并调整好位置

注意:使用此种JavaBean数据源数据填充方式,无法正常进行预览,因为这些动态Fields需要在Java程 序中动态进行数据填充。

前面我们已经使用Jaspersoft Studio设计了两个模板文件:demo1.jrxml和demo2.jrxml。其中 demo1.jrxml的动态列表数据是基于JDBC数据源方式进行数据填充,demo2.jrxml的动态列表数据是基 于JavaBean数据源方式进行数据填充。本小节我们就结合JasperReports的Java API来完成pdf报表输 出。

 

3.4.1 JDBC数据源方式填充数据

第一步:创建maven工程,导入相关maven坐标

<dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.16.0</version>
            <exclusions>
                <exclusion>
                    <groupId>com.lowagie</groupId>
                    <artifactId>itext</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
<!--        <version>5.1.47</version>-->
        </dependency>

第二步:将设计好的demo1.jrxml文件复制到当前工程的resources目录下

第三步:编写单元测试

@Test
    public void jasperReportsDemo1() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/health?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai", "root", "123456");

        String jrxmlPath =
                "D:\\JavaStudy\\RealProject\\ssm_heima_health\\health_parent\\JasperReports\\src\\main\\resources\\demo1.jrxml";
        String jasperPath =
                "D:\\JavaStudy\\RealProject\\ssm_heima_health\\health_parent\\JasperReports\\src\\main\\resources\\demo1.jasper";
        //编译模板
        JasperCompileManager.compileReportToFile(jrxmlPath, jasperPath);
        //为模板文件准备数据,用于最终的PDF文件数据填充
        Map paramters = new HashMap<>();
        paramters.put("company", "传智健康");
        //填充数据
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath, paramters, connection);
        //输出文件
        String pdfPath = "D:\\testdemo1.pdf";
        JasperExportManager.exportReportToPdfFile(jasperPrint, pdfPath);
    }

 

因为我前面已经解决了中文的问题,所以这里中文也会加载成功!!! 

 

3.4.2 JavaBean数据源方式填充数据

第一步:为了能够避免中文无法显示问题,首先需要将demo2.jrxml文件相关元素字体改为“华文宋 体”并将demo2.jrxml文件复制到maven工程的resources目录下

第二步:编写单元测试方法输出PDF文件

 @Test
    public void jasperReportsDemo2() throws Exception {
        String jrxmlPath =
                "D:\\JavaStudy\\RealProject\\ssm_heima_health\\health_parent\\JasperReports\\src\\main\\resources\\demo2.jrxml";
        String jasperPath =
                "D:\\JavaStudy\\RealProject\\ssm_heima_health\\health_parent\\JasperReports\\src\\main\\resources\\demo2.jasper";
        //编译模板
        JasperCompileManager.compileReportToFile(jrxmlPath, jasperPath);
        //为模板文件准备数据,用于最终的PDF文件数据填充
        Map paramters = new HashMap<>();
        paramters.put("company", "传智健康");

        //Javabean 数据源填充,用于填充列表数据
        List<Map> list = new ArrayList();
        Map map1 = new HashMap();
        map1.put("tName", "入职体检套餐");
        map1.put("tCode", "RZTJ");
        map1.put("tAge", "18-60");
        map1.put("tPrice", "66.6");
        Map map2 = new HashMap();
        map2.put("tName", "阳光爸妈老年健康体检");
        map2.put("tCode", "YGBM");
        map2.put("tAge", "55-60");
        map2.put("tPrice", "55.5");
        list.add(map1);
        list.add(map2);

        //填充数据
        JasperPrint jasperPrint = JasperFillManager.fillReport(
                jasperPath,
                paramters,
                new JRBeanCollectionDataSource(list));
        //输出文件
        String pdfPath = "D:\\testdemo2.pdf";
        JasperExportManager.exportReportToPdfFile(jasperPrint, pdfPath);
    }

 

4. 在项目中输出运营数据PDF报表

本小节我们将在项目中实现运营数据的PDF报表导出功能。

4.1 设计PDF模板文件

使用Jaspersoft Studio设计运营数据PDF报表模板文件health_business3.jrxml,设计后的效果如下:

4.2 搭建环境

第一步:导入maven坐标

<!--pdf 报表-->
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.16.0</version>
            <exclusions>
                <exclusion>
                    <groupId>com.lowagie</groupId>
                    <artifactId>itext</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
            <version>2.1.7</version>
        </dependency>

第二步:将资源中提供的模板文件health_business3.jrxml复制到health_backend工程的template目录 下

第三步:将解决中问题的相关资源文件复制到项目中

 

4.3 修改页面

修改health_backend工程的report_business.html页面,添加导出PDF的按钮并绑定事件

 

4.4 Java代码实现

在health_backend工程的ReportController中提供exportBusinessReport4PDF方法

/**
     * 导出运营数据到PDF文件并提供客户端下载
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/exportBusinessReport4PDF", method = RequestMethod.GET)
    public void exportBusinessReport4PDF(HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> result = reportService.getBusinessReportData();
            //取出返回结果数据,准备将报表数据写入到Excel文件中
            List<Map> hotSetmeal = (List<Map>) result.get("hotSetmeal");
            //动态获取模板文件绝对磁盘路径
            String jrxmlPath = request.getSession().getServletContext().getRealPath("template") + File.separator + "health_business3.jrxml";
            String jasperPath = request.getSession().getServletContext().getRealPath("template") + File.separator + "health_business3.jasper";
            //编译模板
            JasperCompileManager.compileReportToFile(jrxmlPath, jasperPath);
            //填充数据
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath, result, new JRBeanCollectionDataSource(hotSetmeal));

            //设置信息头
            response.setContentType("application/pdf;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("运营数据.pdf", "utf-8"));
            //写出文件,关闭流
            OutputStream outputStream = response.getOutputStream();

            //输出文件
            JasperExportManager.exportReportToPdfStream(jasperPrint,outputStream);

            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

 

 

 

 

 

 

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值