Impressed with Prawn for PDF Generation
April 5th, 2009
I’ve been aware of Prawn, the Ruby PDF generation library for some time. Today was the first occasion I had an opportunity to utilise it and I was delighted with how easy it was to use.
My Ruby application needed to generate some tabular PDF reports so I installed the prawn gem and, with a little help from the examples on the Prawn home page and the core and layout documentation, I was off and running.
An example of using prawn via a rake task follows:
require 'rubygems' require 'prawn' require 'prawn/layout' namespace :report do desc "Generate top 10 points getters report" task :top10 => :environment do Prawn::Document.generate("reports/top10.pdf") do font "#{Prawn::BASEDIR}/data/fonts/DejaVuSans.ttf" font_size 14 text "Top 10 Points Getters" text " " # fetch two-dimensional array of data for the table data = Player.top10_data table data, :font_size => 9, :position => :center, :headers => ["#", "Player", "Points"], :row_colors => ["ffffff", "eeeeee"] end end end
Generating the report was then as simple as running:
rake report:top10
I’m looking forward to using other features of Prawn such as image embedding and content positioning. At the time of writing prawn itself is version 0.4.1 and prawn-layout is 0.1.0. Hopefully James Healy, an Australian noted as “instrumental to the forward development of the library”, is coming to Railscamp 5 next month. Then I can pick his brains about what other features are in the pipeline.
Navigation
Latest Blog Posts
- 10 Oct: Configuring CapGun
- 6 Sep: Inspirations for a Ruby DSL
- 28 Jul: At Your Service
- 11 Jun: Rails Camp 5 and Sydney RORO Meetup
- 11 May: Reflecting on JAOO Sydney 2009
Syndication
Tags
actionwebservice automated testing bdd blog capgun capistrano collaboration css cucumber deployment dsl estimating jaoo osdc osdc2008au pdf prawn rails railscamp rake rest rspec ruby selenium services syntax highlighting tdd unconference wds08 web design webrat xml xml-rpc
Comments
Hi Keith,
I’m certainly hoping to make it a long to Railscamp 5. Plans are afoot!
Posted by: James Healy 10 days later
Post a Comment
Please use your real name and be respectful. Textile formatting is supported.