Senior & Expert RoR Developers Discussion Forum by Nyros Technologies

HIRE Ruby on Rails Expert Developers Programmers Coders From India
Ruby on Rails PHP .Net Developers Community, Nyros Technologies, Kakinada
 
Log in  or IF not a member please REGISTER
Username:
Password:   


Keyword
Log in | Profile 

PDF Generation By Using HTMLDOC gem in Ruby on Rails

 
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> Plugins and Gems
View previous topic :: View next topic  
Author Message
sivakrishna.m



Joined: 01 Jan 2008
Posts: 160
Location: Narsipatnam

PostPosted: Mon Jan 04, 2010 10:56 am    Post subject: PDF Generation By Using HTMLDOC gem in Ruby on Rails Reply with quote

HTMLDOC is for converting the html pages into adobe portable document format and also is available in both open source and commercial versions. The wrapper class around HTMLDOC , providing methods for setting the options from ruby application and retriving generated output as a file, diretory or string.

For this there is Ruby HTMLDOC Gem, before installing the gem we have to install HTMLDOC software or packages.

1.For Linux users
===============

Installing HTMLDOC
==================
curl -O http://ftp.easysw.com/pub/htmldoc/snapshots/htmldoc-1.9.x-r1521.tar.gz

tar zxvf htmldoc-1.9.x-r1521.tar.gz

cd htmldoc-1.9.x-r1521

./configure --prefix=/usr/local

make

sudo make install

2. For Windows Users.
=====================

Installing HTMLDOC
==================

Download the latest HTMLDOC software from the site http://www.htmldoc.org/ and install.


After that installing HTMLDOC Gem for all users, follow the below steps

Installing HTMLDOC Gem
======================
> sudo gem install htmldoc ============> for Linux users

> gem install htmldoc ================> for windows users

After installation configure your application :
================================================

open the file config/environment.rb file and place the lines below

Mime::Type.register 'application/pdf', :pdf
require 'htmldoc'

next for DRY up the code add the following method to your application.rb file

def render_to_pdf(options = nil)
data = render_to_string(options)
pdf = PDF::HTMLDoc.new
pdf.set_option :bodycolor, :white
pdf.set_option :toc, false
pdf.set_option :portrait, true
pdf.set_option :links, false
pdf.set_option :webpage, true
pdf.set_option :left, '2cm'
pdf.set_option :right, '2cm'
pdf << data
pdf.generate
end

after that write the below code in your controller

def index
@items = Item.find(:all)

respond_to do |format|
format.html # index.html
format.xml { head Surprisedk }
format.pdf { send_data render_to_pdf({ :action => 'index.rpdf', :layout => 'pdf_report' }) }
end
end

by calling the render_to_pdf method for pdf format responds.

and finally write the code for @items list of items in index.erb.html file


<table><tbody>
<tr>
<th>Name</th>
<th>Description</th>
<th>Created at</th>
</tr>
<% @items.each do |item| %>
<tr>
<td><%= item.name %></td>
<td><%= item.desc %></td>
<td><%= item.created_at %></td>
</tr>
<% end %>
</tbody></table>

finally place the link_to tag for generating pdf file

<%= link_to 'PDF', formatted_items_path(:pdf) %>


Thank You,
Siva
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Senior & Expert RoR Developers Discussion Forum by Nyros Technologies Index -> Plugins and Gems
Page 1 of 1

 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts validates_multiparameter_assignments plugin usage:
0 vijayalakshmi 27 Wed Jul 28, 2010 4:24 am
vijayalakshmi View latest post
No new posts paperclip plugin usage in rails:
0 vijayalakshmi 23 Wed Jul 28, 2010 4:07 am
vijayalakshmi View latest post
No new posts Polymorphism
1 phani.galla 46 Tue Jul 27, 2010 3:27 pm
umamahesh_nyros View latest post
No new posts Deploying Ruby on Rails Application with Passenger
2 sivakrishna.m 630 Tue Jul 27, 2010 2:16 pm
criether View latest post
No new posts Steps to implement star-rating using acts_as_rateable plugin
0 vijayalakshmi 46 Mon Jul 26, 2010 5:47 am
vijayalakshmi View latest post
No new posts Twitter Retweet Integartion
0 Anu 41 Thu Jul 22, 2010 12:27 pm
Anu View latest post
No new posts Usage of build method in rails
0 ktulasi 30 Thu Jul 22, 2010 12:19 pm
ktulasi View latest post
No new posts Facebook Like Integration in Rails
0 Anu 53 Thu Jul 22, 2010 12:16 pm
Anu View latest post
No new posts Fshare in your Rails Application.
0 Anu 39 Thu Jul 22, 2010 12:10 pm
Anu View latest post
No new posts Installation of Postgresql in windows.
1 swaminadhan 198 Thu Jul 15, 2010 10:17 am
proximity3 View latest post




Hire an expert Ruby on Rails developer / coder / programmer or development team from India now!!

Other Forums : PHP   ::   .Net   |   Free unlimited HTML CSS templates download

Nyros Technologies   |   Kakinada City Portal   |   Developers Blog   |   About Ruby on Rails Experts   |   More