Jasperreports Spring: chart not showing when report is exported as HTML

Developed Simple Spring Maven web application with Jsperreports 6.1.0 dependency. Created a Jasper report with static text and chart. When you export a PDF report, the report prints correctly with static text and chart, but when exporting to HTML format, only the static text is displayed without a chart.

After searching the web, I found that the ImageServlet and several parameters are required to export the report in HTML format.

Added mapping of ImageServlet to web.xml

Set image URI via WebHtmlResourceHandler.

The report does not display the chart yet. What is the problem?

Here is my Spring Controller code for exporting a report in HTML format.

List<BeanAuthorBooks> beanList = new ArrayList<BeanAuthorBooks>();
        beanList.add(new BeanAuthorBooks("APJ Kalam",10));
        beanList.add(new BeanAuthorBooks("Robin  Shamra",5));
        beanList.add(new BeanAuthorBooks("Rashmi Bansal",8));
        beanList.add(new BeanAuthorBooks("Dr. B.R.Ambedkar",60));
        beanList.add(new BeanAuthorBooks("Mahatma Gandhi",15));

        Map<String,Object> params = new HashMap();
        JasperReport jasperReport = JasperCompileManager.compileReport(this.getClass().getResourceAsStream("testreport.jrxml"));
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, new JRBeanCollectionDataSource(beanList,false));

        HtmlExporter exporter = new HtmlExporter();
        List<JasperPrint> jasperPrintsList = new ArrayList<JasperPrint>();
        jasperPrintsList.add(jasperPrint);
        exporter.setExporterInput(SimpleExporterInput.getInstance(jasperPrintsList));
        //set ImageHandler. Hack for images export to HTML
        SimpleHtmlExporterOutput output = new SimpleHtmlExporterOutput(response.getWriter());
        WebHtmlResourceHandler webHtmlResourceHandler =  new WebHtmlResourceHandler("image?image={0}");
        output.setImageHandler(webHtmlResourceHandler);           
        exporter.setExporterOutput(output);            
        SimpleHtmlReportConfiguration configuration = new SimpleHtmlReportConfiguration();
        exporter.setConfiguration(configuration);
        exporter.exportReport();

      

Here is my web.xml:

<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/image</url-pattern>
</servlet-mapping>

      

Runtime: Jasperreports 6.1.0, Spring 4.1.1, Eclipse Luna

NOTE. I found many links on different forums with the same problem, but the suggested solutions now seem out of date.

+3


source to share


1 answer


Found the problem ..... POsting solution here ....

Actually My Controller has requestmapping as "/ reports" and my method from which I exported the report has requestmapping as "/ html"

so the final request for the image displayed in the chart became something like "appname / reports / html / image? image = img_0_1".

But the ImageServlet was mapped to the url "/ image" so updated my code like below:



In web.xml

<servlet-mapping>
    <servlet-name>ImageServlet</servlet-name>
    <url-pattern>/reports/image</url-pattern>

      

and in controller updated my url like ......

WebHtmlResourceHandler webHtmlResourceHandler =  new WebHtmlResourceHandler("../image?image={0}");

      

0


source







All Articles