Generate Jasper Reports from your entities/beans/POJOs.
Report UI Add-on provides an easy way to render JasperReports in Vaadin applications through DynamicJasper.
Say, you have the following domain/entity/Java Bean class:
public class Call {
private Long id;
private String client;
private String phoneNumber;
private City city; // enum
private LocalDateTime startTime;
private Integer duration;
private Status status; // enum
... getters and setters ...
}
You can create a new report and add it to any Vaadin layout as follows:
PrintPreviewReport<Call> report = new PrintPreviewReport<>(Call.class);
report.setItems(Repository.findAll());
layout.addComponent(report);
You can optionally set the order of the columns as follows:
PrintPreviewReport<Call> report = new PrintPreviewReport<>(Call.class, "client", "city", "phoneNumber", "startTime", "duration", "status");
If you prefer, you can use the default constructor to avoid creating the columns automatically. In that case, you can use the getReportBuilder()
method to set the columns manually:
PrintPreviewReport<Call> report = new PrintPreviewReport<>();
report.getReportBuilder()
.setMargins(20, 20, 40, 40)
.setTitle("Call report")
.addAutoText("For internal use only", AutoText.POSITION_HEADER, AutoText.ALIGMENT_LEFT, 200, headerStyle)
.addAutoText(LocalDateTime.now().toString(), AutoText.POSITION_HEADER, AutoText.ALIGNMENT_RIGHT, 200, headerStyle)
.addAutoText(AutoText.AUTOTEXT_PAGE_X_OF_Y, AutoText.POSITION_HEADER, AutoText.ALIGNMENT_RIGHT, 200, 10, headerStyle)
.setPrintBackgroundOnOddRows(true)
.addColumn(city = ColumnBuilder.getNew()
.setColumnProperty("city", City.class)
.setTitle("City")
.build())
.addGroup(new GroupBuilder()
.setCriteriaColumn((PropertyColumn) city)
.build())
.addColumn(ColumnBuilder.getNew()
.setColumnProperty("client", String.class)
.setTitle("Client")
.build())
.addColumn(ColumnBuilder.getNew()
.setColumnProperty("phoneNumber", String.class)
.setTitle("Phone number")
.build())
.addColumn(ColumnBuilder.getNew()
.setColumnProperty("startTime", LocalDateTime.class)
.setTitle("Date")
.setTextFormatter(DateTimeFormatter.ISO_DATE.toFormat())
.build())
.addColumn(ColumnBuilder.getNew()
.setColumnProperty("startTime", LocalDateTime.class)
.setTextFormatter(DateTimeFormatter.ISO_LOCAL_TIME.toFormat())
.setTitle("Start time")
.build())
.addColumn(ColumnBuilder.getNew()
.setColumnProperty("duration", Integer.class)
.setTitle("Duration (seconds)")
.build())
.addColumn(ColumnBuilder.getNew()
.setColumnProperty("status", Status.class)
.setTitle("Status").build());
See the DynamicJasper documentation for more configuration examples.
You can get a StreamResource
for downloading the report in various formats. For example:
StreamResource streamResource = report.getStreamResource(
"report.pdf", itemsSupplier, PrintPreviewReport.Format.PDF);
Anchor pdf = new Anchor(streamResource, "Download PDF");
Apache POI is required when using some of the exporting methods:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.10-FINAL</version>
</dependency>
In order to render charts you have to configure an ImageServlet
. For example:
@WebServlet("/report-image")
public static class ReportsImageServlet extends ImageServlet {
}
You can configure the URL pattern using the setImageServletPathPattern
method (default to report-image?image={0}
).