Howto provide a single page preview for PDF & TXT with carrierwave

Updated . Posted . Visible to the public.

Assert rmagick provision ...

Gemfile

gem 'rmagick', '2.13.2' # at this moment the latest stable version

config/initializer/carrierwave.rb

require 'carrierwave/processing/rmagick'

... and define a custom processor

MyUploader.rb

class MyUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick

  def cover
    manipulate! do |frame, index|
      frame if index.zero? # take only the first page of the file
    end
  end

  version :preview do
    process :cover
    process :resize_to_fit => [310, 438]
    process :convert => :jpg

    def full_filename (for_file = model.source.file)
      super.chomp(File.extname(super)) + '.jpg'
    end
  end

  ... # the other stuff

end

Notes

  • Only PDF and TXT files (and of course images) are previewable to my knowledge till dato.
  • Don't forget to provide rmagick on your server
Martin Straub
Last edit
Deleted user #3651
License
Source code in this card is licensed under the MIT License.
Posted by Martin Straub to makandra dev (2013-12-05 09:07)