Blank PDF while downloading

2019-01-28 21:00发布

问题:

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

回答1:

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

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:

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:

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:

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.