1>方法一Itext来实现生成文档这个pdf的文档
1.使用Itext来实现生成我都这个PDF的文档,代码如下
public static void main(String[] args) {
try {
Document document = new Document();//用来获得我都这个document对象来实现获得我都这个文档对象
PdfWriter.getInstance(document,new FileOutputStream(new File("D:\\cs\\test.pdf")));//用来引入我当前的这个pdf的路径
document.open();//打开我当前的这个文档对象s
document.add(new Paragraph("你好"));//在我都这个文档对象中添加我需要插入的文档信息
document.close();//关闭我都这个资源
} catch (Exception e) {
e.printStackTrace();
}
}
2>方法二使用JasperReports来生成我的这个来实现生成PDF
JasperReports就是基于itext来实现生成PDF文档的
在使用JasperReports的时候我需要引入我当前的这几个依赖包
<dependencies>
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.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>
</dependency>
</dependencies>
我的这个实现代码
/**
* 用来编写JasperReports测试方法
*/
@Test
public void test1() throws Exception{
String jrxmlPath =
"D:\\springbootWeb\\Vue+Springboot\\health_parent\\JasperReports\\src\\main\\resources\\demo.jrxml";//得到我的这个模板文件
String jasperPath =
"D:\\springbootWeb\\Vue+Springboot\\health_parent\\JasperReports\\src\\main\\resources\\demo.jasper";//获得我的这个jasper的文件
//编译模板
JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);
//构造数据
Map paramters = new HashMap();
paramters.put("reportDate","2022-03-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);
}
JasperReports的一个执行原理,架构图

?
他的参数的名称以及用处
JRXML:报表填充模板,本质是一个xml文件 Jasper:由JRXML模板编译成的二进制文件,用于代码填充数据 Jrprint:当用数据填充完Jasper后生成的对象,用于输出报表 Exporter:报表输出的管理类,可以指定要输出的报表为何种格式 PDF/HTML/XML:报表形式 |
3>用来获得我当前的这个报表模板
其实在使用JasperReports的时候我在导入我的这个报表模板的时候其实并不是我们手写,他主要是借助了我的这个相关的工具来实现自动生成的,然后我们需要借助一个软件Jaspersoft Studo的这个工具的软件,通过可视化的形式来得到我的这个软件
这个软件的下载地址:
Download Jaspersoft Business Intelligence Suite | Jaspersoft Community
下载这个

?
打开这个软件之后就会显示这个界面

?
选择我当前的newproject的时候就会显示我一下的这个界面,选择我的这个东西点击next

输入我的这个工程名点击finish,完成创建

创建完成之后右击我的这个模块,创建我的这个Jasper Resport的模板文件

然后就会显示一下的这个界面

直接选择我的这个A4,然后点击next之后就会显示我的这个界面,用来填入我的这个模板的名字

?
然后选择我的这个DB数据库类型,第一个是为空的,我们暂时可以先不选择

然后点next最后在点finish,来完成创建?
然后就会显示这个中间这一块就是我们的这个PDF生成之后的一个区域,然后他这里一共有三种视图模式
?
我们可以通过在这个区域来实现获得我的这个pdf的控件 当我拖动我当前的这个文件之后我可以选择我的这个图片显示的位置,我编辑的就是我使用URL地址来实现展示,然后输入我的这个图片的url地址,我当时是使用的七牛云上面的那个图片路径

?下一步用来输出我当前的这个日期,展示我们的这个日期控件

如果需要动态的显示我的这个文本的内容的话我需要自定义一个文本

?
然后直接选中我的这个信息,然后点击我的这个preview的时候他会要你自定义我的这个文本内容

?
然后就会在我的这个文本上面展示这种文本一般只是用来展示很小的数据,如果有很多的数据的话那么我就需要用到Fileds来便利渲染我的这个数据

?
使用Fields空间来实现我的数据的循环展示,这个空间它支持两种形式的数据显示,第一种就是使用JDBC来数据动态展示,第二种就是使用我的这个JavaBean的方式来渲染数据,创建我的这个链接对象
读取数据库中的数据
方法一:使用JDBC的驱动来获取数据

然后选择我的这个jdbc的连接池对象

选择之后然后进行相关的配置
选择我的这个Mysql的驱动包,注意我的这个驱动包一定要是8.0以上的版本

?
然后配置我的这个Mysql的时区

?
erverTimezone 和GMT+8

创建完之后我需要右击我这个demo的outline然后来使用sql来查询我需要查找的数据

?在这里选择我们刚刚创建 数据库

?
然后编写我的这个sql文件然后我的这个数据和字段全部都会显示出来

执行完sql之后我刚刚查询出来的字段就会显示出来,如果需要展示的话我可以直接把我的这个fields的字段直接拖动到我的这个试图上面来使用即可

?
拖动完成之后就会显示我的这些相关的套餐信息

?
注意事项

?
我们在预览的时候没有我们的这个表格边框那么我们因该去设置我们的一个pdf文档里面的数据来设置一个边框,来进行展示,首先雪中我需要设置的那个方块,步骤如下

?
我们去看一下效果

?
那么我们的这个数据行与行之间存在空隙,那么如何解决呢,我们可以直接去设置我们的那个模块宽度

?
我们把空隙弄到最小,然后就是一个完整的表格了

?
然后点击source得到我的这个jrxml文件,然后在我的这个idea当中运行生成PDF文件
方法二.使用我的这个JavaBean的方式来展示我的数据
1.在使用JavaBean的话我们就不需要按照上面的方法来实现去导入我的这个数据库的数据,但是我们需要在这个工具当中手动的创建我的这个Fileds,但是需要注意的是我的这个Fileds的名字要和我的这个后台的这个实体类名字好符合一直,然后我们仍然还是可以把我的这个Fileds来拖动到我的这个Design上面来

在使用JavaBean的方式来调用来生成这个模板的话他是无法进行预览的需要通过使用我们代码的方式给我们的这个数据来进行填充注意我的这个文字必须要是华文宋体格式的,然后我这里会有相关的配置文件,否则代码无法识别 ,注意我的这个字体需要自己在文本框中输入华文宋体

这是我们相关的jrxml文件,将这个文件的内容拷贝到我们的idea当中

【1】使用代码的方式来生成JDBC形式的PDF文件
他的Java代码如下
/**
* 因为我当前需要通过我的这个文件中的sql语句去查询我的数据库当中的数据,所有我们需要引入我的 jdbc的代码
* 用来根据文档这个Drug的jrxml文件来读取文档这个信息,然后生成pdf文件,
* 然后以流的方式来把文件输出到我的电脑磁盘当中
*/
@Test
public void test2() throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/health?" +
"serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8"
, "root", "roots");//得到我的这个数据库的连接对象
String jrxmlPath =
"D:\\springbootWeb\\Vue+Springboot\\health_parent\\JasperReports\\src\\main\\resources\\Drug.jrxml";//得到我的这个模板文件
String jasperPath =
"D:\\springbootWeb\\Vue+Springboot\\health_parent\\JasperReports\\src\\main\\resources\\Drug.jasper";//获得我的这个jasper的文件
//得到我的这个模板编译对象,把我的这个模板文件和我的这个生成的jasper的二进制文件指定我的相关的路径
JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);
//为我的这个模板文件生成我的这个二进制文件
Map map = new HashMap();
map.put("company","套餐管理");//他的这个key的是我当前的这恶个jrxml文件中定义好的这个标题字段名称
map.put("Text","nice");//他的这个key的是我当前的这恶个jrxml文件中定义好的这个字段名称
/*
下一步我们就需要用来进行编译数据了,那么我的这个填充数据的话它的这个对象中的参数的意思是一下几个
第一个参数就是我们的这个编译之后的这个文件的路径变量
第二个参数就是我需要填充的这个map集合
第三个参数就是我们的这个数据源,来实现我们的这个jrxml文件中的sql语句,来查询我的数据库中的书
*/
JasperPrint print = JasperFillManager.fillReport(jasperPath, map, connection);
//然后把我们的这个pdf文件来进行输出
String pdfPath = "D:\\Drug.pdf";
//把我们的这个JasperPrint的对象和我的这个pdf的路径放入到我的这个输出文件的对象当中
JasperExportManager.exportReportToPdfFile(print,pdfPath);
}
把这些代码放入到我们的这个idea中来进行生成pdf
如果在我的这个代码运行的过程中如果说出现了一下错误

这三个文件中的ttl和我的这个xml文件需要装在名字为stsong的文件夹里面否则无法识别

文件见百度网盘
链接:https://pan.baidu.com/s/1rzc86-irjv7ARelhshOJHw? 提取码:GFDS
【2】使用代码的方式来实现JavaBean模式的PDF文件
使用JavaBean的方式来生成PDF它的方式主要是通过我的这个集合的方式来把我的这个数据分装成一个list集合如果说有多条数据的话那么我就可以使用一个list集合里面存入多个Map集合,它key就是我的这个分装的字段名称就是我的这个控件的名称
?
/**
* 因为我当前需要通过我的这个文件中的我们需要使用我们的这个JavaBend的实体类来获得我的这些相关的数据
* 用来根据文档这个Drug的jrxml文件来读取文档这个信息,然后生成pdf文件,
* 然后以流的方式来把文件输出到我的电脑磁盘当中
*/
@Test
public void test3() throws Exception {
String jrxmlPath =
"D:\\springbootWeb\\Vue+Springboot\\health_parent\\JasperReports\\src\\main\\resources\\Drug2.jrxml";//得到我的这个模板文件
String jasperPath =
"D:\\springbootWeb\\Vue+Springboot\\health_parent\\JasperReports\\src\\main\\resources\\Drug2.jasper";//获得我的这个jasper的文件
//得到我的这个模板编译对象,把我的这个模板文件和我的这个生成的jasper的二进制文件指定我的相关的路径
JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);
//为我的这个模板文件生成我的这个二进制文件
Map map = new HashMap();
map.put("company","套餐管理");//他的这个key的是我当前的这恶个jrxml文件中定义好的这个字段名称
map.put("Text","nice");
//我们使用JavaBean的方式来实现数据填充的话那么我们就需要用到我的这个List集合了
/*
它的一个实现方法是这样的:首先我们可以先定义一个list集合,然后再定义两个Map集合,
那么我的map集合里面的key要和我的这个jrxml中的文字的控件的名字要一样,否则在编译的时候这个文件无法找到相对应的字段
然后将我们的这两个集合放到我的这个list集合里面最终放到我们的 这个填充数据的这个对象里面
*/
List<Map> Drug = new ArrayList<>();//定义一个list集合
Map mapDurg1 = new HashMap();//map集合,存入到套餐信息
mapDurg1.put("name","老年人套餐");//存入我的这个套餐名称
mapDurg1.put("code","001");//存入我的这个套餐编号
mapDurg1.put("age","50-60");//存入我的这个适用年龄
mapDurg1.put("sex","1");//存入我的这个适用性别,0代表女,1代表男
Map mapDurg2 = new HashMap();//按照上面的套路一样,存入我的第二个套餐数据
mapDurg2.put("name","学生视力体检套餐");//存入我的这个套餐名称
mapDurg2.put("code","002");//存入我的这个套餐编号
mapDurg2.put("age","6-18");//存入我的这个适用年龄
mapDurg2.put("sex","0");//存入我的这个适用性别,0代表女,1代表男
//最终把我Map集合存入到我的这个list集合中,只包含我的这个JavaBean中的这个数据的map结合
Drug.add(mapDurg1);
Drug.add(mapDurg2);
/*
下一步我们就需要用来进行编译数据了,那么我的这个填充数据的话它的这个对象中的参数的意思是一下几个
第一个参数就是我们的这个编译之后的这个文件的路径变量
第二个参数就是我需要填充的这个其他字段的map集合
第三个存入我的这个JavaBean的这个list集合
*/
JasperPrint print = JasperFillManager.fillReport(jasperPath,map,new JRBeanCollectionDataSource(Drug));
//然后把我们的这个pdf文件来进行输出
String pdfPath = "D:\\Drug2.pdf";
//把我们的这个JasperPrint的对象和我的这个pdf的路径放入到我的这个输出文件的对象当中
JasperExportManager.exportReportToPdfFile(print,pdfPath);
}
|