I’m happy to announce that the Prawn PDF library has hit another milestone on the Ruby Mendicant project roadmap. This time we’ll look at Prawn’s shiny new table drawing support, as well as some of the other features that have been added over the last several weeks. We’ll also look at where things are headed in the future, including when to expect a first gem release. All this and more lies just beyond the cut…

Prawn Tables FTW

The above screenshot shows PDF::Writer and Prawn’s table output side by side. As you can see, they are nearly identical. The Prawn table is on the right, and the only significant difference is the way the headers are drawn. Looks can be deceiving though, as the similarities between the two libraries really are only skin deep.

In PDF::Writer, the above table was generated with this code:

pdf = PDF::Writer.new
table = PDF::SimpleTable.new do |tab|
  tab.column_order.push(*%w(date rate))
  tab.columns["date"] = PDF::SimpleTable::Column.new("date") { |col|
    col.heading = "Date"
  }
  tab.columns["rate"] = PDF::SimpleTable::Column.new("rate") { |col|
    col.heading = "Rate"
  }
  tab.orientation   = :center
  data = csv_data.map do |e|
    { "date" => e[0], "rate" => e[1] }
  end
  tab.data.replace data
end
table.render_on(pdf)
pdf.save_as('currency_pdf_writer.pdf')

In Prawn, the code to do the same looks like this:

Prawn::Document.generate("currency_prawn.pdf") do
  table csv_data, :font_size          => 10,
                  :vertical_padding   => 2,
                  :horizontal_padding => 5,
                  :position           => :center,
                  :row_colors         => :pdf_writer,
                  :headers            => ["Date","Rate"]
end

Style is subjective, but arguably, Prawn is a bit more Rubyish in its design. You need to do a little extra work to make the tables look exactly like PDF::Writer’s output, as Prawn’s default output is very simple. However, even with this ‘fancy’ output, Prawn is still quite fast.

I’ll leave running the benchmarks to individual users, but with a CSV of 1833 records, on my machine you can expect it to take a few seconds (3-4), which is about 25x faster than what I get for PDF::Writer (between 90-100s). Prawn scales linearly, so you can expect to process larger datasets and count the execution time in the seconds and minutes, rather than (as we unfortunately have seen with PDF::Writer) the hours. Some optimizations can be done to speed things up even more, but you will see orders of magnitude in improvement for table generation speed on Prawn.

Getting Fancy with Prawn Tables

Here’s a quick sample of some other fun stuff you can do with Prawn’s table drawing support that aren’t necessarily tied to making things look similar to PDF::Writer:

Though ‘fancy’ may be a bit of an overstatement, it shows that Prawn’s Table support has some flexibility to it. As we go on, I’m sure we’ll figure out more ways to make it do the things we need, but for now we’ve covered the basics. If you’re interested in how this all works, take a look at the example code that generated the above PDF.

Other New Stuff

I’ve also brought a few of Ruport’s features into Prawn, to ease integration in the coming weeks. These features wil be familiar to Ruport users, and worth taking a look at for new Prawn users:

  • horizontal_rule()
  • horizontal_line(x1,x2)
  • vertical_line_at(y,x1,x2)
  • pad(y)
  • pad_top(y)
  • pad_bottom(y)
  • move_up(y)
  • move_down(y)

These features aren’t going to win Prawn any awards, but it cleans things up a little to use these helpers when they’re appropriate.

In addition to these new features, a couple bugs were fixed in Permian, one to do with text height (which was mentioned in the last post here), and another to do with the way rectangles were being drawn.

On the Horizon

Even though I’m announcing Permian Prawn today, it was actually tagged, documented, and ready for use almost two weeks ago! Though not a ton of development happened on my end in the last week or so due to me taking some time off, ideas have been brewing and others have hacked together a bunch of different stuff.

Two different Rails plugins are under development, Steve Quinlan’s prawn_helper and thorny_sun’s prawn_view. I haven’t really had a chance to look into either project, but it’s pretty exciting to think that we’re already getting this kind of interest before an official release. Rails users will want to be sure to check these out. Between these two plugins and Ruport/Rails, Prawn will have much love on Rails apparently, and it’d be nice if we could find a way to combine efforts on these things…

In other news, our m17n support is slowly but surely improving thanks to the efforts of James Healy. I’ve been pulling these patches upstream as they come together, but it’d be great if we started getting some practical testing on these things from people who have a need for UTF-8 PDFs. Most of our testing so far has been somewhat trivial.

It seems like Prawn is in need of a ‘real’ release soon. I’m not promising an exact date, but I think we can look forward to a Prawn 0.1.0 gem before the end of July. All that really needs to be done is some work on text alignment and image embedding before I can package up a basic early beta version of Prawn for everyday use. With luck, that’ll go smoothly and folks can start kicking the tires and let me know what they think.

Until then, there are still ways the adventurous can help out.

Get Involved!

As much as this self-promotion is starting to grind on me, I feel like the more eyes on this project, the better it’ll be. Since my efforts on this project have been generously funded by donations from the Ruby community, I want to make sure that people have a voice in how the project turns out.

Pretty much all the relevant resources can be found through Github
, and if you have trouble finding anything or just want to get in touch, feel free to use the mailing list.

If you happen to be near Edmond, Oklahoma tomorrow (2008.07.10), or near Hartford, CT on July 28th, I’ll be doing Prawn talks. I’ll try to get the materials out to the mailing list, but meeting some folks in person would be great, too.

That’s pretty much all for now, I hope to see more patches, more discussion, and more PDF awesomeness between now and the first release.

Happy Hacking!