使用 Rails Active Storage 渲染 Wicked-PDF 图像

Kaz*_*min 7 wicked-pdf rails-activestorage ruby-on-rails-5.2

我无法让 wicked_pdf 将图像从活动存储显示为 pdf 文件。我是否使用:wicked_pdf_image_tagwicked_pdf_asset_base64image_tag在 pdf 模板中使用。然后我给一个rails_blob_path(company.logo)或只是company.logo给任何其他方法?

Uni*_*key 6

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
Run Code Online (Sandbox Code Playgroud)

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 %>">
Run Code Online (Sandbox Code Playgroud)