流式 PDF 在浏览器中以文本形式打开

发布于 2024-12-09 07:02:22 字数 2299 浏览 0 评论 0原文

我正在编写一个将 PDF 流式传输到浏览器的过滤器。但不提示打开 PDF。我得到的是浏览器窗口中显示的 PDF 内容,如下所示:

%PDF-1.4 %âãÏÓ 4 0 obj <>stream xKoÂ0ïû+æØJ±CGo<-¤£ªIIIJEüû:Qi9 V¾xvw>ÍÚ{ê+âp-Á\*&_ÑöÄï:'21Ñý- )½¾qÄÔµárÙÌç$¥ÇL÷¢wݵ·´Ô£Wש^¨ÐØæT&Bg$ L¨wmUÀ2á89ºÓÝ8ª¬Ø Å«C®Ó$O0ØEYQYå÷jÛä¿ó,Æ[ïéµDLÊ´@guj`íà .:ÜîOòky#Ü/ªMõÃ_å2Ù:tk¾ôýBµð}eÀÀ,X¨ñ P¾¡ú±zËÁãSo6ÄÔWà?²ë1!Zôs|fÉÉ@QÖ8WzÁ:±.?Îßæçô¾RuÛ endstream endobj 1 0 obj<>>>/MediaBox[0 0 595.28 841.88]>> endobj 3 0 obj<> endobj 2 0 obj<> endobj 5 0 obj<> endobj 6 0 obj<> endobj 7 0 obj<> endobj xref 0 8 0000000000 65535 f

我检查了响应标头,它们显示如下:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Disposition: attachment; filename=example.pdf
Connection: keep-alive
Keep-Alive: timeout=5
Accept-Ranges: bytes
Content-Type: application/pdf;charset=ISO-8859-1
Transfer-Encoding: chunked
Date: Thu, 13 Oct 2011 01:56:17 GMT

3db
%PDF-1.4
%âãÏÓ
3 0 obj <</Length 145/Filter/FlateDecode>>stream
xœMŽ1Â0…÷÷+ž›.ñ’&M\E‡B ƒ¸U+%µ´‹ßT¨È-÷½û8Þˆ}„0X­B`lpˆ¨0B”Þñ
Ãs¾wЮ7aƒ¢d7û=œÛ)N¥›1«ëÏz¢Æ+¿gjç
Û£¦±Œèoªi
½÷Ê:ÆëÓ=¥õ0¥fµ‰ÝR³ÂÁ¾(V
endstream
endobj
1 0 obj<</Parent 4 0 R/Contents 3 0 R/Type/Page/Resources<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]/Font<</F1 2 0 R>>>>/MediaBox[0 0 595.28 841.88]>>
endobj
2 0 obj<</BaseFont/Times-Bold/Type/Font/Encoding/WinAnsiEncoding/Subtype/Type1>>
endobj
4 0 obj<</Type/Pages/Count 1/Kids[1 0 R]>>
endobj
5 0 obj<</Type/Catalog/Pages 4 0 R>>
endobj
6 0 obj<</Producer(iText 2.0.8 \(by lowagie.com\))/ModDate(D:20111013145617+13'00')/CreationDate(D:20111013145617+13'00')>>
endobj
xref
0 7
0000000000 65535 f 
0000000227 00000 n 
0000000389 00000 n 
0000000015 00000 n 
0000000477 00000 n 
0000000527 00000 n 
0000000571 00000 n 
trailer
<</Root 5 0 R/ID [<f6f88fa439f0fb7ffd89bb5bcc013692><d7556c8d673d80e2889958cdface8dba>]/Info 6 0 R/Size 7>>
startxref
702
%%EOF

0

我已尝试:

  • 将内容处置设置为附件
  • 没有内容处置
  • 将 MIME 设置为应用程序/下载
  • 将 Cache-Control 设置为 maxage=1
  • 将 Pragma 设置为 public
  • 我还在 Tomcat web.xml 中设置了 MIME 类型
  • 创建了一个新的标准 Web 应用程序并使用了完全相同的代码,并且 PDF 生成顺利。

I am writing a Filter that streams PDF to the browser. But it is not prompting to open the PDF. What I am getting is the content of the PDF displayed in the browser window as below:

%PDF-1.4 %âãÏÓ 4 0 obj <>stream xKoÂ0ïû+æØJ±CGo<-¤£ªIIIJEüû:Qi9 V¾xvw>ÍÚ{ê+âp-Á\*&_ÑöÄï:'21Ñý- )½¾qÄÔµárÙÌç$¥ÇL÷¢wݵ·´Ô£Wש^¨ÐØæT&Bg$ L¨wmUÀ2á89ºÓÝ8ª¬Ø Å«C®Ó$O0ØEYQYå÷jÛä¿ó,Æ[ïéµDLÊ´@guj`íà .:ÜîOòky#Ü/ªMõÃ_å2Ù:tk¾ôýBµð}eÀÀ,X¨ñ P¾¡ú±zËÁãSo6ÄÔWà?²ë1!Zôs|fÉÉ@QÖ8WzÁ:±.?Îßæçô¾RuÛ endstream endobj 1 0 obj<>>>/MediaBox[0 0 595.28 841.88]>> endobj 3 0 obj<> endobj 2 0 obj<> endobj 5 0 obj<> endobj 6 0 obj<> endobj 7 0 obj<> endobj xref 0 8 0000000000 65535 f

I examined the response headers and they show as below:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Disposition: attachment; filename=example.pdf
Connection: keep-alive
Keep-Alive: timeout=5
Accept-Ranges: bytes
Content-Type: application/pdf;charset=ISO-8859-1
Transfer-Encoding: chunked
Date: Thu, 13 Oct 2011 01:56:17 GMT

3db
%PDF-1.4
%âãÏÓ
3 0 obj <</Length 145/Filter/FlateDecode>>stream
xœMŽ1Â0…÷÷+ž›.ñ’&M\E‡B ƒ¸U+%µ´‹ßT¨È-÷½û8Þˆ}„0X­B`lpˆ¨0B”Þñ
Ãs¾wЮ7aƒ¢d7û=œÛ)N¥›1«ëÏz¢Æ+¿gjç
Û£¦±Œèoªi
½÷Ê:ÆëÓ=¥õ0¥fµ‰ÝR³ÂÁ¾(V
endstream
endobj
1 0 obj<</Parent 4 0 R/Contents 3 0 R/Type/Page/Resources<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]/Font<</F1 2 0 R>>>>/MediaBox[0 0 595.28 841.88]>>
endobj
2 0 obj<</BaseFont/Times-Bold/Type/Font/Encoding/WinAnsiEncoding/Subtype/Type1>>
endobj
4 0 obj<</Type/Pages/Count 1/Kids[1 0 R]>>
endobj
5 0 obj<</Type/Catalog/Pages 4 0 R>>
endobj
6 0 obj<</Producer(iText 2.0.8 \(by lowagie.com\))/ModDate(D:20111013145617+13'00')/CreationDate(D:20111013145617+13'00')>>
endobj
xref
0 7
0000000000 65535 f 
0000000227 00000 n 
0000000389 00000 n 
0000000015 00000 n 
0000000477 00000 n 
0000000527 00000 n 
0000000571 00000 n 
trailer
<</Root 5 0 R/ID [<f6f88fa439f0fb7ffd89bb5bcc013692><d7556c8d673d80e2889958cdface8dba>]/Info 6 0 R/Size 7>>
startxref
702
%%EOF

0

I have tried:

  • Setting the Content-disposition to attachment
  • Without Content-disposition
  • Setting the MIME to application/download
  • Setting The Cache-Control to maxage=1
  • Setting the Pragma to public
  • I have also set the MIME type in the Tomcat web.xml
  • Created a new standard web-app and used the exact same code and it the PDF was generated without a hitch.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北渚 2024-12-16 07:02:23

在将响应写入输出流之前,您应该在 Servlet 中确保以下内容:

res.setContentType( "application/pdf" );
res.setHeader("Content-disposition",
                  "attachment; filename=" +
                  "Example.pdf" );

附件标记用于告知不要在浏览器中打开文档,但使用某些 PDF 阅读器应用程序(如 Adob​​e Acrobat)时

文件名是文件的默认名称被使用。

Below are the things that you should make sure is in your Servlet before writing response to outputstream :

res.setContentType( "application/pdf" );
res.setHeader("Content-disposition",
                  "attachment; filename=" +
                  "Example.pdf" );

attachment tag is used to tell that do not open document in browser but with some PDF Reader application (like Adobe Acrobat)

filename is default name of the file to be used.

鱼忆七猫命九 2024-12-16 07:02:23

就我而言,当使用 jasperreports 生成报告时会出现此问题。
我找到了一个解决方案设置此:

response.setContentType( "application/pdf" );

在设置此之前:

JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, new HashMap(), cn);

In my case, this problem raise when generate a report with jasperreports.
I find a solution setting this:

response.setContentType( "application/pdf" );

before setting this:

JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, new HashMap(), cn);
π浅易 2024-12-16 07:02:23

一个技巧是向以 .pdf 结尾的 URL 添加一个虚拟参数。

One trick is to add a dummy argument to the URL that ends with .pdf.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文