0
votes

I use prawn-rails.

I have a show.pdf.prawn view.

I would like to have an action to download this pdf.

I tried the following:

    def download
      send_data(
        show,
        filename: "#{custom_name}.pdf",
        type: 'application/pdf'
      )
    end

What it does, is download something called #{custom_name}.pdf but I m unable to open it (falied to load PDF document).

Having it changed to

    def download
      send_file(
        show,
        filename: "#{custom_name}.pdf",
        type: 'application/pdf'
      )
    end

I am getting

no implicit conversion of true into String

Please advice.

2
How are you defining custom_name ??? - rlarcombe
I will be able to check your solution tomorrow - I already finished work today :) - Andrey Deineko
My solution was a guess... based on the fact that the prawn-rails gem gives you a view handler, so you shouldn't need to call send_data, or send_file. Just render out the view and set the Content-Disposition. ... I'll be interested to hear if it works! - rlarcombe

2 Answers

1
votes

Should work:

send_data render_to_string("show"), filename: "#{custom_name}.pdf",
                                    type: 'application/pdf'

Also you can use prawnto_2 library:

gem "prawnto_2",  require: "prawnto"

and then simply render in the controller action:

def show
  respond_to do |format|
    format.pdf  { 
        prawnto inline:   false,
                filename: "#{custom_name}.pdf",
                prawn:    {} # prawn settings here
    }
  end
end

But I'm not sure how this library is currently maintained, however still works with Rails 4.2.x.

0
votes

How about:

def download
  response.headers['Content-Disposition'] = 'attachment; filename="' + custom_name + '.pdf"'
  render :show, mime_type: Mime::Type["application/pdf"]
end