Pregunta

I currently run Rails 3.1 and am using the latest version of Wicked_pdf for PDF generation. I have set everything up correctly, and PDFs are generated just fine.

However, I want the user to be able to click a button to DOWNLOAD the pdf. At the present time, when clicked, the browser renders the pdf and displays it on the page.

<%= link_to "Download PDF", { :action => "show", :format => :pdf }, class: 'button nice red large radius', target: '_blank'%>

My Controller.

def show
    @curric = Curric.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @curric }
      format.pdf do
        render :pdf => @curric.name + " CV",
        :margin => {:top                => 20,  
                    :bottom             => 20 }

      end
    end
  end

I have been pointed towards send_file, but have absolutely no idea how to use it in this scenario.

Any help is appreciated.

¿Fue útil?

Solución

Let's try:

pdf = render_to_string :pdf => @curric.name + " CV",
                       :margin => {:top     => 20,  
                                   :bottom  => 20 }
send_file pdf

Otros consejos

Decomposition the config you need to set as 'attachment', example:

respond_to do |format|
  format.pdf do
    render pdf: @curric.name + " CV",
           disposition: 'attachment'
  end
end

I am doing it that way:

  def show
    @candidate = Candidate.find params[:id]

    respond_to do |format|
      format.html
      format.pdf do
        @pdf = render_to_string :pdf => @candidate.cv_filename,
            :encoding => "UTF-8"
        send_data(@pdf, :filename => @candidate.cv_filename,  :type=>"application/pdf")
      end
    end    

  end

and it works for me ;-)

//Download pdf generated from html template using wicket_pdf gem 
pdf = render_to_string :template => "simple/show" // here show is a show.pdf.erb inside view

send_data pdf
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top