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.

Tags: prawn pdf ruby | Comments (1)