send PDF with rails send_data

EloHailwidis picture EloHailwidis · Apr 15, 2014 · Viewed 14.8k times · Source

I use ruby 1.9.3 and redmine 1.4.4

According to this -> Please help me send a jpg file using send_data , I do that in a controller:

  @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)

But it returns ArgumentError (invalid byte sequence in UTF-8), why ?

Answer

EloHailwidis picture EloHailwidis · Apr 22, 2014

The PDF file has to be encoded

 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)