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.