Display base64 image (from canvas.toDataURL) in a prawn PDF?

550 views
Skip to first unread message

dave.p...@gmail.com

unread,
Jan 22, 2015, 11:31:14 AM1/22/15
to prawn...@googlegroups.com
Hi,

I want to do something like this. The base64 is derived from calling canvas.toDataURL() in Javascript, then passed to my PDF app.

image = "data:image/png;base64,iVBORw0KGg..." (clipped here, not in my actual code)

Prawn::Document.generate(tmp) do |pdf|
    pdf.image open(image)
end

I get "file is an unrecognised format" errors.

Any ideas or samples? I've searched far and wide with no good hits. I'd prefer not having to write the images to disk.

Thanks,
Dave

Gregory Brown

unread,
Jan 22, 2015, 11:36:28 AM1/22/15
to prawn...@googlegroups.com
Prawn won't do the decoding for you, but you can probably use the base64
standard library[0] for that and then wrap the result in a StringIO
object and pass that to Document#image.

Hope that helps!

-greg

[0] http://www.ruby-doc.org/stdlib-2.2.0/libdoc/base64/rdoc/Base64.html
> --
> You received this message because you are subscribed to the Google
> Groups "Prawn" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to prawn-ruby+...@googlegroups.com
> <mailto:prawn-ruby+...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages