Using an image larger than the page size causes 2 blank pages to render
I'm using Prawn to generate postcards. Users on my site can upload their own postcard templates which are converted into images so we can show them in the UI. Users can then add text via the UI, and then use the postcard templates to print postcards that can be mailed out. If the template they used is correctly sized, then printing is no problem and the postcard meets the size constraint (most common is 5.5" x 4.25").
My problem arises when a template is used that does not adhere to the correct ratio of width and height. At 300 DPI, a 5.5x4.25 inch postcard will be 1600x1275 pixels when I convert the postcard template to an image. And when I render the PDF using Prawn I can just use those dimensions to set the width and height of each page, given a 300 DPI resolution.
BUT, if a user uploads a template that does not adhere to the proper size constraints I have to adjust the page size appropriately. If a user uploads a template that ends up as a 1600x1425 px image, I downsize the height to fit back into the 1600x1275 px constraint, but I only change the page size and not the size of the image that I add to the page.
What happens when I do that is that the resultant PDF renders with 2 blank pages, and then the image shows up on a 3rd page.
Below is a the code I'm using, modified to work as a standalone snippet. I've attached the image I try to add to the PDF, as well as the resultant PDF.
require 'prawn'
require 'prawn/measurement_extensions'
require 'fileutils'
class Numeric
def px(dpi = 300)
(self/dpi.to_f).in
end
end
pdf = Prawn::Document.new margin: 0, skip_page_creation: true
width, height = 1600, 1425
required_dimensions = 5.5, 4.25
new_height = (width / required_dimensions.first) * required_dimensions.last
pdf.start_new_page size: [width.px, new_height.px]
pdf.image File.open(File.expand_path('~/Desktop/postcard.png')), {
width: width.px, height: height.px
};nil
path = File.expand_path('~/Desktop/postcard-test.pdf')
FileUtils.rm_f path
pdf.render_file path
postcard-test.pdf postcard.png
Let me know if you need any more information. Thanks! And thanks for the awesome tool!
EDIT: Forgot to add that I'm using prawn 2.0.2. I also updated my snippet to be more complete.
Hey @siannopollo I'm having this issue as well, did you get anywhere with this?
@jackcohen5 Since there was no answer on this I had to just work-around the problem by cropping the images so they will adhere to our size constraints. So when we create the images from PDF we run those images through ImageMagick (using the convert command) so that we get the exact dimensions we want. Our ruby code looks something like this:
io = IO.popen ['convert', path, '-crop', "#{width}x#{height}+0+0", new_path]
Process.wait io.pid
It would be nice if Prawn just handled the odd-shaped image properly, but this work-around is pretty easy to manage and ensures we always get the dimensions we want. Hope this helps.
This problem still exists in 2022. I agree that conversion using ImageMagick is the best, Another idea was to prepare a bounding_box that is the same height as the image.
pdf = Prawn::Document.new(page_size: 'A4', margin: 0)
image_dimensions = Prawn::Images::PNG.new("/path/image.png")
image_dimensions.calc_image_dimensions(:width => 200.mm)
pdf.bounding_box([0, 297.mm], width: pdf.bounds.width, height: image_dimensions.scaled_height) do
pdf.image("/path/image.png", :width => 200.mm, position: :center)
end