使用rails send_data发送PDF

时间:2014-04-15 12:38:15

标签: ruby-on-rails ruby redmine

我使用ruby 1.9.3和redmine 1.4.4

根据这个 - > Please help me send a jpg file using send_data,我在控制器中执行此操作:

  @file = temp.path
  File.open(@file, 'r') do |f|
    send_data f.read, :filename => "myfile.pdf", :type => "application/pdf", :disposition => "attachment"
  end
  File.delete(@file)

但它返回ArgumentError (invalid byte sequence in UTF-8),为什么?

1 个答案:

答案 0 :(得分:8)

PDF文件必须编码

 file = temp.path
 File.open(file, 'r') do |f|
   send_data f.read.force_encoding('BINARY'), :filename => filename, :type => "application/pdf", :disposition => "attachment"
 end
 File.delete(file)