Jump to content
Changes to the Jaspersoft community edition download ×

Trying to solve a export pdf problem


Nick

Recommended Posts

Hi guys! I've got a problem when I export a report to pdf in my web app. I've tried using two different codes:

The first code (copied from the samples in the JasperReports project home site) was:

Code:
File reportFile = new File(application.getRealPath("/reportes/reporteDeIngresosCiudad.jasper"«»));
JasperPrint jasperPrint = JasperFillManager.fillReport(new FileInputStream(reportFile), parameters, conn);
JRPdfExporter exporterPdf = new JRPdfExporter();
response.setContentType("application/pdf"«»);
ServletOutputStream outputStream = response.getOutputStream();
exporterPdf.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporterPdf.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
exporterPdf.exportReport();
outputStream.flush();
outputStream.close();

And the second code (copied from the forum and currently used) is:

Code:
[code]File reportFile = new File(application.getRealPath("/reportes/reporteDeIngresosCiudad.jasper"«»));
JasperPrint jasperPrint = JasperFillManager.fillReport(new FileInputStream(reportFile), parameters, conn);
byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);
response.setContentType("application/pdf"«»);
response.setContentLength(bytes.length);
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(bytes, 0, bytes.length);
outputStream.flush();
outputStream.close();

The codes works fine, but when the result pdf file has a small size (I think) the result page is blank.

Pd. I'm using: iReports 1.2.7; JDeveloper 10.1.2; Acrobat Reader 5.0; Internet Explorer 6

Please help me.

Link to comment
Share on other sites

  • Replies 7
  • Created
  • Last Reply

Top Posters In This Topic

Hi! Sorry, not sure about what you want to know. Like I said before, I use a outputStream to show the generated report in pdf format, because I don't want to create a file for each generated report (that's no efficient I think).

May I forgot saying that when I get a blank page (instead of my report), I press F5 button to refresh the blank page and the report appears, but that's not a the true solution, right? Anyway, that means that the jasper is compiled and generated correctly, but it is not showed when I export it.

Also, the report query can return from just one row to hundreds of rows. I'm almost sure that I have a blank page as result when the query returns a small number of rows, so the pdf result size is small too. I mean that the IExplorer or the Acrobat has a kind of bug when I try to show a small size file in a outputStream. May I have to set another parameter in the code posted before.

I'll appreciate your help!

Pd. So sorry about my english language.

Link to comment
Share on other sites

I guess you ran into the same problem than I did with Internet Explorer.

 

This Browser involves the file extension to recognize a specific file type. Which makes it difficult to get a pdf shown in the IE.

 

Usually you could set the Content-Disposition header to set the filetype that the browser suggests on saving the file.

 

But even then IE ignores it.

 

The solution:

name whatever URL you're calling to display the PDF as something ending with .pdf

 

I for myself renamed my Servlet producing the output as myServlet.pdf in the url-mapping in web.xml of the application. That worked.

 

-J

Link to comment
Share on other sites

Hi Nick,

 

Sorry for that.

Let me try to write down some concrete steps.

 

1) I assume you already have a web.xml because you want to have your Output from a Servlet.

Go into that web.xml.

You have defined your Servlet with the following tags:

 

<servlet>

<servlet-name>[YourServlet]</servlet-name>

<servlet-class>[YourServletClass]</servlet-class>

</servlet>

 

and mapped it to a URL like this:

 

<servlet-mapping>

<servlet-name>[YourServlet]</servlet-name>

<url-pattern>[YourServletURL]</url-pattern>

</servlet-mapping>

 

But Internet Explorer expects that the file that it should display has a special extension appropriate for the file type (MIME). In this case, when you want to display a PDF file, IE expects that the URL ends with ".pdf" indicating a PDF file.

 

What you have to do:

change that url-pattern thing into:

 

<servlet-mapping>

<servlet-name>[YourServlet]</servlet-name>

<url-pattern>[YourServletURL].pdf</url-pattern>

</servlet-mapping>

 

What it does:

You call your Servlet not by http://SERVER/YourServletURL anymore but by http://SERVER/YourServletURL.pdf

 

As this exact url-pattern is mapped to your Servlet name, your Servlet gets called correctly.

 

One more step:

 

In your service method of the Servlet you have to set the Content-Disposition header by calling:

 

response.setHeader("Content-Disposition", "inline; filename=SomeFilename.pdf").

 

Some browsers take this header as suggestion for a filename in case a user wants to save the file. This also presets the extension for that case.

Another Option would be to

 

response.setHeader("Content-Disposition", "attachment; filename=SomeFilename.pdf")

 

That forces the browser to show up a "Save as" Dialog proposing "SomeFilename.pdf" as filename.

 

I hope I could help you with this

 

-J

Link to comment
Share on other sites

Thanks for your replays jjjj and jcoto.

- I'm still trying to implement jjjj's solution cause I was using a jsp page to generate the pdf report. I'm trying use a servlet instead.

- I had already tried upgrading the AcrobatReader version. As you said jcoto, it works. But I have many users for my web application here in La Paz city and other cities too. So it's not a easy way to solve my problem.

Also, it seems JasperReports has a bug here because it depends of the user layer software.

Regards, Nick.

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...