1

i've got trouble for croping a picture with Rmagick and then display with Prawn

Here is the code :

First a method to instantiate the image with RMagick :

def build_magick_image(path)
    if path.instance_of?(StringIO)
      Magick::Image.from_blob(path.read)
    else
      Magick::Image.read(path)
    end.first
end

In my case i have an instance of StringIO. Then i crop the picture :

img = build_magick_image(picture_path)
cropped_img = img.resize_to_fill(1735, 1560)

But now i can't find how to display it in my Prawn pdf. My last try is :

pdf.image cropped_img, at: [image_x, image_y], height: image_height.mm , width: inner_page_width.mm

But obviously it doesn't work.

Can someone help me?

Thanks :)

S.Martignier
  • 383
  • 2
  • 4
  • 17
  • Did you try with wicked PDF, one of my colleagues created a pdf with an image in it in this way, hope this helps you. `pdf-reports-body table td div.acknowledged_status_true { background-image: url('<%= asset_data_base64("acknowledge/acknowledged_status_true.png") %>'); } ` – Vaibhav Dhoke Oct 05 '17 at 18:37
  • Unfortunately, i work on an existing feature that has been developed with Prawn, so i can't change to wicked – S.Martignier Oct 06 '17 at 06:44
  • ok, Did you check [this](https://stackoverflow.com/questions/9565412/add-image-in-pdf-using-prawn) out, I guess you must have check it. – Vaibhav Dhoke Oct 06 '17 at 06:46
  • 1
    I checked it out, but thanks ;) – S.Martignier Oct 06 '17 at 09:25

1 Answers1

2

I found your question when I was trying to figure out the same thing Here's the solution I came up with:

Wrap your image blob in a StringIO object, which will give Prawn the interface it needs. In your specific case:

cropped_img_sio = StringIO.open(cropped_img.to_blob)
pdf.image cropped_img_sio, at: [image_x, image_y], height: image_height.mm, width: inner_page_width.mm
Will Koehler
  • 1,746
  • 22
  • 16