Rmagick file conversion


#1

I am using rmagick to convert files stored as a tiff to either pdf or
jpg for display to the user. A snippet of test code from the controller
follows. It works OK. ( it does need a sanitizer )

require RMagick

def pdf
@sdoc = Sdoc.find(params[:id])
@fname = ‘/var/www/images/’ + @sdoc.fname
@nf = Magick::ImageList.new(@fname)
@nf.write(’/tmp/ff.pdf’)
send_file ‘/tmp/ff.pdf’
end

What I would like to do is avoid having to save the temporary file but I
cant see how to get RMagick to do the actual conversion from tiff to pdf
without writing to a temporary file as I have done above.

I have tried a few experiments with send_data but I think the image
objects are in image magicks internal format.

I am also not clear on if I could get send_data to process a series of
jpegs stored in an rmagick imagelist.

Thank you
Steven


#2

On Apr 5, 2006, at 8:51 PM, Steven Heimann wrote:

 @nf = Magick::ImageList.new(@fname)
 @nf.write('/tmp/ff.pdf')
 send_file '/tmp/ff.pdf'

end

This code won’t work in production!

Two simultaneous requests are going to produce some bad results…

Look at the Tempfile class for this sort of thing.

As for removing the need for a temporary file, look at Rails’ send_data
and RMagick’s to_blob.


– Tom M.