How to convert base64 string to PNG using shrimp without saving to server in Rails

So, I'm trying to embed a canvas PNG image into a PDF using the Shrimp gem. A Base64 string is generated using the canvas toDataURL () function. Since the image is only required in PDF, I try to avoid saving it to the server. Params [: base64string] is passed to the server correctly.

However i try to use

image = Prawn::Images::PNG.new(base64string)

to create an image, but I get the NoMethodError: undefined `unpack 'method for nil: NilClass.

Any ideas what I'm doing wrong or how should it be done right?

+4
source share
1 answer

found here :

Prawn , . :

require 'prawn'
require 'tempfile'
require 'active_support' # for base64

Prawn::Document.generate('/tmp/test.pdf') do
  file = Tempfile.new('image')
  file.write ActiveSupport::Base64.decode64(image)
  file.close

  image file.path
end

, !

+2

All Articles