Download PDF's in rails using PDFKit Gem

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 :
<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 =
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") %>

No comments:

Post a Comment