How to Generate PDF Files From eXo Platform Node Types Using JasperReports
The eXo Platform’s web content management capabilities allow you to create content you can publish on your website or share on your intranet. The content is displayed via Groovy templates that depend on the content’s JCR Node Type. In order to share this content outside of the eXo Platform context, you need to export the content in a standard format.
In this tutorial, you will learn how to easily generate a PDF version of your content and make it available from both the front and the back end of your eXo Platform instance.
The PDF files will be generated with JasperReports, a popular open source reporting engine. Then, using the eXo Platform IDE, you will be able to quickly develop and deploy REST services to publish JasperReports within the platform.
In this tutorial, we will show you how to prepare a Node Type definition and generate PDF files from there. You can then generate PDF files from any other Node Types.
1. Prerequisites
Add the JasperReports library
to the eXo Platform classpath. You can download it here: https://sourceforge.net/projects/jasperreports/files/jasperreports/JasperReports%204.0.0/.JasperReports has some dependencies that are already available in eXo Platform; if you use the eXo Platform Tomcat bundle, you can simply drop the jar in the
folder.For the REST service part, make sure you have installed the eXo IDE extension following this documentation : InstallingExtensions.IDE.
2. Prepare a Node Type
To create a Node Type, follow these steps:
- Log in as Content Administrator user.
- Go to the Content Administration page.
- Select Repository, then Node Types.
- Please import the following Node Type: https://raw.github.com/exo-addons/resource-center/master/event-nodetype-and-jasper-report/event-nodetype.xml
- After we have the Node Type definition, we need to create a template for this Node Type. Select “Templates” then “Add Template”.
- Label it as Event, grant permissions to all and select
- Switch to the Dialog tab, then replace the default Dialog template with the one found here: https://raw.github.com/exo-addons/resource-center/master/event-nodetype-and-jasper-report/dialog1.gtmpl
- Switch to the View tab, then replace the default View template with the one found here: https://raw.github.com/exo-addons/resource-center/master/event-nodetype-and-jasper-report/view1.gtmpl
Take note of these lines:
def node = uicomponent.getNode() ; def nodeId = node.getUUID();
and
def node = uicomponent.getNode() ; def nodeId = node.getUUID();
These define how we call our REST service
, which will retrieve the event data via the parameter and launch the generation of PDF files. We will go through this REST service in detail in Section 5.- Switch to the CSS tab and add these two CSS templates: https://raw.github.com/exo-addons/resource-center/master/event-nodetype-and-jasper-report/Stylesheet-lt.css and https://raw.github.com/exo-addons/resource-center/master/event-nodetype-and-jasper-report/Stylesheet-rt.css
- Save your template.
To see our result, let’s create a new Event content using our newly created Node Type.
You should see the link in the bottom right-hand corner that will enable you call the REST service.
3. Creating a JasperReports XML Report Template
JasperReports is an open source reporting engine that allows you to produce documents that can be viewed, printed or exported in a variety of formats such as HTML, PDF, Excel, OpenOffice and Word. It uses an XML template to describe the exported document structure and data, and can use any data source provider.
The first step is to create the JasperReports XML report template (which can be done with iReport—JasperReports’ graphical designer). Here is an example of a report template for the
Node Type. The input fields are the title, location, start and end date, summary, and the content of the event:Don’t forget to set the “markup” option to “html” for any HTML parts, if you want JasperReports to interpret it.
Once the template is created, we need to upload it to the eXo Platform. Open your eXo IDE, go to the location of your choice (
in my case) and click on File > Upload… Tip: keep the file extension as .xml. This will allow you to edit it directly in the IDE.Select the report template, and leave the selected Mime Type. We are now ready to generate PDF files.
4. Writing the REST Service
We will now create the REST service, which will retrieve the event data and launch the generation of PDF files. Creating a REST service in eXo IDE is very easy. Click on the New button and select REST Service. This will generate a new REST service template that we can customize to fit our needs. The location of the REST service doesn’t matter. Save it with the name of your choice (in this example, we use
The REST service needs to be able to retrieve the https://raw.github.com/exo-addons/resource-center/master/event-nodetype-and-jasper-report/PDFService.grs
with this one:The URL to call the service was defined in the previous section:
After publishing the REST service, you can easily test it in the IDE. Click on the Launch button in the upper right-hand corner, replace the
parameter in the URL with an event ID of your choice and click Send. The response will be displayed in the IDE’s console.Below is the final result when you click on the link button Get as PDF of a particular Event content.
Questions? Remarks? Join the discussion on the eXo Community!
Make the most out of eXo Platform 4
Register to the next webinar and get a complete overview of what you can do with eXo Platform 4. Reserve your seat now!