Buscar este blog

sábado, 21 de abril de 2018

JasperReports Servers - Remote XML Datasource - Quick start guide

This is a quick-quick start post about how to configure a Jasper Report in JasperReports Server, which will obtain its data from a remote xml file.

Installation

For JasperReports Server I downloaded TIB_js-jrs-cp_6.4.3_bin.zip. This is the manual installation in which you deploy the Jasper War inside an application server, an you need a database already installed. In my case:
  • Application Server: Jboss EAP 6.4
  • Database: Postgresql 9.4
I just followed JasperReports-Server-CP-Install-Guide.pdf, placed inside docs subfolder.

For Jaspersoft Studio I downloaded TIB_js-studiocomm_6.5.1.final_windows_x86_64.zip. This is de "portable" version.

The report

My report will show the data readed from a remote XML placed in https://www.w3schools.com/xml/simple.xml. It containts the following structure:
<breakfast_menu>
 <food>
  <name>Belgian Waffles</name>
  <price>$5.95</price>
  <description>
   Two of our famous Belgian Waffles with plenty of real maple syrup
  </description>
  <calories>650</calories>
 </food>
 <food>
  <name>Strawberry Belgian Waffles</name>
  <price>$7.95</price>
  <description>
   Light Belgian waffles covered with strawberries and whipped cream
  </description>
  <calories>900</calories>
 </food>
 <food>
  <name>Berry-Berry Belgian Waffles</name>
  <price>$8.95</price>
  <description>
   Light Belgian waffles covered with an assortment of fresh berries and whipped cream
  </description>
  <calories>900</calories>
 </food>
 <food>
  <name>French Toast</name>
  <price>$4.50</price>
  <description>
   Thick slices made from our homemade sourdough bread
  </description>
  <calories>600</calories>
 </food>
 <food>
  <name>Homestyle Breakfast</name>
  <price>$6.95</price>
  <description>
   Two eggs, bacon or sausage, toast, and our ever-popular hash browns
  </description>
  <calories>950</calories>
 </food>
</breakfast_menu>

The first step is to create a new JasperReports Project.


Next, you create a new report. In my case, by using de Blank A4 template.



In the final creation step you need to create a new Data Adapter. The selected type must be XML document.


When configuring the data adapter, set the file URL and check de "Use de report Xpath expression when filling the report", this is the simplest way to do.

In the next step of the wizard, select the fields you want to show in your report.


Now, you will have these fields available in the design view. Drag an drop then to the Detail section of the report.

Once all fields are in place, and after some few adjustments, you can preview the result.

The publish

From Jaspersoft Studio you can publish your reports directly to the JasperReports Server. First create a the connection with the server.


As you can see, the report will be placed under /reports/Pruebas/SimpleRemoteReport.

You also will can find this report in JasperReport server web console.

The printing

In order to print this report, I will use the Rest API. It is documented in JasperReports-Server-REST-API-Reference.pdf document.

First, the login:
D:\PortableApps\curl-7.46.0-win32-mingw\bin>curl -v "http://localhost:8080/jasperserver/rest/login?j_username=jasperadmin&j_password=jasperadmin"
*   Trying ::1...
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /jasperserver/rest/login?j_username=jasperadmin&j_password=jasperadmin HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.46.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Pragma: No-cache
< Cache-Control: no-cache
< Expires: Thu, 01 Jan 1970 01:00:00 CET
< P3P: CP="ALL"
< Set-Cookie: JSESSIONID=RsozmojD0KbbqmQbeIYhgQVB; Path=/jasperserver
< Content-Type: text/xml;charset=UTF-8
< Content-Length: 0
< Date: Sat, 21 Apr 2018 17:30:22 GMT
<
* Connection #0 to host localhost left intact

When you request the report, you need to add an extra param called net.sf.jasperreports.xml.source, which references the absolute URL of the remote XML file.
D:\PortableApps\curl-7.46.0-win32-mingw\bin>curl -v --cookie "JSESSIONID=RsozmojD0KbbqmQbeIYhgQVB;"  "http://localhost:8080/jasperserver/rest_v2/reports/reports/Pruebas/SimpleRemoteReport.pdf?net.sf.jasperreports.xml.source=https://www.w3schools.com/xml/simple.xml" -o myfile.pdf
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying ::1...
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /jasperserver/rest_v2/reports/reports/Pruebas/SimpleRemoteReport.pdf?net.sf.jasperreports.xml.source=https://www.w3schools.com/xml/simple.xml HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.46.0
> Accept: */*
> Cookie: JSESSIONID=RsozmojD0KbbqmQbeIYhgQVB;
>
< HTTP/1.1 200 OK
< Server: Apache-Coyote/1.1
< Pragma: No-cache
< Cache-Control: no-cache
< Expires: Thu, 01 Jan 1970 01:00:00 CET
< P3P: CP="ALL"
< Content-Disposition: attachment; filename="SimpleRemoteReport.pdf"
< output-final: true
< Content-Type: application/pdf
< Content-Length: 1788
< Date: Sat, 21 Apr 2018 17:51:46 GMT
<
{ [1788 bytes data]
100  1788  100  1788    0     0   1569      0  0:00:01  0:00:01 --:--:--  1569
* Connection #0 to host localhost left intact

An "myfile.pdf" will be the final PDF.