February 15th, 2009

Using JasperReports in your Flex Application

Last week, I came cross the Flash Viewer for JasperReports. JasperReports is an OpenSource Java Framework to build Reports based on different data sources. We’re using in our projects JasperReports to generate PDF reports, too.

The Flash Viewer Project is using the Flex SDK to render the JasperReports for the HTML or Web based application. All source files of the viewer are available, so that you can customize the viewer for your needs.

But you can’t replace a interactive Flex chart component with the report viewer, because the viewer only generates a static output of the report. You can also using the JFree Chart extensions of the JasperReports, but neither the Charts nor the Text Information are selectable. If you’re changing any data of the report, you have to fill the report again in your Java Backend and rerender the report with the viewer.

So, using the viewer make only sense if you using it as preview of the report or if your reports aren’t as complex and interactive. And the best thing about the viewer is that you can use the same source file for your web and print output.

Additional Infos:

To use the Flash Viewer, you have to setup a Java web server like (Tomcat…) and run the webapp of the Flash Viewer. You get the webapp, if you download the project zip of the Flash Viewer. To display a report, you have to compile the report definition (jrpxml file) and fill the the compiled report. After that, you can use the Flash Viewer to display the report. The project zip contains a sample report, which demostrates how to use the flash viewer.

15 Responses to “Using JasperReports in your Flex Application”

  1. Alfred says:

    Hi
    How use JasperReport Flash Viewer 3.0 in Liferay Portlet Flash??
    What program install in my computer?

    thank you
    Alfred

  2. Patrick says:

    Have you downloaded the compiled SWF File or the source code of the Flash Viewer. In the souce code, you can find a small demo of the viewer. The demos shows the necessary steps to use the Viewer. If you using the charting components of JasperReports, than you have to add the JFreeChart Libraries to the lib folder of your WebApp.

  3. Ryan Gardner says:

    It looks like the flash viewer reads in JRXML files and displays them – is this correct? (i.e. supply the viewer component a URL that is a jrxml file and it will view it)

    JRXML files I thought were the report templates – but is there a way to make JasperReports render the output of the report back into a jrxml file as static text?

    If it’s not jrxml – what format do you have jasper reports generate the report at with the URL you pass in from the flash component?

  4. Patrick says:

    No, the flash viewer doesn’t read the JRXML file. The viewer reads the internal xml fromat “jrpxml”. You get this internal format, if fill the compiled report file (.japser).

    The first two steps are the same as if you generate PDF with Japser Report or any other output format.

    1.Step: You have to compile the JASPER file from the JRXML file. You only have to execute this step once, after the creation of the report.

    2.Step: Fill the JASPER file with the data. As result of this action you get the JasperPrint object, which will stored in the session.

    Now you can use one of the exporters (HTML, PDF, … ) to export the filled report.

    The Flash Viewer calls the XMLServlet of Jasper Reports (net.sf.jasperreports.j2ee.servlets.XmlServlet). This Servlets genernates the JRPXML data by using the JRXmlExporter.

    If you want parameterize the report, than you have to do that in the second step. There you can pass the url into the report. To display the changes you have to export the JRPXML data that again.

  5. Raja S Patil says:

    In Flex builder I have created Flex Library project.
    Then Complied the lib by copying whole SRC folder to
    Projects src folder. It generated .swc file.
    Copied that .swc file to Fresh Test project’s libs.
    Dropped viewer component on Applications canvas.
    In onCreationComplete event handler assigned url to
    the .jrpxml file as follows.

    I have created .jrpxml file by exporting the filled
    print object as Nodata.jrpxml in WAMP’s www folder.
    In URL property I have given
    http://localhost/Nodata.jrpxml

    But it shows only viewer but not the report contents
    what might be wrong.

  6. Tamás AMbrits says:

    Hi Patrick!

    Please help me!
    I have a Flex applictaion. I use MySQL to store the data via PHP script. It’s working very good, but I need create report.
    I created a simple report in iReport. I can view it in the Flex via xViewer. (http://flex.org/software/component/xviewer). Generated the XML, put it into the viever, but I’m not understand, how can I connect my data with it.
    Can you help me how can I use the JasperReport, Flex and MySQL?

    Tnaks!

    Best
    Tamás

  7. Patrick says:

    Hi Tamás,
    you need a Java server (like a Tomcat) to run the webapp of the Flash Viewer. The webapp consists of the java logic to compiles and fills the reports. The project zip contains the webapp and a demo how to use the Flash Viewer.

  8. Patrick says:

    Hi Raja,
    the jrpxml is only the report definition. To display data, you have to compile and fill the report. The viewer displays only the generated data of the fill process and doesn’t use the jrpxml itself.

  9. Bob says:

    Could you please tell me how/or what I should look at to display a Jasper Report using the SWF ?
    I was able to download Flex and build the Jasper demo, no problems.
    I have an existing Tomcat web server, using precompiled Jasper (3.0) templates, and push data using Jsp for parameters and Struts, calling MySQL sps within the Templates. Currently the users get the Reports in PDF format.

  10. Nice design, what is the name of template ?

  11. Leisha Bossi says:

    Thank you for the Information.

  12. UGG says:

    Thank you for this sort of a great blog. Exactly where else could a single get this sort of details written in these kinds of an incite full way? I have a presentation that I am just now working on, and I have been searching for this sort of information.

  13. Wescley de Macedo says:

    Hi Patrick.

    I maked download of xViewer in this link http://www.adobe.com/cfusion/exchange/index.cfm?event=extensionDetail&extid=1384018 where haved a example, but when i copy the file jrvc.swc to my application happened this error when i try to use the viewer.

    Error: GraphicElement undefined
    at GraphicElement()[C:\Documents and Settings\victork\dev\eclipse\vk-flex-ws\xViewer\com\jaspersoft\reports\model\impl\GraphicElement.as:26]
    at com.jaspersoft.reports.model.impl::Rectangle()[C:\Documents and Settings\victork\dev\eclipse\vk-flex-ws\xViewer\com\jaspersoft\reports\model\impl\Rectangle.as:21]
    at com.jaspersoft.reports.viewer::ReportViewer/rectangle()[C:\Documents and Settings\victork\dev\eclipse\vk-flex-ws\xViewer\com\jaspersoft\reports\viewer\ReportViewer.mxml:224]
    at com.jaspersoft.reports.viewer::ReportViewer/showContent()[C:\Documents and Settings\victork\dev\eclipse\vk-flex-ws\xViewer\com\jaspersoft\reports\viewer\ReportViewer.mxml:182]
    at com.jaspersoft.reports.viewer::ReportViewer/showPageContent()[C:\Documents and Settings\victork\dev\eclipse\vk-flex-ws\xViewer\com\jaspersoft\reports\viewer\ReportViewer.mxml:169]
    at com.jaspersoft.reports.viewer::ReportViewer/showPage()[C:\Documents and Settings\victork\dev\eclipse\vk-flex-ws\xViewer\com\jaspersoft\reports\viewer\ReportViewer.mxml:160]
    at com.jaspersoft.reports.viewer::ReportViewer/init()[C:\Documents and Settings\victork\dev\eclipse\vk-flex-ws\xViewer\com\jaspersoft\reports\viewer\ReportViewer.mxml:672]
    at com.jaspersoft.reports.viewer::ReportViewer/___Canvas1_creationComplete()[C:\Documents and Settings\victork\dev\eclipse\vk-flex-ws\xViewer\com\jaspersoft\reports\viewer\ReportViewer.mxml:14]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::UIComponent/dispatchEvent()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\core\UIComponent.as:9156]
    at mx.core::UIComponent/set initialized()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\core\UIComponent.as:1167]
    at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\managers\LayoutManager.as:701]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at mx.core::UIComponent/callLaterDispatcher2()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8565]
    at mx.core::UIComponent/callLaterDispatcher()[E:\dev\3.1.0\frameworks\projects\framework\src\mx\core\UIComponent.as:8508]

    Haved the name of folder in your computer. Why i need to do?

    Thanks,

    Att,

    Wescley de Macedo

  14. ale says:

    hello people,

    need help with jasper report viewer, I can not connect to the servlet, according to the example is using the file parameters.jrpxml

    private function init():void{
    viewer.url = parameters.jrpxml;
    viewer.fetchSize = int(parameters.fetchSize);
    viewer.loadAndRefresh();
    }

    saludos.