PDF Seafood Sauce: Prawn

The Ruby community is privileged to have a number of very valuable utility libraries. One of these is the venerable PDF::Writer, which enables the production of .pdf files with Ruby methods. Unfortunately, PDF::Writer has performance issues that can make it excruciatingly slow. The time has come for new contenders to step into the Ruby PDF ring and one of the most promising is named Prawn.
This is what the Prawn project page says -
Prawn is a PDF writing library for Ruby. It aims to be fast, tiny, and nimble, like the majestic sea creature.
Prawn is a beneficiary of the Ruby Mendicant project and is being driven, to a large degree, by the needs of the Ruport reporting toolset which is looking for a credible alternative to its dependency on PDF::Writer. Progress is pretty swift with a GitHub repository aiding the development effort.
In a recent article, Gregory Brown provided this example of PDF::Writer code vs Prawn code
PDF::Writer
1 2 pdf = PDF::Writer.new 3 table = PDF::SimpleTable.new do |tab| 4 tab.column_order.push(*%w(date rate)) 5 tab.columns["date"] = PDF::SimpleTable::Column.new("date") { |col| 6 col.heading = "Date" 7 } 8 tab.columns["rate"] = PDF::SimpleTable::Column.new("rate") { |col| 9 col.heading = "Rate" 10 } 11 tab.orientation = :center 12 data = csv_data.map do |e| 13 { "date" => e[0], "rate" => e[1] } 14 end 15 tab.data.replace data 16 end 17 table.render_on(pdf) 18 pdf.save_as('currency_pdf_writer.pdf')
same thing with Prawn
1 2 Prawn::Document.generate("currency_prawn.pdf") do 3 table csv_data, :font_size => 10, 4 :vertical_padding => 2, 5 :horizontal_padding => 5, 6 :position => :center, 7 :row_colors => :pdf_writer, 8 :headers => ["Date","Rate"] 9 end
The Prawn-fu looks a lot sweeter to me and the improvement in performance with Prawn over PDF::Writer is commendable. Get your Git on and download the latest Prawn version to give it a try yourself. If the bleeding edge is not for you then a gem should be appearing in the near future.
Keep an eye on this one it’s hot!

