Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wicked-PDF image render with Rails Active Storage

I cannot get wicked_pdf to display an image from active storage to a pdf file. Do I use: wicked_pdf_image_tag or wicked_pdf_asset_base64 or just image_tag in the pdf template. Then do I give a rails_blob_path(company.logo) or just company.logo to any other method?

like image 301
Kazmin Avatar asked Jun 06 '18 13:06

Kazmin


1 Answers

There is some ongoing work to add Active Storage support to wicked_pdf in this GitHub issue thread

Until that is added (you can help!), you can create a helper method something like this (which is a slightly modified version of an example from the thread above):

# Use like `image_tag(wicked_active_storage_asset(user.avatar))`
def wicked_active_storage_asset(asset)
  return unless asset.respond_to?(:blob)
  save_path = Rails.root.join('tmp', asset.id.to_s)
  File.open(save_path, 'wb') do |file|
    file << asset.blob.download
  end
  save_path.to_s
end

Or, if you can use web resources directly in your PDF creation process:

<img src="<%= @user.avatar.service_url %>">
<img src="<%= @user.avatar.variant(resize: "590").processed.service_url %>">
like image 91
Unixmonkey Avatar answered Feb 04 '23 06:02

Unixmonkey