下载时空白PDF [英] Blank PDF while downloading

查看:189
本文介绍了下载时空白PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临一个非常奇怪的问题,我正在尝试使用以下代码从我的struts应用程序发送PDF文件作为附件,

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());

但正在下载的PDF没有数据,意味着它显示空白页面。

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

在上面的代码中,我添加了以下行,将PDF文件保存在我的D:目录中

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));

生成的文件是正确的,意味着包含所有数据。有一件事我注意到从浏览器下载的文件和sample22.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.

我读了一篇文章说它可能是服务器配置的问题,因为我们的服务器可能会破坏输出流。这是我读过的文章从Servlet创建PDF

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.

本文说


当您的服务器使所有字节的值大于127时,可能会发生这种情况。请参阅您的Web(或应用程序)服务器手册,了解如何确保二进制数据正确地发送到浏览器。

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.

我正在使用struts 1.x,jBoss6,iReport 1.2

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

推荐答案

假设您有一个简单的Hello WorldPDF文档:

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

当您打开此文档时,您会看到文件结构使用ASCII字符,但页面的实际内容已被压缩到二进制流:

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:

你没有在任何地方看到Hello World字样,它们与PDF语法一起被压缩,其中包含在这些字词上绘制这些字所需的信息。页面到此流:

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°

现在假设一个进程剃须将所有非ASCII字符转换为ASCII。我已经手动完成了这个操作,你可以在下一个屏幕截图中看到:

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:

我仍然可以打开文档,因为我没有更改任何文件结构:还有一个 / Pages 三个带有一个 / Page 字典。从语法的角度来看,文件看起来还不错,所以我可以在Adobe Reader中打开它:

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:

如您所见,Hello World字样消失了。包含渲染这些单词的语法的流已损坏(在我的情况下是手动,在您的情况下由服务器,或由Struts,或您正在使用的任何认为您正在创建纯文本而不是二进制文件的进程)。

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

您需要做的是找到发生这种情况的地方。也许Struts是罪魁祸首。也许你(无意中)使用Struts就像创建一个纯文本文件一样。很难远程讲述。这是配置问题导致的典型问题。只有有权访问您配置的人才能解决此问题。

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.

这篇关于下载时空白PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆