In rails if you want to generate a pdf, there are multiple gems available that will make your task easy. Normally the approach for creating a pdf will be first create a html template and then convert that template into pdf. Here I am going to tell you how we can use "wicked_pdf" gem.
1) First you need to add the gem to your Gemfile:
2) Now run bundler in root directory:
3) Now create initializers by generators for customizing the configurable options available in wicked_pdf
4) If you are using an older version of rails, you may also need to configure the mime type inside config/initializers/mime_types.rb
Mime::Type.register "application/pdf", :pdf
5) Now one thing to be noted wicked_pdf works over wkhtmltopdf, which needs to be installed on your machine, For installing wkhtmltopdf you can through the following link: Install pdfkit and wkhtmltopdf library on Ubuntu 12.04/ruby
Alternatively you can install all the required components by adding a gem 'wkhtmltopdf-binary' in your Gemfile and running the bundle
6) After that you need to update the path of the wkhtmltopdf inside the config/initializers/wicked_pdf.rb, if it is not the one where wkhtmltopdf if installed:
7) The output of a file in pdf can be achieved as simple as rendering html from a view like this:
class WelcomeController < ApplicationController
respond_to do |format|
render pdf: "index"
If a index.pdf.erb is present there, it will get rendered as pdf
8) Thus we can see that we can create html file with extension .pdf.erb and it will be rendered as a pdf. But as wkhtmltopdf is installed outside the rails application it will not get the layouts present in the rails application. for that we will use the wicked_pdf helpers like this: