Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails : How to save the pdf file generated using wicked pdf

I am using WickedPdf

respond_to do |format|
  format.html
  format.pdf do
    render :pdf => "file_name"
  end
end  

This is working fine . the user is able to download the generated pdf . but i need to store the generated pdf in server for other purposes like mailing etc etc How can i save this generated pdf ?

i tried the following but no idea how to pass the html to wickedpdf wicked_pdf doesn't work -- Ruby on Rails

thanks in advance

like image 558
Krishna Prasad Varma Avatar asked Dec 29 '22 01:12

Krishna Prasad Varma


2 Answers

You probably figured this out already, but I'm learning WickedPdf right now and just learned how to save directly in your controller in the respond_to block. There is great documentation on the Git page for this https://github.com/mileszs/wicked_pdf. Here's what I have in my controller for the show action:

  def show
@user = User.find(params[:id])
respond_to do |format|
  format.html # show.html.erb
  format.pdf do
    render :pdf => "#{@user.name}",
    :save_to_file => Rails.root.join('pdfs', "#{@user.name}.pdf")
  end
end
end  

This ends up saving it to a folder in my root called "pdfs" as the username.pdf. Hope that helps.

like image 113
FattRyan Avatar answered Jan 14 '23 03:01

FattRyan


As far as I'm aware you cannot save files from the respond_to block directly, you'll need some sort of script that actually visits that page with the .pdf extension, and saves it.

I recommend wkhtmltopdf as I use it quite often and it renders PDF's very well. This will allow you to save the PDF to the file system.

like image 33
Rabbott Avatar answered Jan 14 '23 03:01

Rabbott