Sorry this page looks weird. It was automatically migrated from my old blog, which had a different layout and different CSS.

Faster PDFs with Prawn

Prawn was generating for me a 6 page, 1.9MB PDF of 362 sticky labels, each with some text and a barcode PNG image. Prawn was having to resize each PNG from 220px x 120px to 93px x 11px to fit my layout. This took a quarter of a second per image, 90s in total, maxing out the CPU for all that time. By comparison, without images it took 2s.

When I resized the PNGs beforehand to 220px x 11px, the PDF took 15s – a 6-fold speed-up.

So if you’re generating PDFs with Prawn, and they contain images, and it’s slow, try to resize the images beforehand to the dimensions Prawn wants.

I wanted to hand Prawn images with the correct width as well as height. Although Prawn can narrow my barcode PNGs without loss of quality, I wasn’t able to do it myself either in ImageMagick or by generating them at the right width in the first place with the PNG gem. If anybody can shed light on this, I’d love to hear from you.

Andrew Stewart • 4 November 2010 • Ruby
You can reach me by email or on Twitter.