PDF preview after Rails 7 upgrade not working. Comments on fix?

I have upgraded to rails 7. Removed mini_magic as a direct gem and as library on the linux host. Added libvips42 on the linux host and complied with changes in the upgrade document.

For image types all is well.

For PDF (application/pdf) uploads are working fine. Thumbnails not so such.

My model:

  has_one_attached :file do |attachable|
    attachable.variant :thumb, resize_to_limit: [200, 200]
  end

the view:

  <% if row.file.previewable? %>
      <%= link_to(image_tag(row.file.preview(:thumb)), rails_blob_path(row.file, disposition: "attachment")) %>
  <% elsif row.file.variable? %>
        <%= link_to(image_tag(row.file.variant(:thumb)), rails_blob_path(row.file, disposition: "attachment"))%>
  <% else %>
        <%= link_to "Download file", rails_blob_path(row.file, disposition: "attachment") %>
  <% end %>

The view should most likely bechanged to use representable? instead of the if-then-else, but that is for later :slight_smile:

For PDFs I just the get download file option and that works just fine as mentioned.

In the rails console both representable?, variable? and previewable? returns false for the (pdf) file on the record.

The logs shows equal behaviour (no errors) for both image types and PDF (logs are rather long, so not attached here).

Versions:

poppler 20.09.0-3.1+deb11u1
libvips42 8.10.5-2

gems:

image_processing (1.12.2)
      mini_magick (>= 4.9.5, < 5)
      ruby-vips (>= 2.0.17, < 3)
activestorage (= 7.0.4)

Reading the documentation and code in (activestorage/app/models/active_storage/blob/representable.rb) i came on to that:

  def variable?
    ActiveStorage.variable_content_types.include?(content_type)
  end

and that my ActiveStorage.variable_content_types are:

["mage/png",

"image/gif",

"image/jpg",

"image/jpeg",

"image/pjpeg",

"image/tiff",

"image/bmp",

"image/vnd.adobe.photoshop",

"image/vnd.microsoft.icon",

"image/webp",

"image/avif",

"image/heic",

"image/heif"]

No “application/pdf” which may be reasonable as PDFs are not handled by ActiveStorage.

I have now added to my config/initializers/active_storage.rb Rails.application.config.active_storage.variable_content_types << "application/pdf

I restarted my server and now my previews shows for PDFs.

Is this a correct fix? If yes, can anyone point me to where if have missed something in the docs? Or should this be added to the docs? If no, what is a better fix? :slight_smile:

Thanks in advance

Jens

Not all packaged versions of libvips support PDF. Try changing the image processor back to image_magick and check if PDF previews are working again.

If yes, you will need to add support to it to libvips (or configure it to delegate formats it does not support to imagemagick). If it still does not work, post back and we will try to figure out.

Hi Thanks for the reply. I played around with it and it does seem to the case. For now I will leave my solution as i until i have the time to understand how to compile a better version and included in the best way in our builds.

Thanks Jens