2010-08-16 12 views
5

Paperclip, yüklenen çok sayfalı bir PDF dosyasının her sayfası için 2 küçük resim oluşturmasını isterim.Kağıt ataması ile çok sayfalı pdf nasıl görüntülenir

Ben Paperclip 2.3.1.1 çalışan ve benim Varlık modelinde bu kullanıyorum: Ben bu sayfa başına 2 başparmak oluşturmak umuyordum (bir 3 sayfa pdf dosyası yüklediğinizde

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

, 800x600'de ve 100x100'de daha küçük bir görüntüde). Bunun yerine, oluşturduğum 3 klasör (başparmak, orta, orijinal) - orijinal klasör origianl pdf dosyasını içerirken, başparmağım ve ortalamanın her biri pdf'nin sadece ilk sayfası olan bir pdf içeriyor.

PDF'in her sayfası için 2 başparmak oluşturmak için ataç almak için ne yapmam gerekir?


varlıklar/1/orta/dosya 0.png

varlıklar/1/orta/dosya-1: İdeal olarak, ben (yaratılmış 6 görüntüleri) böyle sayfa başına bir görüntü istiyorum. png

varlıklar/1/orta/dosya 2.png

varlıklar/1/başparmak/dosya-0.png

varlıklar/1/başparmak/dosya-1.png

assets/1/thumb/file-2.png

Bunu nasıl yapacağını bilen var mı? Özel bir işlemciye ihtiyacım var mı? Eğer öyleyse, işlemci neye benziyordu?

Teşekkürler.

cevap

9

Burada benzer görevi nasıl uygularım.

Belge modeli:

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 

Sayfa modeli:

class Page < ActiveRecord::Base 

    belongs_to :document 

    has_attached_file :asset 

    QUALITY = 100 
    DENSITY = '80x80' 

end 
+0

Bu çözümü çalışıyorum ettik ama dönüştürme komutu sadece ilk sayfa için 1 resim oluşturmak gibi görünüyor. Aksi halde harika çalışıyor. Herhangi bir fikir? –

+1

Terminaldeki Imagemagick paketinden 'convert' komutunu kullanarak debug olarak oynayabilirsiniz. – taro

+0

Teşekkürler, bununla oynuyorum. Hala çalışmaya başlamadı, sadece ilk sayfa için 1 resim oluşturdu. –

2

Buna yarım çalışma çözümüm var ... ama çok şık değil. Daha iyi bir şey bulmak isterdim, ama yine de paylaşacağımı düşündüm.

Her sayfa için bir tane yeni stil tanımlayarak başladım ... ancak işlemek için kullanabileceğim pek çok sayfaya kadar. (Aptal, biliyorum, ama her resim için benzersiz bir stil olmadıkça ben her sayfa düzgün deposunda silinen/kurtulur böylece Paperclip içinde yol ara değerlemeler erişmek nasıl bilmiyorum) Sonra

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

... Convertnail komutunu uygun sayfa # ile çalıştırmak için ekstra bir mantıkla, Thumbnail işlemcisinden altsınıflar içeren özel bir işlemcim var.

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 
İlgili konular