Search code examples
jspstruts2jasper-reportsdynamic-jasper

struts2, jsp project; show pdf


I am try to show an pdf in jsp page without saving this in the hard disk. I was able to generate a pdf and I tested it by saving it in hard disk. I am using DynamicJasper as reporting engine.

Here is my strut.xml for dynamic-jasper:

<action name="myJasperTest" class="com.via.qcm.view.JasperAction">
  <result name="success" type="dynamic-jasper">
    <param name="dynamicReport">DynamicReport</param>
    <param name="layoutManager">classic</param>
    <param name="parameters">dynamicReportDs</param>
    <param name="documentName">report</param>
    <param name="contentDisposition">application/download</param>
    <param name="format">PDF</param>
  </result>

I am generating pdf and saving it in ByteArrayOutputStream().

baos = new ByteArrayOutputStream();
//export to pdf
Exporter.exportToPdf(jp, baos);

Now my question is how to show this "baos" in webpage as pdf?


Solution

  • There are two problems:

    • you need to return a DynamicReport object, not an Byte Array;
    • the dynamicReport variable must start with a lowercase letter:

    In Struts.xml

        <param name="dynamicReport">dynamicReport</param>
    

    In the Action

    public DynamicReport getDynamicReport(){
        DynamicReport dynamicReport = null;
        // do all your stuff
        return dynamicReport;
    }