1
votes

I am trying to embed an image from Rails' Active Storage into a Prawn PDF.

This is what I've got in my Prawn PDF class:

path = @view.rails_blob_url(@logo, :host => "localhost:3000", :protocol => "http", :locale => nil)
image(path, :vposition => :center)

When I try to open the PDF, I get this error:

ArgumentError in InvoicesController#show http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBGdz09IiwiZXhwIjxudWxsHCJwdXIiOiJibG9iX2lkIn19--06167c36c283f6d5de63ae306b721310af11f70e/Test-Logo.png not found

When I copy that exact same URL into my browser, the image shows up as expected. 5 minutes later it expires (?).

What am I missing here?

How can I show the image in my PDF?

Why can the image be rendered in the browser but not in my PDF?

I've spent all day trying to get this to work today, but to no avail.

2

2 Answers

2
votes

Give this a shot:

image ActiveStorage::Blob.service.send(:path_for, @company.logo_image.key), at: [X, Y], width: DESIRED_WIDTH

Where logo_image is the actual ActiveStorage::Attached::One object. E.g

class Company
  has_one_attached :logo_image
end

Also, I believe as of now, only 2 types are supported: png and jpg.

2
votes

This is what worked for me:

def initialize_globals
  logo_from_object = @organization.logo
  @logo = StringIO.open(logo_from_object.download)
  ..
  ..
  ..
end

def print_header
  image @logo, scale: 0.10
end