[英]Why does generated PDF display properly in Internet Explorer but not in FireFox or Chrome?
I'm generating PDFs in Java using lowagie/iTextPDF and am having a problem displaying the output in Firefox and Chrome, but IE8 displays them fine. 我正在使用lowagie / iTextPDF在Java中生成PDF,但是在Firefox和Chrome中显示输出时遇到问题,但是IE8可以正常显示它们。
Firefox and Chrome display gibberish, like this: Firefox和Chrome显示乱码,如下所示:
%PDF-1.4 % 2 0 obj <>stream x 1?B1 C # @ ?0# ?Tm>}Zh nO?8 1 a غ y ӓ?B $ :9XC a. /f{ $ o -$?Ө ^ c / 'E j }WT / Y< ? 5* endstream endobj 4 0 obj <>>>/MediaBox[0 0 612 1008]/Rotate 90>> endobj 1 0 obj <> endobj 3 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj xref 0 7 0000000000 65535 f 0000000379 00000 n 0000000015 00000 n 0000000467 00000 n 0000000211 00000 n 0000000530 00000 n 0000000575 00000 n trailer <]/Info 6 0 R/Size 7>> startxref 697 %%EOF %PDF-1.4% 2 0 obj <> streamx 1?B1C #@ ?0# ?Tm>}Zh nO?8 1 a B.B?B。$$:9XC.a./f{ $$o-$?Ө^^ c ./'E'j } WT。 / Y<? 5* endstream endobj 4 0 obj <>>> / MediaBox [0 0 612 1008] /旋转90 >> endobj 1 0 obj <> endobj 3 0 obj <> endobj 5 0 obj <> endobj 6 0 obj <> endobj外部参照0 7 0000000000 65535 f 0000000379 00000 n 0000000015 00000 n 0000000467 00000 n 0000000211 00000 n 0000000530 00000 n 0000000575 00000 n尾部<] / Info 6 0 R / Size 7 >> startxref 697 %% EOF
Here's part of the servlet code: 这是servlet代码的一部分:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document reportPDFDocument = (Document)generateReport(request,conn,baos);
PdfWriter.getInstance(reportPDFDocument, baos);
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentType("application/pdf");
response.setHeader("Content-disposition","inline; filename=PrintedSchedule.pdf");
ServletOutputStream out = response.getOutputStream();
baos.writeTo(out);
out.flush();
I've already tried updating Firefox, updating the PDF plugin, and making sure it was set to use Adobe's PDF viewer instead of Firefox's viewer. 我已经尝试过更新Firefox,更新PDF插件,并确保将其设置为使用Adobe的PDF查看器而不是Firefox的查看器。
As a test, I changed this line: 作为测试,我更改了这一行:
From: 从:
response.setHeader("Content-disposition","inline; filename=PrintedSchedule.pdf");
To: 至:
response.setHeader("Content-disposition","attachment; filename=PrintedSchedule.pdf");
Internet Explorer seems to recognize that it's a PDF that's being downloaded. Internet Explorer似乎认识到正在下载的是PDF。 I get a popup window in IE that says:
我在IE中看到一个弹出窗口,显示:
Do you want to open or save this file:
您要打开还是保存此文件:
Name: PrintedSchedule.pdf Type: ADobe Acrobat Document
名称:PrintedSchedule.pdf类型:ADobe Acrobat文档
But FireFox thinks it's an 'htm' document, and gives this: 但是FireFox认为这是一个“ htm”文档,并给出以下内容:
You have chosen to open:
您选择打开:
PrintedSchedule.pdf which is a: HTML Document
PrintedSchedule.pdf是:HTML文档
In Firefox I then choose Adobe Reader to open it with, and it works OK. 然后在Firefox中,我选择Adobe Reader打开它,然后它可以正常工作。 But In the title bar I noticed that it appended '.htm' to the end of the file name.
但是在标题栏中,我注意到它在文件名的末尾附加了“ .htm”。
Could this be a clue as to the problem? 这可能是这个问题的线索吗? If so, how can I force Firefox to recognize it as a PDF?
如果是这样,如何强制Firefox将其识别为PDF? The users want the PDF to display in the browser, not in a popup window, and they don't want to have to download the PDF files separately.
用户希望PDF在浏览器中显示,而不是在弹出窗口中显示,并且他们不想单独下载PDF文件。
Per http://itextpdf.com/examples/iia.php?id=173 you are forgetting to set content length, try adding response.setContentLength(baos.size());
对于http://itextpdf.com/examples/iia.php?id=173,您忘记设置内容长度,请尝试添加
response.setContentLength(baos.size());
before ServletOutputStream out = response.getOutputStream();
在
ServletOutputStream out = response.getOutputStream();
之前ServletOutputStream out = response.getOutputStream();
Try adding 尝试添加
response.setHeader("Expires", "0");
response.setHeader("Cache-Control","must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
// setting the content type
response.setContentType("application/pdf");
// the contentlength
response.setContentLength(baos.size());
// write ByteArrayOutputStream to the ServletOutputStream
OutputStream os = response.getOutputStream();
baos.writeTo(os);
os.flush();
os.close();
after closing document.close(); 关闭document.close()之后;
我对此有答案,我通过在新选项卡中打开.PDF文件成功完成了,从开发角度来看单击按钮或链接目标应为空白,例如(target = _blank)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.