0
votes

I have a normal Rails layout which is my normal Site.

I want a Button on this page to convert/transform this page into a PDF file.

I want to do it with this gem: https://github.com/mileszs/wicked_pdf

I added gem 'wicked_pdf' to my Gemfile, but now how can I convert the actual page?

2

2 Answers

7
votes

Here's what I did to use wicked_pdf:

# Gemfile
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary' # if you need the binary too

Then run

$ bundle install
$ (bundle exec) rails g wicked_pdf    # generates the initializer

Then you can configure global settings in config/initializers/wicked_pdf.rb as stated in the wicked readme on github.

Rails 4 already knows the pdf mime type, but in older versions you'll probably need to register pdf as a mime type:

# config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf

In your controller

respond_to do |format|
  format.html
  format.pdf do
    render  pdf: "your-filename"
  end
end

Then you put a view under app/views/<your-view>/<controller-action>.pdf.erb with the content you wan't.

To reference this by a link you can do something like:

 = link_to "Get PDF", your_path(format: :pdf)

Does this solve your question?

4
votes

Please follow the below link. Its a details tutorial, this will make your life easier:

http://dchua.com/2014/10/30/generate-pdfs-with-html-templates-in-rails/

Note

If you don't want to have your pdf views separately and want to use the same html view, then please use

template: invoices/show.html.erb

instead of

template: invoices/show.pdf.erb