12 December 2009

Ruby on Rails PDF generator : Prawn gem & Prawnto plugin

Download the pdf_demo application from GitHub

It has been amazing working for a project using Ruby on rails framework. And with the awesome support from its community, the experience has been always smooth. In this post I will be introducing you with a wonderful gem/plugin for Rails to generate PDF's. Generating PDF's has never been this easy. When I was asked to generate PF reports in my current project, the hunt for the best plugin began and after testing a few plugins I came across a gem called 'prawn'.

"Prawn takes the pain out of generating beautiful printable documents, while still remaining fast, tiny and nimble."

And Prawnto is a rails plugin which leverages the new prawn library to produce compiled pdf views.

In this post , I will provide simple step by step instruction to install and configure prawn/prawnto and hence will be walking through a simple PDF generating demo application.

Installing Prawn and Prawnto:

To begin with, we have to install prawn gem. From your command prompt(windows OS)

gem install prawn

Once we have successfully installed prawn gem, we need to install Prawnto plugin. Before installing the plugin, lets create the demo application which will be using MySQL database..

From the command prompt

C:\>rails pdf_demo -d mysql

After the project folder structure is created, move into the project folder:

C:\>cd pdf_demo

And now we will be installing the Prawnto plugin


C:\pdf_demo>ruby script/plugin install http://github.com/thorny-sun/prawnto.git/

After we have succesfully installed this plugin, we need to add the following line inside the environment.rb file of our project.

config.gem 'prawn'
within "Rails::Initializer.run do |config|"

Next, lets go ahead and create the Controller named Book


C:\pdf_demo>ruby script/generate controller Book
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/book
      exists  test/functional/
      create  test/unit/helpers/
      create  app/controllers/book_controller.rb
      create  test/functional/book_controller_test.rb
      create  app/helpers/book_helper.rb
      create  test/unit/helpers/book_helper_test.rb

Next lets create the model named book


C:\pdf_demo>ruby script/generate model book
      exists  app/models/
      exists  test/unit/
      exists  test/fixtures/
      create  app/models/book.rb
      create  test/unit/book_test.rb
      create  test/fixtures/books.yml
      create  db/migrate
      create  db/migrate/20091211170948_create_books.rb


Our project contains one database table(books) with two columns namely name and author.So add the following code inside your migration file (20091211170948_create_books.rb)


class CreateBooks < ActiveRecord::Migration
  def self.up
    create_table :books do |t|
      t.string   :name
      t.string   :author
      t.timestamps
    end
  end

  def self.down
    drop_table :books
  end
end

Before we go further with any more code, if you open up your database.yml file, it says


development:
  adapter: mysql
  encoding: utf8
  reconnect: false
  database: pdf_demo_development
  pool: 5
  username: root
  password:
  host: localhost

So we need to create "pdf_demo_development" named database. We can do that from the command prompt itself as shown below


C:\>mysql -u root

Here I have assumed that the username is "root" and password is nil.
If you have got password then use the following command

C:\>mysql -u root -p (password)

Once you get inside the mysql, we can go ahead and create the required database using following command

mysql> CREATE DATABASE pdf_demo_development;

Once this is successfull, we move ahead and migrate the database for our project.


C:\pdf_demo>rake db:migrate
(in C:/pdf_demo)
==  CreateBooks: migrating ====================================================
-- create_table(:books)
   -> 0.0630s
==  CreateBooks: migrated (0.0630s) ===========================================

Next we will be adding some dummy data's into our newly created database table namely 'books'

Use the following command to add data to the table. First select the database from mysql using


mysql> use pdf_demo_development;

Next lets add the dummy data


mysql> INSERT INTO books (name,author) VALUES ("The Rails Way", "Obie Fernandez");
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO books (name,author) VALUES ("Agile Web Development with Rails", "David");
Query OK, 1 row affected (0.01 sec)

So finally we write the codes for generating the PDF and showing the datas entered into the database.

Add the following codes into your app/controllers/book_controller.rb

class BookController < ApplicationController
 def index
 
 end
 
 def show
  @book = Book.find(:all)
  respond_to do |format|
   format.pdf { render :layout => false }
  end   
 end
end

Here we have created a method called 'show' and created a variable called '@book' which contains all the record from the 'books' table.

Next lets create a view(.rhtml file) for the index method within the books folder and add the following line of code inside this file, which is just a link to generate the PDF file.
app/views/book/index.rhtml

<%= link_to "(PDF Report)", :controller=>"book", :action=>"show", :id=>"2", :format=>'pdf' %>

And one final step before we run our server to view the result is to create a file with extension .pdf.prawn
This is the file where we write the code to display the contents in the PDF file. So create a 'show.pdf.prawn' file and add the following code
app/views/book/show.pdf.prawn

pdf.move_down(30) 
books =  @book.map do |item|
 [
  item.name,
  item.author
 ]
end

pdf.table books, :border_style => :grid,
     :row_colors => ["FFFFFF", "DDDDDD"],
     :headers => ["book", "author"],
     :align => { 0 => :left, 1 => :right, 2 => :right, 3 => :right }

Now lets go ahead and run the server

C:\pdf_demo>ruby script/server

And in your favorite browser go to => http://localhost:3000/book/index
and you will be taken to a page with your (PDF report) link and on clicking on this link you can get your PDF report.


You can visit prawn and prawnto websites to view their documentations and generate PDF's with various effects.

09 December 2009

BrowserCMS - RoR Content management System

Hi
These days I am busy learning new technical stuffs from Rails framework to wordpress developments. Recently I am engaged with a project which is related to Rails Content Management System. And among the various content management systems available for Rails framework, I am doing this project in BrowserCMS.



Quoting the description about BrowserCMS's from their WiKi,


BrowserCMS is a general purpose, open source Web Content Management System (CMS), written in Ruby on Rails. It is designed to support three distinct groups of people:
  1. Non-technical web editors who want a humane system to manage their site, without needing to understand what HTML or even Rails is.
  2. Designers who want to create large and elegantly designed websites with no artificial constraints by the CMS.
  3. Developers who want create CMS driven websites for their clients, or add a CMS to their Rails applications.



The very first thing every developer would do is install the BrowserCMS system in their local server and once learn how to go about using it, then move on to installing the system over the server and deploy the application.


The funny part is , In my case it all began in a reverse order.


We have got hosting account from HostingRails, where as our client has got hosting account from WebbyNode.


Initially we configured the BrowserCMS on HostingRails which worked fine as we were successfull in deploying the BrowserCMS demo site. Later on we did the same over WebbyNode and were successfull there too.


As I wanted to work on this project on my local machine and later on deploy it over the server, I went ahead and tried installing it on my local server which was also a successful attempt.


In upcoming days I will be posting step by step instructions, how I was able to install BrowserCMS over localhost in my computer and also various technical aspects on BrowserCMS.


So if you are a rails developer and is interested about BrowserCMS, do get back to get more updates.