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.
[Link] ‘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::[Link]
It creates new pdf document object. Here you can additionally pass options
parameters such as -
Prawn::[Link](:page_size => [11.32, 8.49], :page_layout => :portrait)
Prawn::[Link](A0) Here A0 is page size.
Prawn::[Link](:page_layout => :portrait,
:left_margin => [Link], # different
:right_margin => [Link], # units
:top_margin => [Link], # work
Sandip Ransing [san2821@[Link]] Page 1
Building PDF Document in ruby & rails using prawn 2010
Library
:bottom_margin => 0.01.m, # well
:page_size => 'A4')
[Link]("Prawn Rocks")
=> 12
pdf.render_file('[Link]')
=> #<File:[Link] (closed)>
Here is output file generated [click]
Now let’s go through other goodness of prawn.
pdf = Prawn::[Link]('A3') do
1. FONTS [click]
# Specify font to be used or specify path to font file.
font "[Link]"
font("/[Link]")
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@[Link]] Page 2
Building PDF Document in ruby & rails using prawn 2010
Library
image '[Link]', :height => 50, :position => :center, :border => 2
Scale Image
image '[Link]', :scale => 0.5, :position => :left
Display Remote image from Internet inside pdf
require "open-uri"
image
open(“[Link]
[Link]/profile/[Link]?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@[Link]] 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 => "[Link]",
: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::[Link]("[Link] } )
link_annotation(([0, 100, 100, 150]), :Border => [0,0,1], :Dest =>
s"[Link]
Sandip Ransing [san2821@[Link]] 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 “[Link]”
!!! 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@[Link]] Page 5