Search code examples
imageprawn

Adding image to PDF in Prawn using image data


So I have an image that's being saved in a database that I need to show on my Prawn Pdf, but when I pull the image, decode it, and try to display it on the Prawn PDF, I keep getting the issue where it wants a file path. Is there a way that I can pass the image data to the pdf, instead of the a file path, and it can just display that image?

Is there a better way?


Solution

  • Prawn can accept a StringIO, with the image data all in memory:

    require 'stringio'
    
    image_data = StringIO.new( Base64.decode64(@image) )    
    pdf.image(image_data)