Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Activestorage Docx Previewer
# lib/activestorage_docx_previwer
module ActiveStorage
class Previewer::DocxPreviewer < Previewer
class << self
# also would be supported by unoconv
# "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword", "application/vnd.ms-powerpoint", "application/vnd.oasis.opendocument.text", "application/vnd.openxmlformats-officedocument.presentationml.presentation"
def accept?(blob)
blob.content_type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
end
end
def preview
download_blob_to_tempfile do |input|
draw_first_page_from input do |output|
yield io: output, filename: "#{blob.filename.base}.png", content_type: "image/png"
end
end
end
private
def draw_first_page_from(file, &block)
draw 'unoconv', '-f', 'pdf', '-e', 'PageRange=1', '--stdout', file.path do |output_pdf|
if File.size(output_pdf) > 0
make_image_from_pdf(output_pdf, &block)
end
end
end
def make_image_from_pdf(output_pdf, &block)
previewer = Rails.application.config.active_storage.previewers.find { |i|
i.accept?(OpenStruct.new(content_type: 'application/pdf'))
}
previewer.new(nil).send(:draw_first_page_from, output_pdf, &block)
end
end
end

Docx (and other Office doc) previewer support for ActiveStorage

Converts all document formats using LibreOffice first to PDF and then to an image, using build-in pdf previwer of ActiveStorage

  • needs: unoconv, a command line tool which uses libreoffice, e.g. apt install unoconv
  • needs a PDF converter, because libreoffice will make a pdf from the office document, Mupdf oder Poppler are already integrated into ActiveStorage, and poppler is free to use, e.g. apt install poppler-utils
# config/initializers/active_storage.rb
require 'activestorage_docx_previewer'
Rails.application.config.active_storage.previewers << ActiveStorage::Previewer::DocxPreviewer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.