PDF Generation with Wicked pdf in Rails

I am using ruby 1.9.2 and rails 3. I think Wicked pdf is better than other rails pdf generation plugins. To start with wicked follow the steps :

    Step 1: Open the terminal and go to your rails application folder.

  • $ cd /home/MyApp
    Install wkhtmltopdf first .Open gem file, and add the line
    gem ‘wkhtmltopdf’
    $ bundle install
    Wicked pdf is using the wkhtmltopdf to create a pdf document from a rails html template.
    With wicked pdf we can, 

    1. Define documents structure through a simple html document.
    2. Theme them through CSS
    3. Just Download the static complied version from wkhtmltopdf home page. Extract it and place it under /usr/local/bin.
      Note : If your system is 64-bit download the 64Bit version. Else you get error, Something like ‘Exec Error’,’Executable format error’, etc.

    Step 2: Install wicked pdf plugin

  • $ rails plugin install git://github.com/mileszs/wicked_pdf.git
    now a configuration file is needed, we can use plugins generator script for it . But in rails 3 it will not work. Just copy the wicked_pdf.rb to initializers.
    $ cp vendor/plugins/wicked_pdf/lib/wicked_pdf.rb config/initializers
    and add the following to the config/initializers/wicked_pdf.rb
    WICKED_PDF = {
    :exe_path => ‘/usr/local/bin’
    }

    Step 3:Add pdf instructions to your controller.

  • In my rails app I created an action index_pdf. Under the action put the following line.
    def index_pdf
    render :pdf => “my_pdf”,:layout => false,:template => ‘/users/index_pdf’,:footer => {:center =>
    “Center”, :left => “Left”, :right => “Right”}
    end
    The render :pdf call has a lot of options. you can use these.

    Step 4: Create a view file in app/view/index_pdf.erb

  • Put the link in your app, in your home page or somewhere.
    <%= link_to ‘Create PDF document’, index_pdf_path(@user, :format => :pdf) %>
    the index_pdf_path is the path I mentioned in the route.rb file,
    match “/download_pdf(.:format)” => “users#index_pdf”, :method => :get, :as=>:index_pdf
    When all are set click the link to download the pdf.
  • Include image in Wicked pdf

    write a function in application helper to take your images in wicked pdf.
    def pdf_image_tag(image, options = {})
    options[:src] = File.expand_path(RAILS_ROOT) + ‘/public/images’ + image
    tag(:img, options)
    end
    Add your image in public/images.
    Write the following code in view  <%= pdf_image_tag(‘photo.png’, :style=>”margin:0px;padding:0px”,

    :width=>”160″, :height=>”160″)%>

Its ‘wicked!!’ isn’t it?

Advertisement

Storing a simple student database in Rails

Set up Ruby on Rails from http://rubyonrails.org/download. Make a directory ‘my_rails’ in your home folder for rails applications.
$ cd my_rails

create your application as following
$ rails new student_db
$ cd student_db
For including gems, you need to update the Gemfile, that resides in your application folder. Open the ‘Gemfile’ and update.

Gemfile :
source ‘http://rubygems.org&#8217;
gem ‘rails’, ‘3.0.0’
gem ‘sqlite3-ruby’, ‘1.2.5’, :require => ‘sqlite3’

Specify the version number for sqlite3.    This is for avoiding confusion while installing the gems.

Install gems
$ bundle install

Now you are ready to generate the student model. Use scaffold generator.

Student data model

Note : Rails developers dislike scaffold. Because it itself generates code. So don’t use it. This is only for referring the application.
$ rails generate scaffold Student first_name:string last_name:string roll_no:integer total_mark:integer

Migrate the database using rake.

$ rake db:migrate

Now run the server.

$ rails s

open the browser and go to http://localhost:3000/student

To see this application visit,
Student_data_base

Deploying Rails application in Heroku

Sign up using e-mail and a password.
http://heroku.com/
Then Heroku asks to check our mail and continue. Install git. Then Install heroku gem.

$ gem install heroku
Create SSH keys and tell Heroku the public key by
$ heroku keys:add
Create a new application in Heroku
$ heroku create
Then Heroku creates a random sub domain for our application. We can change it as we like. Then deploy the app to Heroku by
$ git push heroku master
Now we can visit our application by using the address.

Heroku pricing plans

Heroku provides different plans to deploy the application and for using the database space. Here the plan is Blossom. It is free and can take 5MB database maximum.

Git – The version control system

Git is a version control system and a very good friend of a developer. Todays all projects are very much depend on git. What’s the use of git? If a programmer develops a project he want to take copies of the progressing project at stage by stage. Using git we can ‘commit’ the project by giving comments like ‘first commit’ , ‘second commit’ etc. When a programmer deletes a file, he can recover the file using git. Git uses a tree structure to hold the data. It has a parent repository called master branch. We can change the branch to another and can process the data. Multiple branches are using when working on a projects with multiple developers. This branch master is unaltered when working on another branch. Download .rpm packages from RPM packages, and .deb packages from Debian Packages