14

I can't seem to figure out how to import an image from an url

To import the images from my localhost I use:

image"./assets/images/img.png"

When I replace the path with an URL it says

`image' : URL not found

Is there a solution for this, or should I create a def that downloads the image and to use the path it returns?

Wijnand
  • 1,192
  • 4
  • 16
  • 28

2 Answers2

44

you need to do the following -

require "open-uri"

Prawn::Document.generate(INSERT_YOUR_PDF_FILENAME_HERE) do 
   image open(INSERT_YOUR_URL_HERE)
end

Refer to http://rubydoc.info/gems/prawn/0.12.0/frames for more details.

saihgala
  • 5,724
  • 3
  • 34
  • 31
  • Thx, but I found that same solution later that day :-). I forgot to post it here. Thank you anyway! If I would not have found it on my own, this would have fixed the problem for me! – Wijnand Jul 20 '12 at 11:51
  • 1
    Good that your problem has been resolved and I think it would be nice if you accept or uptick this answer! – saihgala Jul 22 '12 at 16:28
  • How can I use this if I have a file.pdf.rb because i think that code is for the controller ?? – Frank004 Nov 26 '14 at 14:46
  • Could someone tell me how to use this in https://github.com/spree-contrib/spree_print_invoice/blob/master/app/views/spree/admin/orders/invoice.pdf.prawn on line 8? I cant seem to get it to work. – Paul Mar 02 '15 at 10:33
2

I got this working for me:

image open("https://driver.mbpgroup.eu/images/mbp_logo.jpg"), position: :right, width: 250

I could NOT get this to work for me (Prawn documentation):

image "https://driver.mbpgroup.eu/images/mbp_logo.jpg", position: :right, width: 250