Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Images won't load from Active Storage using Amazon S3 Bucket #1097

Open
daveomcd opened this issue Jan 17, 2024 · 2 comments
Open

Images won't load from Active Storage using Amazon S3 Bucket #1097

daveomcd opened this issue Jan 17, 2024 · 2 comments

Comments

@daveomcd
Copy link

I began having issues rendering images on my PDFs using Active Storage and images from an Amazon S3 bucket. I tried several things and couldn't get anything to work for me. I wanted to post a solution that helped me patch it for now until I find another long-term answer for PDF generation after reading about wkhtmltopdf ceasing future developments.

I created a helper function like so ...

def image_for_pdf(image_blob)
  if image_blob.attached?
    Tempfile.create([image_blob.filename.base, image_blob.filename.extension_with_delimiter], binmode: true) do |file|
      file.write(image_blob.download)
      file.rewind
      base64_data = Base64.encode64(file.read)
      "data:#{image_blob.content_type};base64,#{base64_data}"
    end
  end
end

and then I proceeded to call for an image like this ...

<% temp_image_path = image_for_pdf(object.attached_image) %>
<%= image_tag(temp_image_path) if temp_image_path.present? %>

Hopefully, this helps others as I didn't see another working solution. Also if anyone notices a problem with this code please let me know. I was nervous about creating the temporary files, but I believe using the block as I have it will delete them after use? But perhaps someone could point out my problem if that's not the case.

@n-at-han-k
Copy link

This is great

1 similar comment
@FullStackDev0331
Copy link

This is great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants