在Rails中强制内联渲染PDF文档 [英] Forcing the inline rendering of a PDF document in Rails

查看:97
本文介绍了在Rails中强制内联渲染PDF文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一项从一组XML文件生成PDF文件的服务. PDF正确生成.但是,每次我单击查看PDF"链接时,浏览器都会要求用户下载PDF文件.

I'm writing a service that generates PDF files from a set of XML files. The PDF is being correctly generated. However, everytime I click on the "view PDF" link, the browser asks the user to download the PDF file.

我需要PDF才能内联显示,就像任何常规HTML页面一样.我虽然写的代码正确,但是必须缺少一些内容-浏览器不断要求用户下载.

I need the PDF to display inline, just like any regular HTML page. I though I wrote the code right, but something must be missing - the browser keeps asking the user to download.

这是当前代码:

class PdfController < Controller
  def generate
    # stuff
    send_data pdf_bytes, :disposition => 'inline', :type => 'application/pdf'
  end
end

有什么想法吗?

推荐答案

尝试完全删除Content-Disposition标头.根据我的经验,Content-Disposition: attachment可以很好地工作,但是对于其他任何值,许多浏览器的行为都不一致.如果要显示内联,最好删除标题并希望获得最好的显示. IE似乎与此标头最多的问题. (惊讶,令人惊讶.)只需确保您仍在设置Content-Type: application/pdf.

Try removing the Content-Disposition header altogether. It's been my experience that Content-Disposition: attachment works pretty well, but many browsers have inconsistent behavior for any other value. If you want to display inline, it might just be better to remove the header and hope for the best. IE seems to have the most problems with this header. (Surprise, surprise.) Just make sure you're still setting Content-Type: application/pdf.

另一种选择是使用iframe并将iframesrc设置为PDF文件.几乎所有支持嵌入式PDF查看的浏览器都可以正确处理此问题.缺点是,您最终可能会显示空白的iframe,而不受支持的浏览器原本会在仅下载PDF的情况下进行了适度的回退.

The other option would be to use an iframe and set the src of the iframe to your PDF file. Almost all browsers that support inline PDF viewing will handle this correctly. The downside is that you might end up displaying a blank iframe whereas non-supported browsers would have otherwise done a graceful fallback to simply downloading the PDF.

这篇关于在Rails中强制内联渲染PDF文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆