我正在使用 Prawn 和 Prawnto 在 Ruby on Rails 应用程序(Rails 版本 2.2.2)中生成 PDF,该应用程序运行良好,可以愉快地生成 PDF 并将其发送给用户以在 Firefox 中下载。
问题出在IE7上。
我有一条路线设置如下:
map.invoice_pdf '/invoices.pdf', :controller => 'invoices',
:action => 'index', :format => 'pdf'
然后我有一个像这样的链接可以调用:
invoice_pdf_path(:year => params[:year], :month => params[:month],
:unpaid_only => params[:unpaid_only])
我的控制器中有以下内容:
def index
params[:year] = default params[:year]
params[:month] = default params[:month]
params[:page] ||= 1
@invoices = Arobl.find_invoices_for_customer(current_customer.strCustomerID,
params)
respond_to do |format|
format.html{ render :action => 'index' }
format.pdf{
prawnto :inline => false, :filename =>
"#{current_customer.strCustomerID}_invoice.pdf"
end
在 FF 中,这按预期工作,当单击链接时,将使用 .pdf 格式调用显示操作,并以正确命名的 PDF 进行响应。当使用 IE7 时,它会说找不到文件或网站,并引用“invoices.pdf”而不是预期的 customer_id_invoice.pdf 文件名。
知道什么可能导致这种行为吗?
Thanks!
我也有这个问题。当我尝试在 Internet Explorer(7 或 8)上请求没有 SSL 的相同 PDF 时,它可以工作,但如果我使用 SSL 请求它,它就不起作用...
我们认为我们可能已经追踪到 IE 在下载 PDF 时所期望的标头。我还没有检查 prawnto 源代码来查看它设置了什么标头,但我们可能会使用一些 Rack Middleware 来注入我们需要的标头:
# add headers for PDF downloads in IE
# PDFs not downloading correctly via SSL in IE
# solution: add some headers for PDF downloads
# http://marc.info/?l=php-general&m=124301243808544&w=2
class RackAddPdfHeadersForIe
def initialize( app )
@app = app
end
def call( env )
@status, @headers, @body = @app.call env
add_headers if is_pdf? and is_internet_explorer?
[@status, @headers, @body]
end
def is_pdf?
@headers['Content-Type'] =~ /pdf/
end
def is_internet_explorer?
@headers['User-Agent'] =~ /MSIE ([0-9]{1,}[\.0-9]{0,})/
end
def add_headers
@headers['Content-Description'] = 'File Transfer'
@headers['Content-Transfer-Encoding'] = 'binary'
@headers['Expires'] = '0'
@headers['Pragma'] = 'public'
end
end
于是我尝试了一下,以为可以,结果发现确实还是不行。
所以我最终这样做了,无论出于什么原因,这对我有用:
class ReportsController < ApplicationController
def payroll_summary
respond_to do |format|
format.pdf do
response.headers['Content-Disposition'] = "attachment;filename=\"#{action_name}.pdf\""
response.headers['Content-Description'] = 'File Transfer'
response.headers['Content-Transfer-Encoding'] = 'binary'
response.headers['Expires'] = '0'
response.headers['Pragma'] = 'public'
render
end #format.pdf
end #respond_to
end #payroll_summary
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)