Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Prawn resize my image automatically?

When I try to insert an image (PNG at 72dpi) using Prawn, like this:

image "#{Rails.root}/public/images/pdf/logo.png"

Prawn inserts the image but scales it up by about 30% which makes it blurry (and bigger.) I dont have anything else in the file and the PDF initialization is fairly standard:

super(:page_layout => :portrait,
      :left_margin => 0.5.in,
      :right_margin => 0.5.in,
      :top_margin => 1.in,
      :bottom_margin => 0.5.in)

Based on the documentation, Prawn is supposed to insert the picture into the document without any modifications whatsoever. Why is this image resizing happening?


NOTE: I noticed that bounding boxes are also bigger than they should be (by the same ratio as my image).

like image 424
Alain Avatar asked Apr 18 '13 18:04

Alain


1 Answers

you can try passing the width and height that the image actually is see if that helps

Prawn::Images

Public Instance Methods image(file, options={}) click to toggle source

Add the image at filename to the current page. Currently only JPG and PNG files are supported.

NOTE: Prawn is very slow at rendering PNGs with alpha channels. The workaround for those who don’t mind installing RMagick is to use:

github.com/amberbit/prawn-fast-png

Arguments:

file path to file or an object that responds to #

Options:

:at an array [x,y] with the location of the top left corner of the image.

:position One of (:left, :center, :right) or an x-offset

:vposition One of (:top, :center, :center) or an y-offset

:height the height of the image [actual height of the image]

:width the width of the image [actual width of the image]

:scale scale the dimensions of the image proportionally

:fit scale the dimensions of the image proportionally to fit inside [width,height]

Prawn::Document.generate("image2.pdf", :page_layout => :landscape) do     
    pigs = "#{Prawn::BASEDIR}/data/images/pigs.jpg" 
    image pigs, :at => [50,450], :width => 450                                      

    dice = "#{Prawn::BASEDIR}/data/images/dice.png"
    image dice, :at => [50, 450], :scale => 0.75 
  end   

If only one of :width / :height are provided, the image will be scaled proportionally. When both are provided, the image will be stretched to fit the dimensions without maintaining the aspect ratio.

If :at is provided, the image will be place in the current page but the text position will not be changed.

If instead of an explicit filename, an object with a read method is passed as file, you can embed images from IO objects and things that act like them (including Tempfiles and open-uri objects).

  require "open-uri"

  Prawn::Document.generate("remote_images.pdf") do 
    image open("http://prawn.majesticseacreature.com/media/prawn_logo.png")
  end

This method returns an image info object which can be used to check the dimensions of an image object if needed. (See also: Prawn::Images::PNG , Prawn::Images::JPG)

like image 101
MZaragoza Avatar answered Nov 09 '22 03:11

MZaragoza