Why does generated PDF display properly in Interne

2019-05-26 13:40发布

问题:

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 and Chrome display gibberish, like this:

%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

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

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:

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. I get a popup window in IE that says:

Do you want to open or save this file:

Name: PrintedSchedule.pdf Type: ADobe Acrobat Document

But FireFox thinks it's an 'htm' document, and gives this:

You have chosen to open:

PrintedSchedule.pdf which is a: HTML Document

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.

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.

回答1:

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



回答2:

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



回答3:

I have answer for this ,I sucessed by opening .PDF file in new tab,From developemnt perspective ON CLICK either button or link target should be balnk e.g(target=_blank)