wicked_pdf_image_tag Tag for ActiveStorage::Attachment not working

Created on 22 Jan 2018  路  5Comments  路  Source: mileszs/wicked_pdf

wicked_pdf_image_tag Tag does not support Rails 5.2 ActiveStorage.

I created a workaround for people having this issue in the meantime:

def wicked_image_active_storage_workaround( image )
  if image.is_a? ActiveStorage::Attachment
    save_path = Rails.root.join( "tmp", "#{image.id}.jpg")
    File.open(save_path, 'wb') do |file|
      file << image.blob.download
    end
    return save_path.to_s
  end
end

In the view

image_tag wicked_image_active_storage_workaround( image )

Keep in mind to clean up the tmp folder from time to time, or delete the image directly after using it.

Looking forward to a clean solution of this!

asset-helpers feature-request

Most helpful comment

None of the above worked for me, most likely due to the fact that I generate the PDF in a background job. Here's what I ended up with :

def wicked_blob_path(file)
  ActiveStorage::Blob.service.send(:path_for, file.blob.key)
end
= image_tag(wicked_blob_path(user.avatar), alt: '_')

All 5 comments

I ran into the same issue, and simply skipped the tag helper like this:

<img src="<%= file.service_url %>">

or if you need resizing, like this:

<img src="<%= file.variant(resize: "590").processed.service_url %>">

These examples are great. I'd love to see a PR that makes WickedPDF work with ActiveStorage better.

I think the first example would be almost good enough (but perhaps named something like wicked_active_storage_asset, and maybe using WickedPdfTempfile).

Do you know if this same technique would work for an AS::Blob or non-attachment, or are they all attachments at that point?

None of the above worked for me, most likely due to the fact that I generate the PDF in a background job. Here's what I ended up with :

def wicked_blob_path(file)
  ActiveStorage::Blob.service.send(:path_for, file.blob.key)
end
= image_tag(wicked_blob_path(user.avatar), alt: '_')

Although using service_url works for my apps when I tested Active Storage, its not ideal though, see Rails API.
It would be nice if we could just keep using image_tag + Active Storage without any additional helpers like in the first example, which would only work with template files specifically made for Wicked PDF.

After all what makes Wicked PDF so great is that one can use the same template files for serving Webbrowsers and creating PDF files.

I use Wicked PDF for around 3 years now on my background workers (thanks @unixmonkey 馃挴), and never had any problems with my images (I use Shrine instead of Active Storage).

Here is the solution we ended up using, allowing us to use Disk storage in development and S3 on production:

module PDFHelper
  def wicked_blob_path(active_storage_attachment)
    service = active_storage_attachment.service

    case service
    when ActiveStorage::Service::DiskService
      service.path_for(active_storage_attachment.blob.key)
    when ActiveStorage::Service::S3Service
      active_storage_attachment.service_url
    else
      raise "Unsupported ActiveStorage service for WickedPDF integration: #{service.name}"
    end
  end
end

In our view files we used this helper as normal:

%section
  = image_tag(wicked_blob_path(organization.logo))
Was this page helpful?
0 / 5 - 0 ratings

Related issues

lafeber picture lafeber  路  5Comments

andheiberg picture andheiberg  路  3Comments

lisamburns picture lisamburns  路  3Comments

douglasep picture douglasep  路  4Comments

netwire88 picture netwire88  路  7Comments