Jump to content

Problem in XLS and PDF export


dnvsrikanth

Recommended Posts

Hi,

 

I am using JasperReports to generate reports in my project. I will generate the report using an OutputStream and display it in a JSF page.

 

I use JR version 2.0.5 and Apache Trinidad 1.0.5 in my application.

 

The problem here is exporting to excel and pdf formats are not working properly. When I export to an excel file i see all junk data in the file/stream. And when I export it to pdf the report is blank without any data.

 

Its working fine when I export the report to html, rtf and xml formats

 

Please help me out in this regard.

 

Thanks ,

DNV Srikanth.



Post Edited by dnvsrikanth at 06/23/2009 10:03



Post Edited by dnvsrikanth at 06/23/2009 10:17
Link to comment
Share on other sites

  • Replies 16
  • Created
  • Last Reply

Top Posters In This Topic

In the pdf export, the usual reason of a blank page is the integrity of the datasource passed to the report. Check if you're actually passing a good datasource (object integrity and content), and check the "When no data type" section in your report.You can also try, just to see the result, with a JREmptyDataSource object instead of your datasource. Then we'll see what we can do :)For the xls file, I'm sorry but I cannot help you, I've never tried to export in xls.

 

Regards,

Morgan

Link to comment
Share on other sites

  • 2 weeks later...

Hi,

 

Not sure what you mean by "i see all junk data in the file/stream". You mean the propert Excel viewer is not opening and you end up with a text viewer displaying binary content?

What about PDF. Do you see the PDF viewer opening? Or nothing opens and you end up with a blank browser page?

 

Thanks,

Teodor

 

Link to comment
Share on other sites

Hi Teodard,

 

When I run the report in a web application these problems occur. I am trying to display the report in a JSF page

 

So when I try to export the page to pdf stream it is displaying blank pdf stream and the excel stream displays all junk data.

 

DNV Srikanth.

Link to comment
Share on other sites

Hi,

 

This is the code I am using to export the report to pdfstream:

From the JSF page am passing a parameter to Java Generator Class and exporting the report based on the parameter passed.

 

DNV Srikanth

Code:
if(formatType.equals("pdf")){JRExporter exporter = null;exporter = new JRPdfExporter();response.setContentType("application/pdf");response.setHeader("Content-Disposition","inline;filename=\""+reportName+".pdf\"");exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());exporter.exportReport();}
Link to comment
Share on other sites

Hi,

 

I'm afraid this is a bit too simplistic. Delivering PDF to the browser is a little more tricky.

Some browsers (including IE in your screenshot) have to know before hand how many bytes are going to be delivered, before even starting to send those bytes through the response output stream.

This means that you would have to measure the length of the PDF file before sending it to the browser.

Streams have to be closed properly and stuff like that.

So I encourage you to take a look at the PdfServelet class that we ship as part of the JR library. You might even want to use it. Check the /demo/samples/webapp sample inside the JR project to see how.

The source of this servlet is here and you can see what we are doing in order to ensure PDF will come out nicely on all browsers:

http://jasperforge.org/scm/viewvc.php/trunk/jasperreports/src/net/sf/jasperreports/j2ee/servlets/PdfServlet.java?root=jasperreports&view=markup

 

I hope this helps.
Teodor

 

Link to comment
Share on other sites

Hi

 

Thank you, I'll check the link provided and implement as you have mentioned. But the code I have mentioned worked properly. But later logging was implemented in the application and everything was fine except these exports.

 

Anyhow I'll refer the link mentioned.

 

And this is the code am using for excel stream:

 

 

 

Thanks,

DNV Srikanth

Code:
if(formatType.equals("excel")) {            ServletOutputStream  outputStream= response.getOutputStream();            exporter = new JExcelApiExporter();            ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();            response.setContentType("application/xls");            response.setHeader("Content-Disposition", "inline; filename=\""+reportName+".xls\"");            exporter.setParameter(JExcelApiExporterParameter.JASPER_PRINT, jasperPrint);            exporter.setParameter(JExcelApiExporterParameter.OUTPUT_STREAM, xlsReport);            exporter.setParameter(JExcelApiExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.FALSE);            exporter.setParameter(JExcelApiExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);            exporter.setParameter(JExcelApiExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.TRUE);            exporter.setParameter(JExcelApiExporterParameter.IS_FONT_SIZE_FIX_ENABLED, Boolean.TRUE);            exporter.setParameter(JExcelApiExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);            exporter.exportReport();                        byte bytes[] = xlsReport.toByteArray();            response.setContentLength(bytes.length);            outputStream.write(bytes, 0, bytes.length);            outputStream.flush();            outputStream.close(); }
Link to comment
Share on other sites

  • 3 months later...

Hi

I have found the problem. As I have said earlier some code change to implement logging creted this mess and I reverted that change by reverting that change> I haven't made changes to the exporting code snippet whatever I have posted earlier in this thread.

 

Thanks,

DNV Srikanth.

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