I'm converting our backend file upload to work with Shrine. I managed to get the image upload and thumbnailing up and running pretty easily but I've been struggling to do the same with PDF files.
The upload itself works, however, I can't manage to generate a thumbnail/preview for the file. I'm using Shrine alongside ImageProcessing and vipslib.
I tried using the thumbnail method provided by vips but that seems to work just with image files and I also tried to follow this SO with no success.
Let me give you now some context:
This is the Shrine initializer
require "shrine"
require "shrine/storage/file_system"
require "shrine/storage/google_cloud_storage"
Shrine.storages = {
cache: Shrine::Storage::GoogleCloudStorage.new(bucket: ENV['CACHE_BUCKET']),
store: Shrine::Storage::GoogleCloudStorage.new(bucket: ENV['STORE_BUCKET'])
}
Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data # for retaining the cached file across form redisplays
Shrine.plugin :restore_cached_data # re-extract metadata when attaching a cached file
Shrine.plugin :determine_mime_type
And this is the Uploader
class DocumentUploader < Shrine
require 'vips'
def generate_location(io, context)
"documents/#{Time.now.to_i}/#{super}"
end
plugin :processing
# plugin :processing # allows hooking into promoting
# plugin :versions # enable Shrine to handle a hash of files
# plugin :delete_raw # delete processed files after uploading
# plugin :determine_mime_type
#
process(:store) do |io, context|
preview = Tempfile.new(["shrine-pdf-preview", ".pdf"], binmode: true)
begin
IO.popen *%W[mutool draw -F png -o - #{io.path} 1], "rb" do |command|
IO.copy_stream(command, preview)
end
rescue Errno::ENOENT
fail "mutool is not installed"
end
preview.open # flush & rewind
versions = { original: io }
versions[:preview] = preview if preview && preview.size > 0
versions
end
end
As mentioned, the uploader, at the moment is what breaks and doesn't generate the preview. The previous version of the file looked like this:
class DocumentUploader < Shrine
require 'vips'
def generate_location(io, context)
"documents/#{Time.now.to_i}/#{super}"
end
plugin :processing
# plugin :processing # allows hooking into promoting
# plugin :versions # enable Shrine to handle a hash of files
# plugin :delete_raw # delete processed files after uploading
# plugin :determine_mime_type
#
process(:store) do |io, context|
thumb = Vips::Image.thumbnail(io.metadata["filename"], 300)
thumb
end
end
I have seen very little documentation online about this topic.
Update: Answering questions
The command vips pdfload
spits out the usage information and it indeed says that PDF would be loaded using libpoppler.
I installed the tar file straight from their download page and the version is 8.7.0 running on a Debian system.
Thanks about the license info - will look into that as well!