You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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 ...
and then I proceeded to call for an image like this ...
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.
The text was updated successfully, but these errors were encountered: