Posted almost 6 years ago. Visible to the public.

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

Assert rmagick provision …

Gemfile

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

config/initializer/carrierwave.rb

Copy
require 'carrierwave/processing/rmagick'

… and define a custom processor

MyUploader.rb

Copy
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
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Martin Straub
Last edit:
over 4 years ago
by Thomas Klemm
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Martin Straub to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more