Anda di halaman 1dari 5

Building PDF Document in ruby & rails using prawn 2010

Library

Building PDF Document in ruby & rails application using


prawn Library
Brief

Before getting started with this tutorial, I would like to thanks Greg and Prawn
team for their awesome work towards ruby and rails community.

Installing prawn (core, layout, format, security)

gem install prawn

or

Add following line in rails environment file inside initializer block.

config.gem ‘prawn’

Optionally you can specify version to be used and then run task

rake gems:install

Generating pdf using rails console

./script/console

pdf = Prawn::Document.new

It creates new pdf document object. Here you can additionally pass options
parameters such as -
Prawn::Document.new(:page_size => [11.32, 8.49], :page_layout => :portrait)

Prawn::Document.new(A0) Here A0 is page size.

Prawn::Document.new(:page_layout => :portrait,

:left_margin => 10.mm, # different

:right_margin => 1.cm, # units

:top_margin => 0.1.dm, # work

Sandip Ransing [san2821@gmail.com] Page 1


Building PDF Document in ruby & rails using prawn 2010
Library
:bottom_margin => 0.01.m, # well

:page_size => 'A4')

pdf.text("Prawn Rocks")

=> 12

pdf.render_file('prawn.pdf')

=> #<File:prawn.pdf (closed)>

Here is output file generated [click]

Now let’s go through other goodness of prawn.

pdf = Prawn::Document.new('A3') do

1. FONTS [click]

# Specify font to be used or specify path to font file.

font "times.ttf"

font("/times.ttf")

2. TEXT [click]

text “Sandip Ransing”, :size => 41, :position => :center, :style => :bold

3. STROKE LINE [click]

stroke do

rectangle [300,300], 100, 200

end

4. IMAGE [click]

Display Local file system Image

Sandip Ransing [san2821@gmail.com] Page 2


Building PDF Document in ruby & rails using prawn 2010
Library

image 'sandip.png', :height => 50, :position => :center, :border => 2

Scale Image

image 'sandip.png', :scale => 0.5, :position => :left

Display Remote image from Internet inside pdf

require "open-uri"

image
open(“http://t2.gstatic.com/images?q=tbn:kTG6gAKrnou2gM:http://www.faceb
ook.com/profile/pic.php?uid=AAAAAQAQrLXvTWfyY2ANjttV8D1c0QAAA
AnDHPFJe0pPFR84iIzXPKro&t=1")

end

5. LINE BREAKS

movedown(20)

6. TABLE/GRID [click]

data = [

["Name", {:text => 'Sandip Ransing', :font_style => :bold, :colspan => 4 }],

["Address", {:text => 'SHIVAJINAGAR, PUNE 411005', :colspan => 4 }],

["Landmark",{:text => 'NEAR FC COLLEGE', :colspan => 4 }],

["Mobile","9860648108", {:text => "", :colspan => 3 }],

["Education", {:text => "Bachelor in Computer Engineering", :colspan => 4


}],

["Vehicle", 'Hero Honda',"Reg. No.", {:text => "MH 12 EN 921", :colspan


=> 3 }],

["Additional", "GDCA", "class", 'First', ""],

Sandip Ransing [san2821@gmail.com] Page 3


Building PDF Document in ruby & rails using prawn 2010
Library

[{:text => "Areas of Speciality", :font_style => :bold}, {:text => "Ruby,
Rails, Radiant, Asterisk, Adhearsion, Geokit, Prawn, ....,...", :font_style =>
:bold, :colspan => 4}],

[{:text => "Website", :colspan => 2},{:text => "www.funonrails.com",


:colspan => 3}],

[{:text => "Company", :colspan => 2},{:text => "Josh Software", :colspan
=> 3}]

table data,

:border_style => :grid, #:underline_header

:font_size => 10,

:horizontal_padding => 6,

:vertical_padding => 3,

:border_width => 0.7,

:column_widths => { 0 => 130, 1 => 100, 2 => 100, 3 => 100, 4 => 80 },

:position => :left,

:align => { 0 => :left, 1 => :right, 2 => :left, 3 => :right, 4 => :right }

7. LINKS [click]

link_annotation([200, 200, 500, 40],:Border => [0,0,1], :A => { :Type =>


:Action, :S => :URI, :URI =>
Prawn::LiteralString.new("http://twitter.com/sandipransing") } )

link_annotation(([0, 100, 100, 150]), :Border => [0,0,1], :Dest =>


s"http://funonrails.com")

Sandip Ransing [san2821@gmail.com] Page 4


Building PDF Document in ruby & rails using prawn 2010
Library

8. PDF Security

encrypt_document :user_password => 'hello', :owner_password => 'railer',

:permissions => { :print_document => false }

9. Prawn Inline Formatting

Prawn-format supports inline text formatting that gives user enough flexibility
to use html tags.

require ‘prawn/format’

text “This is <strong>Strong </strong> text”, :inline_format => true

text “This is <b>bold</b> text \n It should be on newline.”, :inline_format =>


true

END PDF Document

end

Render PDF File

pdf.render_file “my.pdf”
!!! NOTE: As of time now prawn-format is incompatible with latest prawn gem, It is compatible
with prawn version <= 0.6 s

Sandip Ransing [san2821@gmail.com] Page 5