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 jasperreports-4.0.0.jar to the eXo Platform classpath. You can download it here: http://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 /lib 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.

01-xjasper1

02-xjasper2

03-xjasper3

  • After we have the Node Type definition, we need to create a template for this exo:event Node Type. Select “Templates” then “Add Template”.

04-xjasper4

  • Label it as Event, grant permissions to all and select exo:event as the Name.

05-xjasper5

06-xjasper6

07-xjasper7

Take note of these lines:

and

These define how we call our REST service /rest/private/pdf/export/event/${nodeId}, which will retrieve the event data via the {nodeId} parameter and launch the generation of PDF files. We will go through this REST service in detail in Section 5.

08-xjasper8

  • Save your template.

To see our result, let’s create a new Event content using our newly created Node Type.

09-xjasper9

You should see the link in the bottom right-hand corner that will enable you call the REST service.

10-xjasper10

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 exo:event Node Type. The input fields are the title, location, start and end date, summary, and the content of the event:

https://raw.github.com/exo-addons/resource-center/master/event-nodetype-and-jasper-report/event-report.xml.

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 (dev-monit/jasper 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.

11-xjasper11

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 ).

12-xjasper12

The REST service needs to be able to retrieve the PDFService.grs with this one: https://raw.github.com/exo-addons/resource-center/master/event-nodetype-and-jasper-report/PDFService.grs

The URL to call the service was defined in the previous section: /rest/private/pdf/export/event/${nodeId}

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 nodeId 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.

13-xjasper13

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!

Be part of the discussion, share your comments

comments

Keep in touch with the author

Tags: , , , , ,