当前位置:网站首页>Jasperreports - print PDF (project tool)

Jasperreports - print PDF (project tool)

2022-06-26 10:45:00 Slightly drunk CC

【 Statement 】

  1. The material used in this article comes from the Internet ;
  2. This article is for learning purposes only ;
  3. Please refer to the blogger for details –> strive4free

One 、 Introduce

1.1 JasperReports principle

  JasperReports Is a powerful 、 Flexible report generation tool , Able to display rich page content , And turn it into PDF,HTML, perhaps XML Format . The library consists entirely of Java It's written in , Can be used in a variety of Java Applications , Include J2EE,Web Generate dynamic content in the application . In general ,JasperReports Will combine Jaspersoft Studio( Template designer ) Use export PDF report form .

[ remarks : The picture is from the wisdom podcast ]

Module name explain
JRXML Report filling template , The essence is a xml file
Jasper from JRXML Binary file compiled from template , For code filling data
Jrprint When the data is filled Jasper Object generated after , Used to output reports
Exporter Report output management class , You can specify the format of the report to be output
PDF/HTML/XML Report form

1.2 Development process

Use JasperReports export pdf report form , The development process is as follows :

  1. Make report template
  2. Template compilation
  3. Structural data
  4. Fill in the data
  5. The output file

1.3 Template designer Jaspersoft Studio

  Jaspersoft Studio Is a graphical report design tool , It's very convenient to design PDF Report template file ( It's really just a xml file ), combining JasperReports Use , You can render PDF file .

1.4 After designing the template, you can fill in the data

1.4.0 Add dependency

<!-- jasperreports rely on ( export PDF) -->
<dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>6.10.0</version>
    <exclusions><!--  Built in dependent itext There is no... In the central warehouse , Need to be excluded  -->
        <exclusion>
            <groupId>com.lowagie</groupId>
            <artifactId>itext</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!--mysql  Dependence -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>
<!--junit4  Dependence -->
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.13</version>
  <scope>test</scope>
</dependency>

1.4.1 Design jrxml Template file

 Insert picture description here
[ remarks : The picture is from the wisdom podcast ]
 Insert picture description here
[ remarks : The picture is from the wisdom podcast ]

[ remarks : The picture is from the wisdom podcast ]

1.4.2 JDBC Fill data in data source mode

It will be designed demo1.jrxml Copy the file to the of the current project resources Under the table of contents ; Write unit tests ;

@Test
public void testReport_JDBC() throws Exception{
    
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/kanganhealth","root", "root");//jrxml Template file path ( Designed in advance )
    String jrxmlPath = "D:\\ideaProjects\\projects111\\jasperreports_test\\src\\main\\resources\\demo1.jrxml";
    // from JRXML Binary file compiled from template , For code filling data ( Automatically created when file does not exist )
    String jasperPath = "D:\\ideaProjects\\projects111\\jasperreports_test\\src\\main\\resources\\demo1.jasper";// Compiling templates 
    JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);// Structural data 
    Map paramsMap = new HashMap();
    paramters.put("company","demo Test cases ");// Fill in the data --- Use JDBC Data source filling 
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperPath,paramsMap,connection);
    // The output file 
    String pdfPath = "D:\\test.pdf";
    JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
}

Output effect :
 Insert picture description here
[ remarks : The picture is from the wisdom podcast ]

1.4.3 JavaBean Fill data in data source mode

Write unit test method output PDF file

@Test
public void testReport_JavaBean() throws Exception{
    
    String jrxmlPath = "D:\\ideaProjects\\projects111\\jasperreports_test\\src\\main\\resources\\demo2.jrxml";
    String jasperPath = "D:\\ideaProjects\\projects111\\jasperreports_test\\src\\main\\resources\\demo2.jasper";// Compiling templates 
    JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);// Structural data 
    Map paramters = new HashMap();
    paramters.put("company","demo Testing company ");List<Map> list = new ArrayList();
    Map map1 = new HashMap();
    map1.put("tName"," Induction medical package ");
    map1.put("tCode","RZTJ");
    map1.put("tAge","18-60");
    map1.put("tPrice","500");Map map2 = new HashMap();
    map2.put("tName"," Sunshine parents elderly health examination ");
    map2.put("tCode","YGBM");
    map2.put("tAge","55-60");
    map2.put("tPrice","500");
    list.add(map1);
    list.add(map2);// Fill in the data --- Use JavaBean Data source filling 
    JasperPrint jasperPrint = JasperFillManager
    							.fillReport(jasperPath,paramters,new JRBeanCollectionDataSource(list));
    // The output file 
    String pdfPath = "D:\\test.pdf";
    JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
}

The test results

[ remarks : The picture is from the wisdom podcast ]

原网站

版权声明
本文为[Slightly drunk CC]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170528547277.html