1

We recently integrated jasper report to our system. Now we can run report and show result to users with jasper report via standard way, run and show result to user.

But in some cases we need to save result to an object storage and show user when requested, as a document. As far as I know JasperPrint is a serialized object. saving report result to object storage as a serialized object is not a good way as we experienced our prior report tool. if the object changed the serialization mechanism could not deserialize object.

So we want to save jasper result in xml format to object storage but we couldn't find any way to show exported xml in JRViewer.

Is there any way to convert exported xml to a visual form?

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
mfidan
  • 647
  • 5
  • 19
  • Why not just save the PDF file and open it when needed? Also, do you restart the application between the report generation and the show report to the user? If not you can just store the report in a temp static variable. – Mateus Viccari Jan 11 '16 at 10:12
  • main reason not to prefer pdf is file size. for an 5 page report saving jasper as an xml with compression is about 5k but it is far to big with pdf. Our application is enterprised and creator and viewer wouldn't be the same user. – mfidan Jan 11 '16 at 10:25
  • I did some tests here and the smallest size i could get was saving in pdf... Even saving in XML or jrprint resulted in a bigger file size than PDF. Maybe you could provide the jrprint somehow? – Mateus Viccari Jan 11 '16 at 10:43
  • without compression you are right but compression changes sizes. but the difference in size is acceptable between pdf and xml. thanks for advice. i will do it with pdf unless an answer comes. in this case i still need a viewer for pdf. actually i want to show in standard viewer. – mfidan Jan 11 '16 at 11:33

1 Answers1

1

Jasper Report provides two method's for saving and loading your filled report, JasperPrint (note: excluding export's to other formats as pdf,xls ecc, since it would be very difficult to load and export to another format).

With courtesy of @Robert Mugattarov, What is the difference between JasperReport formats?

.jrprint is a serialized JasperPrint object i.e. an actual report instance i.e. a template that has been filled with data. This file can be deserialized back into a JasperPrint object.

.jrpxml is a human readable XML represenatation of a JasperPrint object i.e. an XML version of a template that has been filled with data. This file can be unmarshalled back into a JasperPrint object.

Since you do not wish do have a serialized object, the solution that remains is the jrpxml format in .

Example of saving and loading to jrpxml.

//Save JasperPrint to jrpxml (xml format)
JRXmlExporter exporter = new JRXmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleWriterExporterOutput(new File("myJasperPrint.jrpxml")));
exporter.exportReport();

//Load jrpxml to JasperPrint object
JasperPrint print = JRPrintXmlLoader.load("myJasperPrint.jrpxml");

//To show it in JasperViewer
JRViewer jrv = new JRViewer(print);

If you need to reduce file size I suggest that you zip/unzip the jrpxml file.

What is a good Java library to zip/unzip files?

Community
  • 1
  • 1
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109