2
votes

I'm attempting to generate a PDF from a directory with a bunch of images in it using Prawn. The code I have so far works perfectly, with only one small problem, it inserts a blank page at the beginning of the PDF.

Since I don't necessarily know the sizes of the images (other than them being about the same, +/- 50 pixels in either dimension) I'm using RMagick to get the maximum dimensions of the images in the directory so I can center them on each page with a bit of margin.

Here's the code stripped down to just the necessary steps to reproduce the behavior:

require 'rmagick'
require 'prawn'

# Page files always have a name in the form of 'page_#.jpg'
pages = Dir.glob('*.jpg').sort_by { |file| file.split('_')[1].to_i }

# There's probably a better way to do this part, but the naive way works just fine, so whatever...
widths = Array.new

heights = Array.new

pages.each do |page|
  image = Magick::Image::read(page).first

  widths << image.columns

  heights << image.rows
end

Prawn::Document.generate('test.pdf', :page_size => [widths.max + 36, heights.max + 36]) do |pdf|
  pages.each do |page|
    pdf.image page, :position => :center, :vposition => :center
  end
end

This is the first time I've really done anything with Prawn before so there's probably something simple that I'm missing.

If it matters; I'm using Ruby 1.9.3-p327 and Prawn 0.12.0.

1

1 Answers

0
votes

It appears that whatever was causing this issue has been fixed. Using the same code as above with Prawn 1.1.0 does not produce a blank page before the other pages.