Jump to content
Changes to the Jaspersoft community edition download ×

Charts not displayed in HTML reports


gkari

Recommended Posts

Dear all,

We are aware that this problem has been discussed on this forum before. However, we have not been able to find its solution despite trying every proposed solution.

All other formats except HTML generate charts perfectly fine. However, the HTML report just shows up a blank image.

We use JSF+Icefaces+Glassfish.

This is what we have in  our java file:

exporter = new JRHtmlExporter();
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
exporter.setParameter(JRHtmlExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.TRUE);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image=");
exporter.setParameter(JRHtmlExporterParameter.IMAGES_DIR_NAME, "/<webappname>/images/");
exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.TRUE);  
session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrintObj);

Following is the web.xml setting.

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

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

If someone can provide pointers on what we are missing here, we would appreciate that very much.

Thanks,
GK

 

 

Link to comment
Share on other sites

  • Replies 22
  • Created
  • Last Reply

Top Posters In This Topic

Hi Lucian,

These are the only steps we use:

1. Configuring the image servlet in the web.xml as follows:

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

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

2. Setting the jasperObj against the DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE attribute as follows:

HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);

session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrintObj);

Am I missing something?

Regards,

GK

Link to comment
Share on other sites

Your configuration is inconsistent.  The image servlet is mapped to /reports/image, but IMAGES_URI points to ../servlets/image.  The IMAGES_URI parameter should point to the image servlet.

Also, don't use IMAGES_DIR_NAME and IS_OUTPUT_IMAGES_TO_DIR in a web application, that might interfere with IMAGES_URI and the image servlet.

Regards,

Lucian

Link to comment
Share on other sites

Hi Lucian,

Thank you for your response. This is how my code looks now:

web.xml mapping

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

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

source code

            HttpSession session = (HttpSession)FacesContext.getCurrentInstance().getExternalContext().getSession(true);

            Map imageMap = new HashMap();
            session.setAttribute("IMAGES_MAP", imageMap);
            session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrintObj);

            exporter = new JRHtmlExporter();
            exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
            exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imageMap);
            exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/reports/image?image=");    

Despite these changes, the chart does not get populated. Am I missing something?

Regards,

GKAri

 

Link to comment
Share on other sites

Hi,

 

You don't need to use the IMAGES_MAP export parameter either.

Please check the /demo/samples/webapp sample shipped with the JR project source files.

If you do exactly what has been done there, it would work for you the same way.

 

I hope this helps.
Teodor

 

Link to comment
Share on other sites

Sorry to jump in on this topic, but did the OP ever find a solution? I'm having the exact same issue. I've copied my code below, but it is the same as the what was posted, and nearly identical to the sample HTML servlet provided in the demo. Also, in the html page that gets displayed, if I copy the image location of the broken image, the output I paste is the correct path to the image file.

 

Exporter parameters:

                List jasperPrintList = BaseHttpServlet.getJasperPrintList(request);
        if (jasperPrintList == null)
       {
            throw new ServletException("No JasperPrint documents found on the HTTP session.");
        }
            JasperPrint jasperPrint = (JasperPrint)jasperPrintList.get(0);
            JRHtmlExporter exporter = new JRHtmlExporter();

            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);                       
            request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
            exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image?image=");

 

Web XML:

 

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

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

Link to comment
Share on other sites

Hi,

 

I still suspect there is something wrong with the IMAGES_URI parameter.

You have provided a path that is relative to the URL that delivers the HTML.

How do you know the image URL you copy from the report is good. Does it load a valid image when pasted in the browser's URL address by itself?

Do you mean the exact same image URL does not work when in the report, but works when loaded directly in the browser?

What is this image URL? What is the URL of the servelet or JS that delivers the HTML report?

 

Thank you,
Teodor

 

Link to comment
Share on other sites

Well, it could be that the problem is caused by the relative path, but I'm not sure how to fix it. In my report, I copied the image location, which is saved on my hard drive. So the URL for the image isomething like C:\Users\Reports\Images. If I pasted that into my browser, it added the "file:///" prefix to the URL, and the image loaded fine.

From the sounds of it, the problem could be caused by how I'm saving my chart files. When I generate the reports and charts, they are saved to another location on my local machine. Would this stop the HTML exporter from being able to access them?

Link to comment
Share on other sites

Hi,

 

russ_87,

What you are saying is a different story. This thread was about how to deliver the HTML report directly to the browser. This is why there was a need for the image servlet.

But you say you actually save your HTML on disk. This is not something we would recomend people to do in Web applications and I'm not going to comment further on that.

I would only point out that you don't need any image servlet anymore, since you just need to export your report to a HTML file on disk. The images will be saved in a separate folder that accompanies your main HTML, just as it would work when you save an HTML page from the browser on your local computer.

Instead of the image servlet parameter, you would need to use the IMAGES_DIR parameter, to tell the exporter where to put the images. But again, I would not do things like that because this is not scalable. What if several users run the same report at the same time? Are you going to override files?

 

I hope this helps.
Teodor

 

Link to comment
Share on other sites

This is only going to highlight how ignorant I've been of the process, but actually, that was what I was trying to do.

When I originally started working with JasperReports, for a school project, we were generating the reports and saving them to the hard disk. I was able to successfully get them to save the reports, charts included, when I was doing it that way. As an extension of the project, we've been converting it to a web application. This includes delivering the reports directly to the browser, either as PDF or HTML. The PDF reports work fine, I'm able to get them to display in the browser properly, chart images included. However when I follow the same steps for HTML, the problem I described above persists.

The URL for the html page is http://localhost:8080/STATWeb/STAT/HtmlServlet

It's on that html page that the chart shows as a broken image, and when I right click on it and select "Copy Image Location" it shows as the path to my local disk. Obviously I'm not doing something correctly when trying to have the HTML exporter access the images, but I'd appreciate a nod in the right direction.

Link to comment
Share on other sites

Ok, so I think I'm on the path to a solution, but I'm still not sure on how to correct it. I was reading up on the subject here:

http://www.ibm.com/developerworks/websphere/library/techarticles/0411_olivieri/0411_olivieri.html

In the article, this mentions how to add images to your report. The sample code and this line:

"In this example, the <imageExpression> sub-element uses a java.io.File object to locate the image file that needs to be loaded."

makes me think that the problem is how I'm adding the images to the report design. I'm using java code to do it, and here is the code that I use to add an image. Now, what I'm thinking is that the problem is caused by the fact that I'm setting the expression to a string, when it needs to be a file in order for it to be mapped properly. Any thoughts as to whether or not I'm on the right track? I can't find a way to set the expression to the File class and still have the report compile properly.

 

Code:
            JRDesignImage image = new JRDesignImage(jDesign);            JRDesignExpression expression = new JRDesignExpression();            image = new JRDesignImage(jDesign);            image.setVerticalAlignment(JRDesignImage.VERTICAL_ALIGN_MIDDLE);            image.setY(imageDesignY);            image.setHeight(350);            image.setWidth(500);            image.setHorizontalAlignment(JRDesignImage.HORIZONTAL_ALIGN_CENTER);              image.setScaleImage(JRDesignImage.STRETCH_TYPE_RELATIVE_TO_BAND_HEIGHT);            image.setLazy(true);            expression.setValueClass(java.lang.String.class);            expression.setText(fileURL);            image.setExpression(expression);
Link to comment
Share on other sites

  • 3 months later...

 Hi All,

 
Sorry i am jumping into discussion in between, but issue is also look's similar so if any one can help I will greatly appreciate.
 
We have developed a dash board which we want to drill down functionality in web page. We have used JSP code for that which we got from one of Jasper blog. But code is compiling fine but when we tried to run the code its giving blank page with image alignment in web page. We have followed steps given in following link. It’s really looking good in blog. i think we have missed out some thing some where. Can any one please tell us where we are going wrong?
 
 
 
 
I have attached my jsp code and jasper files for your ref which i am using.
 
 
 
Thanks in Advance
 
Aezaz Ahmed
Link to comment
Share on other sites

  • 4 weeks later...

Why do we need to put the jasperPrint object in the ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE?

We already set it in the exporter. This is duplication,I think. Can the exporter itself set the session attribute, using the already known jasperPrint object? This will simplify a lot of things.

Link to comment
Share on other sites

Hi,

 

Kalin, if you don't see the relevance of using that session parameter, then this thread is probably not relevant to your current problem.

I suggest you create a new thread explaining what your problem is. Please make sure you provide enough detail about what you are doing and what does not work, attaching relevant files and all.

 

Thank you,

Teodor

 

Link to comment
Share on other sites

  • 4 weeks later...

 Hi

I have a slightly similar problem . My images are displayed but it looks like they are not up to date.  Let me explain  a little bit .  I have a portlet that sends parameters to a servlet that serves the report as an html. So far so good. The problem is that if I change the restriction ( where clause in the SQL ) I do not get the correct chart reported. The "funny" thing is that if I copy the source of the chart and open it in another tab THAT image is correct. Once this is done, simply requesting the report again shows the correct chart. I am using the built in ImageServlet (see code) : 

Is there anything I am missing ? Some parameter to force reloading of images maybe ? Attaching a screenshot of what I see. Using jasper reports 4 Any help would really be appreciated. TIA Marco
   

Code:
	<servlet>		<description>built in servlet needed for serving images in html reports</description>		<servlet-name>ImageServlet</servlet-name>		<servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>	</servlet>
Link to comment
Share on other sites

 Hi 

after posting I kept on reading the forum and found this thread ( http://jasperforge.org/plugins/espforum/view.php?group_id=102&forumid=103&topicid=26406 ) that rang a bell . 

The solution to my problem was storing the prints in different session objects and it works like a pie. 

 

I will need to implement a method that clears the current session or I will risk of filling it with useless garbage ... but that's another story.

Code:


Post Edited by mferretti at 02/03/2011 14:36
Link to comment
Share on other sites

  • 6 months later...

Hi,

I know this is an old post, but just trying to help some one like me, spending days trying jasperreports in JavaEE + JSF + Facelets.

Like Teodord said, maybe the problem is in IMAGE_URI. For me it was. The change i made in "webapp" sample was this line:

 

            exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "/MyProjectName-war/servlets/image?image=");

 

Replaced   "../servlets"   by   "/MyProjectName-war/servlets".

 

regards

Picanha

Link to comment
Share on other sites

  • 1 year later...

HI,

       i am very new to jasper and i have one Question regarding this discussion, Only for to generate chart in HTML, why we have to set web.xml? and 

       why we are using "exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "servlets/image?image=");" and                               "request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);" ??? because for PDF and Excel chart is coming directly.. why specially for html?? please give your valuable answers....

thanks in advance,

Chaitanya

 

 

Link to comment
Share on other sites

  • 10 months later...

Hello,

Sorry to post my question this thread because i didn't get any link to post my question. I am using Jasper report library version 5.6. In this version JRHtmlExporter is deprecated. I am using JasperRunManager().runReportToHtmlFile() to export my report from a servlet to browser. But images and charts are not getting displayed in the browser. With pdf it works fine. I tried with depricated classes and followed the above code, but still images are not displayed and UI looks more corrupt than the previous one with unloaded images symbol. Please could some help me with the better solution to this problem?.

Thanks in advance

Nitin

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...