Reputation: 21
I have managed installing PDFKit and wkhtmltopdf on Windows 10 for using with Rails 5.
But I can't find any relevant exemple on how to render a html.erb
file to PDF.
I took a look on this : pdfkit Usage and it works to save any website to PDF.
I have a <div>
that I want to render to PDF.
Upvotes: 0
Views: 4885
Reputation: 21
Answer :
1) Add gem 'pdfkit
gem 'wkhtmltopdf-binary'
to Gemfile.
2) Run bundle install
3) Install wkhtmltopdf, and run which wkhtmltopdf
then paste this path to config/initializers/pdfkit.rb
`# config/initializers/pdfkit.rb
PDFKit.configure do |config|
config.wkhtmltopdf = '/home/harri/.rbenv/shims/wkhtmltopdf'
config.default_options = {
:page_size => 'A4',
:encoding => 'UTF-8',
:print_media_type => true
}
config.default_options[:load_error_handling] = 'ignore'
# Use only if your external hostname is unavailable on the server. config.root_url = "http://localhost" config.verbose = false end `
4) In your controller :
`html = render_to_string(:action => "edit", :layout => false)
kit = PDFKit.new(html)
kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/pdf.css"
#kit.to_file("#{Rails.root}/public/system/daoe/daoe" + @daoe.id.to_s+'.pdf')
send_data(kit.to_pdf, :filename => 'report.pdf', :type => 'application/pdf', :disposition => 'inline'`
Use kit.to_file if you want to save the PDF, or send_data if you want to render the PDF fullpage.
Upvotes: 0