Jasper Report

Jasper report can help us to export a pdf file in Java. To make it simple, we will only focus on data source with JRBeanCollectionDataSource, and use only the Text Field, and Static Text basic elements to build this report.

Assume we have already created a jasper templatejapser_template.jasper. In this example, we avoid to use the database data source, and use a JRBeanCollectionDataSource. JRBeanCollectionDataSource would take a list of Java bean object, we can sort of use it like the returning list from a SQL query. Here is a simple code for doing so:

public class MyBean{
    private String item1;
    private String item2;
    
    public MyBean(String i1, String i2){
        item1 = i1;
        item2 = i2
    }
    
    public String getItem1(){return item1;}
    public String getItem2(){return item2;}
}

public class Test {
    public static void main(String[] args) throws Exception {
        FileInputStream main = new FileInputStream("./jasper_template.jasper");
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(main);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("param1", "Something...");
        params.put("param2", "Something Else...");
        
        List<MyBean> myList = new ArrayList<MyBean>();
        myList.add(new MyBean("1,1", "1,2"));
        myList.add(new MyBean("2,1", "2,2"));
        myList.add(new MyBean("3,1", "3,2"));        
        myList.add(new MyBean("4,1", "4,2"));
                
        JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(myList);
        
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, beanCollectionDataSource);
        byte[] pdfBytes = JasperExportManager.exportReportToPdf(jasperPrint);
        FileOutputStream out = new FileOutputStream("./out.pdf");
        out.write(pdfBytes);
        out.flush();
        out.close();
}

We use Jasper Studio to create a jasper template. Select a blank template, and then give your project a name.

Then select one empty record for data adapter.

In Column Header part, add two Static Text elements, and change their captions to anything you like. Then resize static text, and the column header height to make it looks nice.

Open Dataset and Query Dialog, and then go to Java Bean tab. In The bottom, open Fields tab (should be opened by default), add two fields. Name item1, and item2, which will be the java bean field names we use in the code.

Then go to the Parameters tab, add two parameters. We call it param1, and param2 to match our code. You can pass, add as many as you need in your case.

In the template, add two Text Field in the Detail part. For the first one, double click in it, and select the item1 fields we added previous. You will see it caption becomes $F{item1}. Likewise, do the same thing for item2.

For parameters, we can use Text Field to display them. Add a text field on the template, and double clicks on it. Select the param1 we added in the previous steps. You will see it caption become $P{param1}

Save the template, and compile it. You will notice that a new file with your template name and with extension .jasper is created. You can copy this file to your java project. In our code, we call this file jasper_template.jasper. Copy it to root direct to your code. Run the code, and there would be a out.pdf file created. The list of java bean should be displayed on such pdf file.

  • java/jasper_report.txt
  • Last modified: 2019/01/08 15:47
  • by chongtin