Open Source Reporting Software Solution
By: Ruben Oganesyan
Tags: Ireport, Pdf, Rest, Jasper Reports, Java

Web and mobile developers often need to manage preparation of PDF pages for printing or (and) sending them to customers by email.

PDF files have full control over the text and graphics display on the page. Unfortunately, libraries for generating dynamically populating PDF files are not included in the standard tools of PHP, JS (Web), Java or Swift (Android and iOS, respectively). In this article, I want to tell you about the open-source solution for generating PDF files.

JasperReports is an open Java library for generating dynamically populated files. It has many tools for creating complex report forms, including PDF, but other formats are also available: RTF, DOCX, HTML, XLS, XLS, CSV, and XML. In other words, it is enough to develop one form, create one layout — and you can export it to any of the above formats.

There are also appropriate libraries, such as PDFLib (commercial version) for PHP and its open-source version PDFLib-Lite. However, the library is quite expensive, and the Lite version is distributed only in source code, and this restriction may become a problem when installing it in the development environment.

PDFbox is another open-source Java library for working with PDF documents. It allows you to create new PDF documents, manage existing documents with the ability to extract the content from them. But it doesn’t have an UI (User Interface), unlike JasperReports.

I think JasperReports is particularly useful in large projects related to reporting and not only in PDF format. It has everything you need to implement it in your project: simple creation of complex report forms, UI for convenient layout, server application, and simple integration with the frontend.

In this article I will cover the following topics:

Installing the development environment and server application.

Creating a PDF file that is automatically filled in from the database.

Integration of the server application with the frontend to get the created PDF.

To start using JasperReports in your project, you need to download two applications: JaspersoftStudio — we will call it the working environment — and JasperServer — we will call it the server application.

JaspersoftStudio is an Eclipse-based development environment with the built-in Java library JasperReports, where dynamic or static PDF files are developed: for example, tickets, receipts, contracts, analytical charts, and other.

JasperServer is a server application where files are uploaded from Jaspersoft Studio. They can be accessed from a mobile or web app. JasperServer has a UI that allows you to view reports, create accounts for different users, and grant them appropriate access. You can also set up an email sending (Scheduler).

Configuring the working environment and server application

You can download and install apps using the links above. After setting up two applications, you must establish a connection from the working environment to the server application.

First of all, you should attach the data source (in our case, PostgreSQL) to the working environment, where the PDF is going to take data. Then we will start developing our first PDF file.

Data Adapters → Create Data Adapter → Database JDBS Connection and specify the connection data:

JDBC Driver — PostgreSQL (org.postgresql.Driver). If there is no driver for your DBMS, you can install the necessary driver in the Driver Classpath tab. JDBC URL — it consists of the host, port, and DB name. Username и password for your database account.

Click on the already familiar Test button and, if the connection to the database is successful, Finish.

From this database, the PDF will be filled in the working environment. Since we are planning to upload our first PDF file to the server application as well, let’s attach the same data source to the server application:

Data Sources → Add Resource → Data Source and repeat everything from the point above.

Now we can start creating a PDF. Sources in JasperReports are stored in the JRXML (this is XML with sewn tags and attributes that the JasperReports API works with) format.

Click File → New → Jasper Report → Blank A4 → Specify the name of the JRXML file → Finish.

After creating a new project you see the following picture:

Seven different blocks — each block has its own behavior that differs from the others. You can read more about this in the documentation. By clicking on Source you can see the structure of these blocks:

So, let’s delete five extra blocks and leave only two: Title and Detail. The Delete (Windows) or Backspace (OS X) button will help us to do this.

Now let’s add two elements. There are two ways to add a new element: write a container in the XML structure (the Source button), or drag the desired element from the upper-right window Palette — Static Text, where will be field names and Text Field, inside which we will sew the field variables pulled from the database:

You can write a query in the database in the queryString tag, or click the DataSet and Query editor dialog button. This opens a new window where you need to select the data source (1), write a query (2), and declare the field variables. The Read Fields button (3) will read all fields automatically when a valid request is made. To view the data, click on the Data preview (4).

Great! We have got four fields with the String type, and now we can perform almost any manipulation with them. For example, we can just output them as a list and write a little logic.

Print the names of the required fields in Static Text elements and put them in the Title container. Specify the field variables in the Text Field elements in the Detail container, since they are going to multiply. Our PDF will output the name, city, and email address. To avoid being completely boring, let’s write a simple logic in the Text Field element, using the fourth field, the client’s gender, Sex.

Will do the following: if the client is a woman, in front of the name will add Mrs., if man Mr. To do this we use the Java ternary operator:

By clicking on Preview next to the Source button, you can see the result:

As you can see in the screenshot, the PDF was successfully assembled: it took all the values and applied the logic, correctly entering Mrs. and Mr.

Let’s also set the input parameter City, so that you can filter data by city. This can be done either by clicking Parameters → CreateParameter in the Outline window, or by adding a new parameter tag with the name and class attributes:

<parameter name="City" class="java.lang.String"/>

All that remains is to add the parameter to the SQL query:

Pass the City parameter the value San Francisco (I’ll show you how to do this in the next paragraph) and click Data Preview to view the result.

The PDF was collected after successfully filtering the data. Let’s go further.

Since we already have a dynamically filling PDF file, we can upload it to a server for further integration with our frontend applications. To do this, click on Publish Report to JasperReports Server → open the server by double-clicking → select the server folder where you want to upload the PDF (in our case reports) → Next → Data Source from Repository select the data source you have created earlier on the server application → Finish.

Integration with frontend

JasperReports API includes its own RESTful implementation for client-server interaction — REST v2. If it doesn’t suit you, you can use a simple object access protocol — SOAP.

We will consider REST v2.

There are four main methods available for CRUD (Create-Read-Update-Delete) actions: GET (get), POST (add, edit, delete), PUT (add, replace), DELETE (delete). All detailed information can be found in the documentation links above.

We will consider the more common and relevant GET method for this article.

http://<host>:<port>/jasperserver[pro]/rest_v2/reports/path/to/report.<format>?<arguments>

Above — synchronous request, with the help of which it is possible to get the output data of the file (ready PDF) in one request-response (asynchronous call is available for review here).

I think the host and port are clear, but /reports/path/to/report is the URI of the file being called. Since we have uploded the source of the PDF file (Example.jrxml) into the server reports folder, the filled URI variant will be: /reports/reports/Example.

format is a format (in our case PDF).

arguments are parameters.

Above we have added the City parameter, it will be passed in the query with the value of San Francisco to filter data for this city.

If the call is not made from an authorized zone, you need to add two more parameters/attribute: j_username and j_password (logpass for authorization). By default, login and password on the server is jasperadmin.

Thus, we get the following URL:

http://localhost:8080/jasperserver/rest_v2/reports/reports/Example.PDF?city=San Francisco&j_username=jasperadmin&j_password=jasperadmin &j_username=&j_password=

This way we will get the PDF already generated. For example, when you call up this URL through your browser address bar, the file should be downloaded automatically.

You may need to display the PDF as an image. For example, if the client just wants to view a file, you can show the document in PNG format, or if they want to download it, you can provide a PDF.

In the example of Java using the PDFbox library, let’s look at how you can generate and retrieve a PDF file from an external application and then convert it into a PNG.

Below is the PullPDF class with one method that accepts the URL as an argument.

You can get the same result using, for example, the Spring Framework. But I tried to show a universal method that can be used both in Android and the web when working with Java.

Conclusion

If you want to automate the generation of a simple receipt for an online store and you have limited time to create it, I recommend using native tools of your project or a familiar framework. Since it takes to time install JasperReports and familiarizing yourself with the documentation to develop more comprehensive reporting forms may not pay off. In other cases, JasperReports is a good open-source solution for automating reporting.