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.