Google

Jul 11, 2013

JasperReports generating a PDF file


This is an extension to the JasperReport step by step tutorial to demonstrate how to generate a PDF file from the Main.java. To generate a PDF file you  need the iText.jar as a dependency. Here are the Maven dependency files required.

  
<!-- Jasper reports -->
<dependency>
 <groupId>net.sf.jasperreports</groupId>
 <artifactId>jasperreports</artifactId>
 <version>5.0.1</version>
</dependency>
<dependency>
 <groupId>org.codehaus.groovy</groupId>
 <artifactId>groovy-all</artifactId>
 <version>2.1.1</version>
</dependency>
<dependency>
 <groupId>com.lowagie</groupId>
 <artifactId>itext</artifactId>
 <version>2.1.7</version>
</dependency>


Next, the Main.java that writes the output to a PDF file.

  
package com.mycompany.app.jasper;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRPdfExporterParameter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class Main
{
    
    public static JasperDesign jasperDesign;
    public static JasperPrint jasperPrint;
    public static JasperReport jasperReport;
    public static String reportTemplateUrl = "person-template.jrxml";
    
    public static void main(String[] args) throws IOException
    {
        try
        {
            InputStream resourceAsStream = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream(reportTemplateUrl);
            //get report file and then load into jasperDesign
            jasperDesign = JRXmlLoader.load(resourceAsStream);
            //compile the jasperDesign
            jasperReport = JasperCompileManager.compileReport(jasperDesign);
            
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            OutputStream outputfile = new FileOutputStream(new File("c:/temp/person.pdf"));
            
            //fill the ready report with data and parameter
            jasperPrint = JasperFillManager.fillReport(jasperReport, getParameters(),
                    new JRBeanCollectionDataSource(
                            findReportData()));
            
            //coding for PDF
            JRPdfExporter exporterPdf = new JRPdfExporter();
            exporterPdf.setParameter(JRPdfExporterParameter.JASPER_PRINT, jasperPrint);
            exporterPdf.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, output);
            exporterPdf.exportReport();
            outputfile.write(output.toByteArray());
            
        }
        catch (JRException e)
        {
            e.printStackTrace();
        }
    }
    
    private static Collection<Person> findReportData()
    {
        //declare a list of object
        List<Person> data = new LinkedList<Person>();
        Person p1 = new Person();
        p1.setFirstName("John");
        p1.setSurname("Smith");
        p1.setAge(Integer.valueOf(5));
        data.add(p1);
        return data;
    }
    
    private static Map<String, Object> getParameters()
    {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("footerText", "Just to demonstrate how to pass parameters to report");
        return params;
    } 
}

If you run the above class, a PDF file named person.pdf will be generated under c:\temp folder.

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home