Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

send PDF with rails send_data

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 ?

like image 590
EloHailwidis Avatar asked Apr 15 '14 12:04

EloHailwidis


1 Answers

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)
like image 82
EloHailwidis Avatar answered Oct 21 '22 09:10

EloHailwidis