Generate PDF(e-ticket) from html in Rails

In most of the projects we get requirements in which we need to send an attachment or download PDFs for order’s e-ticket or any certificate kind of things. So here I have written a post  for how to create PDFs from html using “pdfkit” gem.

Following are the steps for creating pdf from html views.

  1. Add gem “pdfkit” in gemfile
  2.  Download following 2 files
  3. Create “bin” folder in your rails application folder and extract above files in this “bin” folder. These are the binaries which are needed for creating  pdf from html views.
  4. Create “pdfkit.rb” in “config/initializers” folder of the application. And add following code
    PDFKit.configure do |config|
      if ["development"].include?(Rails.env)
        #only if your are working on 32bit machine
        config.wkhtmltopdf = Rails.root.join('bin', 'wkhtmltopdf-i386').to_s
        #if your site is hosted on heroku or any other hosting server which is 64bit
        config.wkhtmltopdf = Rails.root.join('bin', 'wkhtmltopdf-amd64').to_s
      config.default_options = {
        :disable_smart_shrinking=> false
  5. Create a file e_ticket.rb in models folder as following
    # e-ticket
    class ETicket < AbstractController::Base
      include AbstractController::Rendering 
      include AbstractController::Helpers 
      include AbstractController::Translation 
      include AbstractController::AssetPaths 
      include Rails.application.routes.url_helpers 
      helper ApplicationHelper 
      self.view_paths = "app/views" 
      attr_reader :html 
      def initialize(order) 
        @order = order 
        @html = render_to_string(partial: "shared/e_ticket", :layout => false,
             :disposition => 'inline')
      def pdf
        kit =
        kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/pdf.css"

In above code, we have added style sheet “pdf.css” in which we have to write the pdf related css rules. Keep this file as separate for pdf only .
Now you have your e-ticket ready to download and attach to any mail. You have to just create e-ticket by

eticket =
pdf = eticket.pdf

yehhhhhh……………..its ready to download and attach.

Now I am writing brief for downloading and attaching the pdf to mail.

How to download eticket pdf.

In your orders controller you can create an separate action for downloading or you can do this in your orders show action in pdf response. Any way I am creating separate action for downloading order’s e-ticket pdf.

class OrdersController < ApplicationController
  def download
    @order = Order.find(params[:id])
    eticket =
    send_data(eticket.pdf, :filename => "e-Ticket #{@order.number}",
       :type => 'application/pdf')

Sending e-ticket as attachment in mail

class OrderMailer < ActionMailer::Base
  def send_e_ticket(order)
    @order = order
    subject = " E-Ticket"
    eticket =
    attachments["E-Ticket #{@order.number}.pdf"] = eticket.pdf
    mail(:to =>””, :subject =>”E-ticket”)

Now send an mail using following code


yippeeeeee…..Mail sent with attached e-ticket pdf.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s