为什么生成的PDF在Internet Explorer中正常显示,而在FireFox或Chrome中却没有? [英] Why does generated PDF display properly in Internet Explorer but not in FireFox or Chrome?
问题描述
我使用lowagie / iTextPDF在Java中生成PDF并且在Firefox和Chrome中显示输出时出现问题,但IE8显示它们很好。
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.
Firefox和Chrome显示乱码,如下所示:
Firefox and Chrome display gibberish, like this:
%PDF-1.4% 20 obj<>streamx 1?B1C #@ ?0# ?Tm>}Zh nO?8 1 a ӓ ӓ $ :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预告片<] /信息6 0 R /规格7 >> startxref 697 %% EOF
%PDF-1.4 %���� 2 0 obj <>stream x���1B1C��#@�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
这是servlet代码的一部分:
Here's part of the servlet code:
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();
我已经尝试更新Firefox,更新PDF插件,并确保设置为使用Adobe的PDF查看器而不是Firefox的查看器。
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.
作为测试,我改变了这一行:
As a test, I changed this line:
来自:
response.setHeader("Content-disposition","inline; filename=PrintedSchedule.pdf");
收件人:
response.setHeader("Content-disposition","attachment; filename=PrintedSchedule.pdf");
Internet Explorer似乎认识到它是一个正在下载的PDF。我在IE中看到一个弹出窗口,上面写着:
Internet Explorer seems to recognize that it's a PDF that's being downloaded. I get a popup window in IE that says:
是否要打开或保存该文件:
Do you want to open or save this file:
姓名:PrintedSchedule.pdf
类型:ADobe Acrobat文件
Name: PrintedSchedule.pdf Type: ADobe Acrobat Document
但FireFox认为这是'' htm'文件,并给出:
But FireFox thinks it's an 'htm' document, and gives this:
您已选择开启:
PrintedSchedule.pdf
这是:HTML文档
PrintedSchedule.pdf which is a: HTML Document
在Firefox中我然后选择Adobe Reader打开它,和它运作正常。但是在标题栏中我注意到它在文件名的末尾添加了.htm。
In Firefox I then choose Adobe Reader to open it with, and it works OK. But In the title bar I noticed that it appended '.htm' to the end of the file name.
这可能是问题的线索吗?如果是这样,我如何强制Firefox将其识别为PDF?用户希望PDF在浏览器中显示,而不是在弹出窗口中显示,并且他们不希望单独下载PDF文件。
Could this be a clue as to the problem? If so, how can I force Firefox to recognize it as a 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.
推荐答案
每 http://itextpdf.com/examples/iia.php ?id = 173 您忘记设置内容长度,尝试在 ServletOutputStream之前添加
response.setContentLength(baos.size());
out = response.getOutputStream();
Per http://itextpdf.com/examples/iia.php?id=173 you are forgetting to set content length, try adding response.setContentLength(baos.size());
before ServletOutputStream out = response.getOutputStream();
这篇关于为什么生成的PDF在Internet Explorer中正常显示,而在FireFox或Chrome中却没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!