Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF not previewable with rails active storage?

Following the Rails guide, apparently PDF previews are supported "out of the box" by rails' ActiveStorage.

some non-image files can be previewed: that is, they can be presented as images. For example, a video file can be previewed by extracting its first frame. Out of the box, Active Storage supports previewing videos and PDF documents.

And the code sample it gives is this:

<ul>
  <% @message.files.each do |file| %>
    <li>
      <%= image_tag file.preview(resize_to_limit: [100, 100]) %>
    </li>
  <% end %>
</ul>

I uploaded a pdf using this code, and I got this error:

ActiveStorage::UnpreviewableError

Maybe there is something I'm missing?

Update

Okay, I think I know what the problem is. I'm supposed to install those third party libraries like muPDF on my machine or on the "Server" for this to work. Running brew install muPDF failed for me though. I'll keep researching.

like image 983
Angel Garcia Avatar asked Oct 15 '25 18:10

Angel Garcia


1 Answers

I may be a little late for you, but for those who still might look for an answer, installing the gem poppler worked for me.

In Gemfile

gem 'poppler' 

Then

bundle install

Now the preview works ! And you can also use file.previewable? to check if the file can be previewed. With the example above :

<ul>
  <% @message.files.each do |file| %>
    <%= if file.previewable? %>
      <li>
        <%= image_tag file.preview(resize_to_limit: [100, 100]) %>
      </li>
    <% end %>
  <% end %>
</ul>
like image 157
GmS Avatar answered Oct 18 '25 11:10

GmS