PDF in rails using prawn 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 prawnor
Add following line in rails environment file inside initializer block.config.gem 'prawn'Optionally you can specify version to be used and then run taskrake gems:installGenerating pdf using rails console./script/consolepdf = Prawn::Document.newIt 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 :bottom_margin => 0.01.m, # well :page_size => 'A4')pdf.text("Prawn Rocks")=> 12 pdf.render_file('prawn.pdf')=> #Here is output file generated [click]
Now let’s go through other goodness of prawn.pdf = Prawn::Document.new('A3') do

  1. FONTS [click]
  2. # Specify font to be used or specify path to font file.font "times.ttf"font("/times.ttf")

  3. TEXT [click]
  4. text 'Sandip Ransing', :size => 41, :position => :center, :style => :bold

  5. STROKE LINE [click]
  6. stroke do rectangle [300,300], 100, 200 end

  7. IMAGE [click]
  8. Display Local file system Imageimage 'sandip.png', :height => 50, :position => :center, :border => 2Scale Imageimage 'sandip.png', :scale => 0.5, :position => :leftDisplay Remote image from Internet inside pdfrequire "open-uri" image open('http://t2.gstatic.com/images?q=tbn:kTG6gAKrnou2gM:http://www.facebook.com/profile/pic.php?uid=AAAAAQAQrLXvTWfyY2ANjttV8D1c0QAAAAnDHPFJe0pPFR84iIzXPKro&t=1")end

  9. LINE BREAKS
  10. movedown(20)

  11. TABLE/GRID [click]
  12. 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', ""], [{: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 }

  13. LINKS [click]
  14. 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")

  15. PDF Security [click]
  16. encrypt_document :user_password => 'hello', :owner_password => 'railer', :permissions => { :print_document => false }

  17. Prawn Inline Formatting
  18. Prawn-format supports inline text formatting that gives user enough flexibility to use html tags.require 'prawn/format'text 'This is Strong text', :inline_format => truetext 'This is bold text \n It should be on newline.', :inline_format => true

SAVE PDF Fileendpdf.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

Advertisements

About sandipransing

Web Developer #ruby #rails #JS
This entry was posted in Ruby. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s