Rails: Convert HTML to PDF? [closed]
Solution 1:
The gem, WickedPDF, does exactly that.
Solution 2:
The greatest thing nowaday is PDFKit for this job:
- https://github.com/pdfkit/PDFKit
- http://railscasts.com/episodes/220-pdfkit
Solution 3:
Try WickedPDF, PDF generator (from HTML) plugin.
Here is example
gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
#wicked_pdf is a wrapper for wkhtmltopdf, you'll need to install that, too
In Your Controller(I supposed you are in show method):
respond_to do |format|
format.html
format.pdf do
pdf = render_to_string :pdf => 'test',
layout: 'pdf.html.erb',
template: 'show.pdf.slim',
header: { :right => '[page] of [topage]'},
margin: {top: 0,
bottom: 0,
left: 0,
right: 0},
outline: {outline: true,
outline_depth: 2}
end
end
In your view for PDF link
= link_to 'Download Pdf', you_show_path(@uour_object, format: :pdf)
If you want to send pdf in email attachment, first save your pdf file in public or temp folder then in mailer class (e.g. in mailers/e_mailer.rb)
attachments["test.pdf"] = File.read(Rails.root.join('public',"test.pdf"))
mail(:to => to, :cc => cc , :subject => "subject")
After sending email you can delete file
File.delete(Rails.root.join("public", "test.pdf"))