Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Creating PDF from HTML in rails

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 420
    Comment on it

    Creating PDF from HTML in rails

    Hi friends,
    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:

    	gem 'wicked_pdf'
    

    2) Now run bundler in root directory:

    	> bundle
    

    3) Now create initializers by generators for customizing the configurable options available in wicked_pdf

    	> rails generate wicked_pdf
    	#=> 
    	create  config/initializers/wicked_pdf.rb
    

    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:

    	WickedPdf.config = {
    	  exe_path: '/usr/local/bin/wkhtmltopdf'
    	}
    

    Note: For information regarding wkhtmltopdf you can see http://wkhtmltopdf.org/

    7) The output of a file in pdf can be achieved as simple as rendering html from a view like this:

    	class WelcomeController < ApplicationController
    	  def index
    	    respond_to do |format|
    	      format.html
    	      format.pdf do
    	        render pdf: "index"
    	      end
    	    end
    	  end
    	end
    

    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:

    	<!doctype html>
    	<html>
    	  <head>
    	    <meta charset='utf-8' />
    	    <%= wicked_pdf_stylesheet_link_tag "pdf" -%>
    	    <%= wicked_pdf_javascript_include_tag "number_pages" %>
    	  </head>
    	  <body onload='number_pages'>
    	    <div id="header">
    	      <%= wicked_pdf_image_tag 'image path' %>
    	    </div>
    	    <div id="content">
    	      <%= yield %>
    	    </div>
    	  </body>
    	</html>


    9) At last there are so many customizable options that can be used during the rendering of the pdf, some of them are like:

    respond_to do |format|
          format.html
          format.pdf do
            render pdf: 'file_name',
              disposition: 'attachment', # default 'inline'
              template:'template_path',
              ..............
              header: {   
                 html:
                   {   
                 	 template: 'some_header', # use :template OR :url
                     layout: 'pdf_layout_name',
                     .....
                    }
              },
              footer: {   
              	 html:
                    {   
                 	  template: 'some_footer',# use :template OR :url
                      layout: 'pdf_layout_name',
                      .....
                    }
              },
              toc: {   
                 font_name: "NAME",
                 depth: LEVEL,
                 ......
              }
          end
        end

    There are so many other customizable options that you can get at https://github.com/mileszs/wicked_pdf

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: