7

I´m willing to use Gruff to render some graphics inside prawn documents.

I want to avoid the delay of writing images to the disk only for them to be read by Prawn.

Gruff offers the to_blob method that renders the graph image as a blob in memory, but looking at the prawn manual image section, it only gives us a way to render images directly from files.

Am I missing something? Is there a way to render it from memory?

Ricardo Acras
  • 35,784
  • 16
  • 71
  • 112

1 Answers1

14

You should be able pass the image data as a StringIO

require 'stringio'
require 'pdf'

Prawn::Example.generate("foo.pdf") do |pdf|
  data = StringIO.new(render_my_image_to_a_string)
  pdf.image(data)
end 
James Healy
  • 14,557
  • 4
  • 33
  • 43
  • 1
    Thank you! Works great with Barby for barcode generation. – Hairgami_Master Mar 09 '16 at 03:28
  • just to add to this still working response : if you are storing your blob as `data:image/png;base64,` then see this [answer](https://stackoverflow.com/a/35517769/6324558) to get your `render_my_image_to_a_string` – Alexis Delahaye Feb 06 '20 at 10:08