Presentation is loading. Please wait.

Presentation is loading. Please wait.

PDF Generation in Rails

Similar presentations


Presentation on theme: "PDF Generation in Rails"— Presentation transcript:

1 PDF Generation in Rails
Using Prawn & Prawnto

2 Why users Love PDFs They are great for: Reports Static data Forms
Invoices Tightly controlled formatting Print friendly Portable Looks the same for everyone

3 PDF Library types HTML to PDF (if your team skill set is more aligned with HTML/CSS) e.g: princeXML, HTMLDOC, wkhtmltopdf, etc. PDF Template binding (The structure is mostly static, the document is very large & complex) e.g: pdftk, iText, etc. Dynamic (not too large/complex, pure ruby library, open source solution) => PRAWN

4 What type should I use? What is the content? (tax form, invoice, eBook, product list) How large will the files be? How complex is the formatting? Do they need to be generated ‘on demand’, or would it be a batch or background job? Do you mind calling out to a library or command line tool? Character encoding, utf-8, internationalization, etc Who will be in charge of maintaining them? (developer or designer?)

5 Because… Why Prawn ? Lots of clear examples
Worked well for the report style that I needed (dynamic structure, simple table-based layout) Quick to generate pdfs on demand Fastest pure ruby pdf tool out there Code was well tested and easy to follow Forum/mailing list was active and helpful

6 Prawn is a joint effort team :
Prawn history Gregory Brown Prawn is a joint effort team : Gregory Brown, James Healy, Brad Ediger, Daniel Nelson, Jonathan Greenberg and dozens of contributors from the Ruby community.

7 Prawn history Goal: Bring the sort of modularity and aggressive code re-use found in web development to the printable document problem space.

8 Prawn Performance Around 20 times faster than PDF::Writer Even faster on ruby 1.9 Well designed (seems to scale linearly) Images are only loaded once

9 PDF Generation Libraries
PDF::WRITER (discontinued) Pure Ruby. Has been around for awhile Many examples out there Powerful, but ‘tedious’ syntax for positioning, styling, etc Prawn Lightweight, easy to learn syntax Newer library, still in alpha, but very promising Faster than PDF::Writer Continues to get faster and better

10 Using Prawn Insert into your Gemfile gem "prawn", "~> " gem "prawnto", "~> 0.1.1“ Install Prawn: $ sudo gem install prawn

11 Add to Controller prawnto :prawn => { :top_margin => 75, :left_margin =>55, :right_margin => 100} See the manual, for detail (center, column, etc.)

12 Create a File *.pdf.prawn
If you want to make a pdf file for index.html.erb, create a file: index.pdf.prawn If you want to make a pdf file for each content, create a file: show.pdf.prawn

13 Write Code on index.pdf.prawn
pdf.text "List of Drinks", :size =>22, :style => :bold, :align => :center pdf.text "\n"products do |product| [ product.name, product.description, (product.price),"Baht" ] end pdf.table productspdf.text "\n“ pdf.image "#{Rails.root}/app/assets/images/vending.jpg", :scale => 0.3, :position => 120

14 Write Code on show.pdf.prawn
pdf.text "Product ID = :size =>18, :style => :bold pdf.text " ",:style => :italic

15 Create Link for Printable Version
on: index.html.erb <%= link_to 'Printable Version', :format => 'pdf') %> on: show.html.erb <%= link_to 'Printable Version', :format => 'pdf') %> (rake routes for seeing the path name)

16 Flow 1. Setting Gemfile 2. Create index/show.pdf.prawn 3. Set the controller 4. Create Links to PDF

17 Tutorial Open existing project, e.g. blog application (lab 10) from Dr. Matt Create scaffold a list of product (see prawn tutorial) Create PDF for list of product and some blog content.

18 References: For Gem instalation: Video Tutorial: Ruby Pdf Toolls: Ruby Pdf Writer (Discontinued) : Sample Code: Brown, G., Manual Ruby for PDF : Mccafrey: Git Hub :

19


Download ppt "PDF Generation in Rails"

Similar presentations


Ads by Google