0

I like to generate PDF-Files which also includes images uploaded by the user. The prawn gem works so far, and can also embed images located in the file system. My problem is that I want to include user uploaded images, and where these images are stored will probably change in the future (from file system to some cloud service). I use the dragonfly gem for handling the images, this uses rack to access the images and sometimes process them on the fly.

Now the simpliest idea does not work (with report being my object and spot_image my image field)

image report.spot_image

no implicit conversion of #<Class:0x007fc07ecf1110> into String

I tried also to open the file via http with open-uri. This should work, but it blocks on my development machine, I think because the development rails server is single threaded:

image_path = report.spot_image.remote_url
image_url = "#{view.request.protocol}\#view.request.host_with_port}
             /#{image_path.sub(/^\//,"")}"

image open(image_url)  # timeout in development-mode

It may probably work in production, but even then it does a needless http-request. Can I ask dragonfly (or rack) directly for the image? prawn uses duck-typing and needs some object that responds to read and rewind as image.

Meier
  • 3,858
  • 1
  • 17
  • 46
  • `It may probably work in production, but even then it does a needless http-request` - what do you mean by needless? If its a remote image than some component will have to do a http request to fetch it for you to embed – Cody Caughlan Jul 17 '15 at 16:08

1 Answers1

0

I found relevant documentation about dragonfly file handling. The following did not work: The report.spot_image.data method returns the image data as string, but prawn recognizes the data as path instead of as image data. The dragonfly tempfile method returns a closed temporary file, but prawn does not recognize that it can open it.

I had success with the file method, which returns an open file handle. It is unclear if prawn is closing this file than. So I used the block-style method to make sure the file is closed:

   report.spot_image.file do |f|
       image f
   end

This works so far. (not yet tested with cloud storage)

Meier
  • 3,858
  • 1
  • 17
  • 46