access images in AWS S3

30 views
Skip to first unread message

Herbert Schmoll

unread,
Jan 2, 2021, 6:34:02 AM1/2/21
to Prawn
Hi,

I have replaced carrierwave by active_storage and have stored a company logo (jpg) in S3.

In a view it worked immediately with  
 <%= image_tag(@company.photo) if @company.photo.attached? %>

But in a prawn  with 
 image @company.photo.to_s , :width => 250, fit: [270, 270], position: :center

it worked not. I get: #<ActiveStorage::Attached::One:0x5573ad70> not found

Any recommendations?

Alexander Mankuta

unread,
Jan 2, 2021, 6:36:14 AM1/2/21
to Prawn
Hi,

You'd need to download the image before you can use it with Prawn.

Please refer to Active Storage guide section 6. Downloading Files.

Herbert Schmoll

unread,
Jan 2, 2021, 10:25:10 AM1/2/21
to Prawn
 Thanks,   download it to the public folder? and the image "#{RAILS_ROOT}/public/images/xxx.jpg" ?

Alexander Mankuta

unread,
Jan 2, 2021, 12:17:00 PM1/2/21
to Prawn
It's up to you.

However, since Prawn accepts IO objects for images you can do something like this:

@company.photo.download do |image|
  pdf.image image, width: 250, fit: [270, 270], position: :center
end

Herbert Schmoll

unread,
Jan 2, 2021, 3:01:03 PM1/2/21
to Prawn
If have still a problem . Here is my code:

if @company.photo.attached?
    @logo = @company.photo.download
end

respond_to do |format|
   format.html
   format.pdf do
      pdf = ValuesPdf.new(current_user, current_company, @logo, view_context)
      send_data pdf.render, filename: @documentname, type: "application/pdf", disposition: "attachment"
  end
end

And inside of ValuesPdf: 

def initialize(user, company, logo, view)
   @user = user
   @company = company
   @logo = logo
   @view = view
end

def first_page

bounding_box([0, cursor] , width: 500 ) do
   if @company.photo.attached?
      image @logo, :width => 250, fit: [270, 270], position: :center
  end
end

If I do so I get 

pathname contains null byte 
Reply all
Reply to author
Forward
0 new messages