- 通过iText打印pdf文档
1 导入jar包
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
2 详细代码段
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream("F:\\test.pdf"));
document.open();
document.add(new Paragraph("hello itext"));
document.close();
- 通过jasperreport和模板设计器jaspersort studio答应pdf文档
jasperreport的底层也是itext,jasperreport需要jrxml模板。jrxml模板可以由jaspersort studio生成。
1 导入jar包
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
</dependency>
- 详细代码段
String jrxmlPath ="F:\\Users\\lin\\IdeaProjects\\gitdemo1\\health_parent\\itext\\src\\main\\resources\\demo.jrxml";
String jasperPath = "F:\\Users\\lin\\IdeaProjects\\gitdemo1\\health_parent\\itext\\src\\main\\resources\\demo.jasper";
JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);
Map paramter = new HashMap();
paramter.put("reportDate","2019-10-10");
paramter.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,paramter,new JRBeanCollectionDataSource(list));
String pdfPath = "f:\\test.pdf";
JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
- 介绍jaspersort studio
- jdbc数据源式填充数据
在
在driver classpath中加载数据驱动,然后点击测试。
然后右击test1 选择databaseandquery 点击确定。之后可以在fields里拖拽变量。
//jrxmll 路径
String jrxmlPath ="F:\\Users\\lin\\IdeaProjects\\gitdemo1\\health_parent\\itext\\src\\main\\resources\\demo3.jrxml";
//生成的jasper文件的路径
String jasperPath = "F:\\Users\\lin\\IdeaProjects\\gitdemo1\\health_parent\\itext\\src\\main\\resources\\demo.jasper";
//把jrxmll编译成jasper文件
JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);
//需要提供mysql的连接
Connection connection = connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/health","root","root");
JasperPrint jasperPrint = null;
//数据填充
jasperPrint = JasperFillManager.fillReport (jasperPath,null,connection);
//目标pdf的路径
String pdfPath = "f:\\test.pdf";
//打印pdf
JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
- javabean数据源式填充数据
在field新建,然后拖拽,详细java代码
String jrxmlPath ="F:\\Users\\lin\\IdeaProjects\\gitdemo1\\health_parent\\itext\\src\\main\\resources\\demo2.jrxml";
String jasperPath = "F:\\Users\\lin\\IdeaProjects\\gitdemo1\\health_parent\\itext\\src\\main\\resources\\demo.jasper";
JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);
Map paramter = new HashMap();
List<Map> list = new ArrayList<>();
Map map1 = new HashMap();
map1.put("id",1L);
map1.put("name","tianyi");
Map map2 = new HashMap();
map1.put("id",2L);
map1.put("name","lier");
list.add(map1);
list.add(map2);
JasperPrint jasperPrint = JasperFillManager.fillReport (jasperPath,paramter,new JRBeanCollectionDataSource(list));
String pdfPath = "f:\\test.pdf";
JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);