Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

java - Blank PDF while downloading

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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á2PD5′ 1?YBò?4<Rsrò?ó?rR5C2€j@*C?1 ?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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...