我使用lowagie/iTextPDF在Java中生成PDF,並且在Firefox和Chrome中顯示輸出時出現問題,但IE8顯示它們正常。爲什麼生成的PDF在Internet Explorer中正確顯示,但沒有在FireFox或Chrome中顯示?
Firefox和鉻顯示亂碼,這樣的:
%PDF-1.4%2 0 OBJ <>流x1B1C#@ 0#TM>}深航 nO?8 1 a غ y ӓB $ :9XC a./f { $ o - $?Ө^c/012'E j }WT/Y<? 5* endstream endobj 4 0 obj < >>>/MediaBox [0 0 612 1008]/Rotate 90 >> endobj 1 0 obj <> endobj 3 0目標<> endobj 5 0目標<> endobj 6 0目標<> 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
以下是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();
我已經嘗試過更新的Firefox,更新PDF插件,並使確定它被設置爲使用Adobe的PDF查看器而不是Firefox的查看器。
作爲一個測試,我改變了這一行:
來源:
response.setHeader("Content-disposition","inline; filename=PrintedSchedule.pdf");
要:
response.setHeader("Content-disposition","attachment; filename=PrintedSchedule.pdf");
Internet Explorer中似乎認識到,這是一個的被下載的PDF。我得到了IE中的彈出窗口,上面寫着:
你想打開或保存此文件:
名稱:PrintedSchedule.pdf 類型:使用Adobe Acrobat文檔
但Firefox認爲這是一個 'HTM' 的文件,並給出了這一點:
您已選擇打開:
PrintedSchedule.pdf 這是一個:HTML文檔
在Firefox我再選擇安裝Adobe Reader打開它,和它的作品確定。但在標題欄中,我注意到它在文件名的末尾添加了「.htm」。
難道這是線索問題?如果是這樣,我該如何強制Firefox將其識別爲PDF?用戶希望PDF在瀏覽器中顯示,而不是在彈出窗口中顯示,並且他們不希望單獨下載PDF文件。
請隨時檢查此:http://stackoverflow.com/questions/6089904/itext-generated-pdf-not-shown-correctly-in-chrome – NMALKO 2013-05-06 21:02:25
你有任何這個問題的現場例子,你可以鏈接到?如果firefox和chrome都不認爲它是PDF,那麼我猜測頭文件有問題。 – 2013-05-14 21:31:53
不幸的是,我沒有任何公開的示例。另外,我嘗試更改我的servlet代碼以完全匹配以下鏈接的關節,並且它不起作用。 http://itextpdf.com/examples/iia.php?id=173 – robz1330 2013-05-15 16:04:40