2010-08-16 100 views
5

我希望Paperclip爲上載的多頁PDF文件的每個頁面創建2個縮略圖。如何使用回形針縮略多頁PDF

我運行回形針2.3.1.1,在我的資產模型中使用這樣的:

has_attached_file :asset, 
        :styles => { :medium => "800x600>", :thumb => "100x100>" } 

所以,當我上傳3頁的PDF文件,我希望這將創建每頁2個大拇指(一個在800x600和100x100更小的圖像)。相反,我得到了一個創建的3個文件夾(thumb,medium,original) - 原始文件夾包含origianl pdf文件,而thumb和medium均包含pdf,只有pdf的第一頁全部爲像素化。

我需要做什麼才能讓回形針爲PDF的每個頁面創建2個大拇指?理想情況下,我想在每個網頁的一個圖像像這樣(在6個圖像):


資產/ 1 /中/文件0.png

資產/ 1 /中/文件-1。 PNG

資產/ 1 /中/文件2.png

資產/ 1 /拇指/文件0.png

資產/ 1 /拇指/文件1.png

assets/1/thumb/file-2.png

有沒有人知道如何做到這一點?我需要定製處理器嗎?如果是這樣,處理器會是什麼樣子?

謝謝。

回答

9

這裏我怎麼實現類似的任務。

文檔模型:

class Document < ActiveRecord::Base 

    has_many :pages, :dependent => :destroy 

    has_attached_file :asset 

    after_asset_post_process :make_pages 

    private 

    def make_pages 
    if valid? 
     Paperclip.run('convert', "-quality #{Page::QUALITY} -density #{Page::DENSITY} #{asset.queued_for_write[:original].path} #{asset.queued_for_write[:original].path}%d.png") 
     images = Dir.glob("#{asset.queued_for_write[:original].path}*.png").sort_by do |line| 
     line.match(/(\d+)\.png$/)[1].to_i 
     end 

     images.each do |page_image| 
     pages.build(:asset => File.open(page_image)) 
     end 
     FileUtils.rm images 
    end 
    end 
end 

頁型號:

class Page < ActiveRecord::Base 

    belongs_to :document 

    has_attached_file :asset 

    QUALITY = 100 
    DENSITY = '80x80' 

end 
+0

我已經嘗試這種解決方案,但convert命令似乎只產生第一頁1個圖像。否則它效果很好。有任何想法嗎? – 2013-07-04 05:38:08

+1

您可以使用來自終端中的Imagemagick軟件包的「convert」命令來進行調試。 – taro 2013-07-04 10:08:26

+0

謝謝,我一直在玩這個。仍然沒有得到它的工作,只爲第一頁生成1張圖片。 – 2013-07-04 18:58:32

2

我有一個半工作的解決方案...但這不是很優雅。我真的想提出更好的建議,但我想我會分享。

我開始時定義了一堆新樣式,每個頁面一個樣式......一直到我希望能夠處理的很多頁面。 (愚蠢的,我知道,但我不知道如何訪問路徑插值的曲別針,使每個頁面獲取保存/店裏正確刪除,除非對每個圖像的獨特風格)

{ ... 
: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]}, 
} 

然後...我有一個自定義處理器,從縮略圖處理器的子類,有一些額外的邏輯用於運行帶有正確頁面的轉換命令#。

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