gpt4 book ai didi

ruby-on-rails - 在 IE7 中使用 Prawn 生成 Rails PDF

转载 作者:行者123 更新时间:2023-12-04 02:17:20 24 4
gpt4 key购买 nike

我正在使用 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])

我的 Controller 中包含以下内容:

 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 文件名。

知道什么可能导致这种行为吗?

谢谢!

最佳答案

我也遇到这个问题了。当我尝试在 Internet Explorer(7 或 8)上请求不使用 SSL 的相同 PDF 时,它可以工作,但如果我使用 SSL 请求它,它就不起作用...

我们认为我们可能已经追踪到 IE 在下载 PDF 时所期望的 header 。我还没有检查 prawnto 源代码来查看它设​​置了哪些 header ,但我们可能会使用一些 Rack Middleware 来注入(inject)我们需要的 header :

# 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

关于ruby-on-rails - 在 IE7 中使用 Prawn 生成 Rails PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1574108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com