Jasperreports Spring : Chart is not being displayed when report exported as HTML

0

Developed a Simple Spring Maven Web application with Jsperreports 6.1.0 dependency. Created a Jasper report having a static text and a chart. When exported report in PDF format, report is printed properly with static text and chart, but when exported to HTML format only static text is displayed not chart.

After searching on internet found that ImageServlet and few parameters are needed to export report in HTML format.

Added ImageServlet mapping in web.xml

Set Image URI through WebHtmlResourceHandler.

Still report does not show chart. What is the problem?

Here is my Spring Controller code to export 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>

Work Environment : Jasperreports 6.1.0, Spring 4.1.1, Eclipse Luna

vaidya.vallabh's picture
Joined: Apr 2 2013 - 4:10am
Last seen: 4 years 2 months ago

You can share code to export to PDF, with the new version 6.1, no longer works the routine that I have .

Thank you.

 

luisneiran - 3 years 11 months ago

1 Answer:

0

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

Actually My Controller has requestmapping as  "/reports" and my method from which I was exporting report has request mapping "/html",

so final request for image being displayed in chart was becoming something like "appname/reports/html/image?image=img_0_1".

But ImageServlet was mapped with URL "/image" so updated my code as below :

In web.xml

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

and in controller updated my url as ......

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

vaidya.vallabh's picture
Joined: Apr 2 2013 - 4:10am
Last seen: 4 years 2 months ago
Feedback
randomness