Wednesday, 3 October 2007

RESTful PDF on Rails 2.0

In this post, I'll guide you on how to serve PDF the RESTful way. Since Rails 2.0 preview is just released, I choose to use it over version 1.2.3. You can refer http://weblog.rubyonrails.com/2007/9/30/rails-2-0-0-preview-release on how to install Rails 2.0 preview.

deman@ubuntu:~/rails$ rails restful
create
create app/controllers
create app/helpers
......
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log

To generate pdf, you need to install railspdf plugin.

gem install pdf-writer

ruby script/plugin install svn://rubyforge.org//var/svn/railspdfplugin/railspdf/

We need to edit config/initializers/mime_types.rb to register pdf mime type:

# Be sure to restart your server when you modify this file.

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
# Mime::Type.register_alias "text/html", :iphone

Mime::Type.register "application/pdf", :pdf


Create a controller, in my case example controller.

script/generate controller example show
deman@ubuntu:~/rails/restful$ script/generate controller example show
exists app/controllers/
exists app/helpers/
create app/views/example
exists test/functional/
create app/controllers/example_controller.rb
create test/functional/example_controller_test.rb
create app/helpers/example_helper.rb
create app/views/example/show.html.erb

Then add the line below into config/routes.rb:

map.resources :example

Make sure you add the above sentence before this sentence:

# Install the default routes as the lowest priority.

Then we create the model for example.


deman@ubuntu:~/rails/restful$ script/generate model example word:string
exists app/models/
exists test/unit/
exists test/fixtures/
create app/models/example.rb
create test/unit/example_test.rb
create test/fixtures/examples.yml
create db/migrate
create db/migrate/001_create_examples.rb


deman@ubuntu:~/rails/restful$ rake db:migrate
(in /home/deman/rails/restful)
== 1 CreateExamples: migrating ================================================
-- create_table(:examples)
-> 0.4842s
== 1 CreateExamples: migrated (0.4844s) =======================================

We then run ruby console to add one record

deman@ubuntu:~/rails/restful$ script/console
Loading development environment (Rails 1.2.3)
>> e = Example.create(:word => "Hello World")
=> #

Edit app/controllers/example_controller.rb
class ExampleController < ApplicationController

def show
@example = Example.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.pdf { render :action => 'show' }
end

def getpdf
@paper='A4'
end
end
end


Edit app/views/example/show.html.erb
<%= @example.word %>



Edit app/views/example/show.rpdf
pdf.select_font "Times-Roman"
pdf.text @example.word


Ok, now we set to test our application.

To view the normal html page, http://localhost:3000/example/1
To view the pdf file instead, http://localhost:3000/example/1.pdf

Tq.

4 comments:

arlingtonVa said...

I've been working on an application that generates PDF docs (in Java).

One question for folks: Why do we still need PDF docs?

For Printing?
If the user needs to print, then use CSS with breakpoints in the HTML

For storing?
Database timestamps

Unfortunately, I wasn't involved in the design. But in the future I plan to politely ask, 'Why PDF?'.

PDF solutions cost a lot more money and often are unnecessary. And they go against the Ruby way - clean and simple ;)

Aditya said...

It maybe that PDF documents are self-contained and thus easily shareable or "email"able as attachments etc.

Although, yes, I think they are a huge pain to create and format for the programmer. However, the end up looking and printing a lot nicer than HTML.

Sudirman said...

Arlingtinva, yes I agree with you, in most situation, we don't need pdf. Some situation, we still need pdf for example digitally-signed document, for email sharing, to ensure the document looks like what we intended when print etc.

CSS can be standard for printing, but support for printing still lack in most browser. So I guess we stuck with PDF for quite some time until all the browsers support the latest CSS.

Buster Dog said...

Sometimes you want a really sharp publish quality document to print out. PDF is the way to go.