Rails: how to save a 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 

It works great. the user can download the created PDF file. but I need to save the generated PDF file on the server for other purposes, such as mailing, etc. etc. How to save this generated PDF file?

I tried the following but don't know how to pass html to wickedpdf wicked_pdf does not work - Ruby on Rails

early

+4
source share
2 answers

You probably already understood this, but now I'm learning WickedPdf and just learned how to save it directly to the controller in the response_to block. There is good documentation on this Git page for this https://github.com/mileszs/wicked_pdf . Here is 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 will save it in a folder in my root called "pdfs" as username.pdf. Hope this helps.

+8
source

As far as I know, you cannot directly save files from the response_to block, you will need some kind of script that actually visits this page with the extension .pdf and saves it.

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

+1
source

Source: https://habr.com/ru/post/1339769/


All Articles