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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With