I have a half-working solution to this... but it isn't very elegant. I'd really like to come up with something better, but I thought I'd share anyway.
I started by defining a bunch of new styles, one for each page... up to however many pages I want to be able to handle. (stupid, I know, but I don't know how to access the path interpolations in Paperclip so that each page gets saved/deleted in the store properly unless there is a unique style for each image)
{ ...
:page_0 => {:geometry=>'800[0]', :format=>:png, :processors=>[:multipage_thumbnail]},
:page_1 => {:geometry=>'800[1]', :format=>:png, :processors=>[:multipage_thumbnail]},
:page_2 => {:geometry=>'800[2]', :format=>:png, :processors=>[:multipage_thumbnail]},
:page_3 => {:geometry=>'800[3]', :format=>:png, :processors=>[:multipage_thumbnail]},
:page_4 => {:geometry=>'800[4]', :format=>:png, :processors=>[:multipage_thumbnail]},
:page_5 => {:geometry=>'800[5]', :format=>:png, :processors=>[:multipage_thumbnail]},
}
Then... I have a custom processor which subclasses from the Thumbnail processor, with some extra logic for running the convert command with the proper page #.
module Paperclip
# Handles thumbnailing images that are uploaded.
class MultipageThumbnail < Thumbnail
# Creates a Thumbnail object set to work on the +file+ given. It
# will attempt to transform the image into one defined by +target_geometry+
# which is a "WxH"-style string. +format+ will be inferred from the +file+
# unless specified. Thumbnail creation will raise no errors unless
# +whiny+ is true (which it is, by default. If +convert_options+ is
# set, the options will be appended to the convert command upon image conversion
def initialize file, options = {}, attachment = nil
@page = options[:geometry].match(/\[(\d+)\]/)[1] rescue 0
@page ||= 0
options[:geometry] = options[:geometry].sub(/\[\d+\]/, '')
super
end
# Performs the conversion of the +file+ into a thumbnail. Returns the Tempfile
# that contains the new image.
def make
return nil if @page >= page_count
src = @file
dst = Tempfile.new([@basename, @format].compact.join("."))
dst.binmode
begin
options = [
source_file_options,
"#{ File.expand_path(src.path) }[#{@page}]",
transformation_command,
convert_options,
"#{ File.expand_path(dst.path) }"
].flatten.compact
success = Paperclip.run("convert", *options)
rescue PaperclipCommandLineError => e
raise PaperclipError, "There was an error processing the thumbnail for #{@basename}" if @whiny
end
dst
end
def page_count
@page_count ||= begin
files = Paperclip.run("identify", "#{@file.path}")
files.split(/\n/).size
rescue PaperclipCommandLineError
1
end
end
end
end