Documentation versions (currently viewingVaadin 7)

Jasper reports on Vaadin sample


I meet JasperReports some years ago and I liked this report library; this year I did need to implement a report on a personal project using Vaadin, but surprisingly I was not able to found a sample of this, so I did this little sample and article.

First, you will need a JDK Maven and Mysql in order to try the sample, and you can download the code here:

There is a README.txt file you can follow in order to run the sample, basically you need to:

  1. Create database running resources/database.sql on Mysql or MariaDB

  2. Compile the entire project: run "mvn install”.

  3. Deploy the application in Jetty: run "mvn jetty:run"

  4. Go to http://localhost:8080/  in your browser


Let’s see the sample code step by step.
The data is only a person table with some data.
The main class has two UI components (the report generating button and a list component used to show current data in database.):

final Button reportGeneratorButton = new Button("Generate report");
layout.addComponent(new PersonList());

The list is implemented on, I am using a FilteringTable (, that loads the data using a Vaadin SQLContainer:

SQLContainer container=null;
TableQuery tq = new TableQuery("person", new ConnectionUtil().getJDBCConnectionPool());
container = new SQLContainer(tq);
filterTable = buildPagedTable(container);

And the SQLContainer is provided with a JDBCConnectionPool created from a properties file (resources/

Properties prop=PropertiesUtil.getProperties();
public JDBCConnectionPool getJDBCConnectionPool(){
JDBCConnectionPool pool = null;
try {
  pool = new SimpleJDBCConnectionPool(
} catch (SQLException e) {
return pool;

The report generation is implemented on ReportGenerator class, this class loads the report template:

File templateFile=new File(templatePath);
JasperDesign jasperDesign = JRXmlLoader.load(templateFile);

Compile report template:

jasperReport = JasperCompileManager.compileReport(jasperDesign);

Fill report with data:

HashMap fillParameters=new HashMap();
JasperPrint jasperPrint = JasperFillManager.fillReport(

Export the jasperPrint object to Pdf format:

JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));

And finally execute all the logic to generate the report and sent it to an OutputStream:

JasperDesign jasperDesign=loadTemplate(templatePath);
JasperReport jasperReport=compileReport(jasperDesign);
JasperPrint jasperPrint=fillReport(jasperReport, conn);
exportReportToPdf(jasperPrint, outputStream);

But all the logic at is called from the ReportUtil class, this class is the responsible to connect Vaadin layer with ReportGenerator layer. There are two methods: the first one is prepareForPdfReport, this method creates a database connection, generates the report as a StreamResource (calling the another method) and finally extends the source button with a FileDownloader component in order to upload the generated report stream, so all the uploading magic is done by FileDownloader extension (

Connection conn=new ConnectionUtil().getSQLConnection();
StreamResource myResource =createPdfResource(conn,reportTemplate,reportOutputFilename);
FileDownloader fileDownloader = new FileDownloader(myResource);

The second method createPdfResource, uses ReportGenerator class in order to return the generated report as a StreamResource:

return new StreamResource(new StreamResource.StreamSource() {
  public InputStream getStream () {
    ByteArrayOutputStream pdfBuffer = new ByteArrayOutputStream();
    ReportGenerator reportGenerator=new ReportGenerator();
    try {
      reportGenerator.executeReport(baseReportsPath+templatePath, conn, pdfBuffer);
    } catch (JRException e) {
    return new ByteArrayInputStream(
}, reportFileName);

So, in order to call the report generator process when only need to call ReportUtil like we did in ‘’:

final Button reportGeneratorButton = new Button("Generate report");
new ReportsUtil().prepareForPdfReport("/reports/PersonListReport.jrxml",

Finally, the jasper report design can be found in the WEB-INF/personListReport.jrxml file

This is a picture of the sample running and the generated report:

And that’s all, I expect to help someone with this sample, thanks for reading.