pdf in rails



Create PDFs using HTML+CSS. Uses wkhtmltopdf on the back-end which renders HTML using Webkit.

You need to install these gems mentioned below :
gem install pdfkit
gem install wkhtmltopdf-binary



Than need to add this line into application.rb :

config.middleware.use PDFKit::Middleware, :print_media_type => true 
Now In View you need to add content or whatever you wants to use :

download_pdf.html.erb
<H1>PDFKit works awesome</H1>
This is you HTML data and going to be convert in pdf

Than in the controller we need add this:

class HomeController < ApplicationController
  def download_pdf  
    html = render_to_string(:action => '../home/download_pdf', :layout => false)
    pdf = PDFKit.new(html)
    send_data(pdf.to_pdf)
  end
end

Than you need to add this link to download pdf or you can change as per your conditions :
<p id="pdf_link">
  <%= link_to "Download (PDF)", home_download_pdf_path(:format => "pdf") %>
</p>

Comments

Popular Posts

How to pass hash in Postman

nginx: unrecognized service

Bootstrap Select Picker append add new item if search not exist

Reading Excel Sheets using "Roo" gem in ruby on rails

Add CORS to Nginx on AWS Elastic Beanstalk

Enable gzip compression on Elastic Beanstalk with nginx

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Get video duration by URL in Ruby on Rails

site-enables nginx setting in ruby in rails