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 :
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>

About the author

Being the CEO and Founder of ClecoTech International, Mr. Ashish Prajapati is dedicated towards his aim of mentoring young startups into a full-fledged businesses. He is helping startups from America, Europe, India, and various other countries through proper guidance and the use of latest technologies to develop their innovation and ideas into definite realities.

Leave a Reply

Your email address will not be published. Required fields are marked *