class ActiveStorage::Preview
install and use third-party software, make sure you understand the licensing implications of doing so.
These libraries are not provided by Rails. You must install them yourself to use the built-in previewers. Before you
and the other requires muPDF (version 1.8 or newer). To preview PDFs, install either Poppler or muPDF.
. Two PDF previewers are provided: one requires Poppler,
The built-in previewers rely on third-party system libraries. Specifically, the built-in video previewer requires
Outside of a Rails application, modify ActiveStorage.previewers
instead.
# => [ ActiveStorage::Previewer::PDFPreviewer, ActiveStorage::Previewer::VideoPreviewer, DOCXPreviewer ]
Rails.application.config.active_storage.previewers << DOCXPreviewer
# Add a custom previewer for Microsoft Office documents:
# => [ ActiveStorage::Previewer::PDFPreviewer, ActiveStorage::Previewer::VideoPreviewer ]
Rails.application.config.active_storage.previewers
by manipulating Rails.application.config.active_storage.previewers
in an initializer:
first previewer for which accept?
returns true when given the blob. In a Rails application, add or remove previewers
To choose the previewer for a blob, Active Storage calls accept?
on each registered previewer in order. It uses the
documentation for more details on what’s required of previewers.
subclassing ActiveStorage::Previewer and implementing the requisite methods. Consult the ActiveStorage::Previewer
ActiveStorage::Previewer::VideoPreviewer and ActiveStorage::Previewer::PDFPreviewer. Build custom previewers by
A previewer extracts a preview image from a blob. Active Storage provides previewers for videos and PDFs:
extracting its first frame, and a PDF blob can be previewed by extracting its first page.
Some non-image blobs can be previewed: that is, they can be presented as images. A video blob can be previewed by
def image
def image blob.preview_image end
def initialize(blob, variation_or_variation_key)
def initialize(blob, variation_or_variation_key) @blob, @variation = blob, ActiveStorage::Variation.wrap(variation_or_variation_key) end
def previewer
def previewer previewer_class.new(blob) end
def previewer_class
def previewer_class ActiveStorage.previewers.detect { |klass| klass.accept?(blob) } end
def process
def process previewer.preview { |attachable| image.attach(attachable) } end
def processed
Processing a preview generates an image from its blob and attaches the preview image to the blob. Because the preview
blob.preview(resize_to_limit: [100, 100]).processed.service_url
Processes the preview if it has not been processed yet. Returns the receiving Preview instance for convenience:
def processed process unless processed? self end
def processed?
def processed? image.attached? end
def service_url(**options)
This method synchronously processes a variant of the preview image, so do not call it in views. Instead, generate
preview has not been processed yet.
Returns the URL of the preview's variant on the service. Raises ActiveStorage::Preview::UnprocessedError if the
def service_url(**options) if processed? variant.service_url(options) else raise UnprocessedError end end
def variant
def variant ActiveStorage::Variant.new(image, variation).processed end