2

I am facing a very strange issue, I am trying to send the PDF file as attachment from my struts application using below code,

        JasperReport jrReport = (JasperReport) JRLoader.loadObject(jasperReport);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jrReport, parameters, dataSource);
        jasperPrint.setName(fileNameTobeGivenToExportedReport);
        response.reset();
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileNameTobeGivenToExportedReport + ".pdf" + "\"");
        response.setHeader("Cache-Control", "private");
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

but the PDF that is being downloaded is coming with no data, means it is showing the blank page.

When in the above code I added the below line to save the PDF file in my D: directory

File pdf = new File("D:\\sample22.pdf");
JasperExportManager.exportReportToPdfStream(jasperPrint, new FileOutputStream(pdf));

The file that is getting generated is proper, mean with all the data. One thing that I noticed that the file that is downloading from browser and "sample22.pdf" have same size.

I read an article that says that it might be an issue with server configuration as our server might be corrupting the output stream. This is the article that I read Creating PDF from Servlet.

This article says

This can happen when your server flattens all bytes with a value higher than 127. Consult your web (or application) server manual to find out how to make sure binary data is sent correctly to the browser.

I am using struts 1.x, jBoss6, iReport 1.2

6
  • You have answered the question yourself (by the way: you're quoting from an illegal copy of my book). You can check your own findings by downloading the PDF file and by looking at the bytes that follow the stream keywords. If you don't see any binary data (illegible stuff), but instead a bunch of question marks or white space, then your PDF got corrupted somewhere in the process. Commented Jan 9, 2015 at 14:15
  • @BrunoLowagie Can you be more clear, I don't think I get you properly. It would be very helpful. Thanks
    – Vicky
    Commented Jan 9, 2015 at 15:44
  • I need screen shots to explain. I'll add them in an answer. Commented Jan 9, 2015 at 16:02
  • While I think @BrunoLowagie's answer is likely correct, there is another possibility: Some browsers in some versions have been known to not properly process streams without a Content-Length header given beforehand. The way the OP is streaming into response.getOutputStream() does not allow for such a header to be generated (without server-side buffering that is).
    – mkl
    Commented Jan 9, 2015 at 17:01
  • Yes, @mkl is right. The best way to rule this out, is by testing the application with different browser/viewer combinations. Commented Jan 9, 2015 at 17:07

1 Answer 1

8

Suppose that you have a simple "Hello World" PDF document:

enter image description here

When you open this document, you see that the file structure uses ASCII characters, but that the actual content of the page is compressed to a binary stream:

enter image description here

You don't see the words "Hello World" anywhere, they are compressed along with the PDF syntax that contains info needed to draw these words on the page into this stream:

xœ+är
á26S°00SIá2PÐ5´ 1ôÝBÒ¸4<RsròÂó‹rR5C²€j@*\C¸¹ Çq°

Now suppose that a process shave all the non-ASCII characters into ASCII. I've done this manually as you can see in the next screen shot:

enter image description here

I can still open the document, because I didn't change anything to the file structure: there is still a /Pages three with a single /Page dictionary. From the syntactical point of view, the file looks OK, so I can open it in Adobe Reader:

enter image description here

As you can see, the words "Hello World" are gone. The stream containing the syntax to render these words were corrupted (in my case manually, in your case by the server, or by Struts, or by whatever process you are using that thinks you are creating plain text instead of a binary file).

What you need to do, is to find the place where this happens. Maybe Struts is the culprit. Maybe you are (unintentionally) using Struts as if you were creating a plain text file. It is hard to tell remotely. This is a typical problem caused by a configuration issue. Only somebody with access to your configuration can solve this.

5
  • it is really helpful, at least I can reach to the root cause of issue. I will surely check this out and update you. Thanks..
    – Vicky
    Commented Jan 10, 2015 at 12:01
  • I compared both files and the same is happening as you shown above. But can you give me any idea, which property/configuration should I check or try to add in struts/jBoss configuration file ? it will be very helpful. Thanks.
    – Vicky
    Commented Jan 12, 2015 at 5:40
  • The best way to debug this, is by creating the most simple example you can imagine (one that doesn't involve iText). For instance: create a Servlet that sends a binary file to the browser. Execute this example and check where it goes wrong. Once you've fixed the simple example, you've fixed the iText example. (This may require you to create a new question with totally different tags; iText shouldn't be one of those tags.) Commented Jan 12, 2015 at 6:54
  • Bruno, with this PDF generation I have one more method which is generating excel sheet and that is working fine. The issue with the pdf is with pdf only.
    – Vicky
    Commented Jan 12, 2015 at 7:50
  • That is strange. What could be the difference? Have you asked JasperSoft? Maybe they're doing something strange in the PDF creation process. Commented Jan 12, 2015 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.