2010-08-16 42 views
5

Tôi muốn có Paperclip tạo 2 hình thu nhỏ cho mỗi trang của tệp PDF nhiều trang được tải lên.Cách thu nhỏ một trang nhiều pdf với kẹp giấy

Tôi đang chạy Kẹp giấy 2.3.1.1 và sử dụng này trong mô hình tài sản của tôi:

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

Vì vậy, khi tôi tải lên một tập tin pdf 3 trang, tôi đã hy vọng điều này sẽ tạo ra 2 ngón tay trên mỗi trang (một ở 800x600 và hình ảnh nhỏ hơn ở mức 100x100). Thay vào đó, tôi nhận được 3 thư mục được tạo (thumb, medium, original) - thư mục gốc chứa tệp pdf origianl, trong khi ngón tay cái và phương tiện chứa một pdf chỉ với trang đầu tiên của pdf tất cả được pixelated.

Tôi cần làm gì để lấy kẹp giấy để tạo 2 ngón tay cái cho mỗi trang của pdf? Lý tưởng nhất, tôi muốn một hình ảnh trên một trang như thế này (6 hình ảnh được tạo ra):


tài sản/1/trung bình/file-0.png

tài sản/1/trung bình/file-1. png

tài sản/1/trung bình/file-2.png

tài sản/1/thumb/file-0.png

tài sản/1/thumb/file-1.png

asset/1/thumb/file-2.png

Có ai biết cách làm điều này không? Tôi có cần bộ xử lý tùy chỉnh không? Nếu vậy, bộ xử lý sẽ trông như thế nào?

Cảm ơn.

Trả lời

9

Ở đây cách tôi triển khai tác vụ tương tự.

mô hình tài liệu:

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 

mô hình trang:

class Page < ActiveRecord::Base 

    belongs_to :document 

    has_attached_file :asset 

    QUALITY = 100 
    DENSITY = '80x80' 

end 
+0

Tôi đã cố gắng giải pháp này nhưng lệnh chuyển đổi dường như chỉ tạo ra 1 hình ảnh cho trang đầu tiên. Nếu không nó hoạt động rất tốt. Bất kỳ ý tưởng? –

+1

Bạn có thể chơi với lệnh 'convert' từ gói Imagemagick trong terminal để gỡ lỗi nó. – taro

+0

Cảm ơn, tôi đã chơi đùa với điều đó. Vẫn chưa nhận được nó để làm việc, chỉ tạo ra 1 hình ảnh cho trang đầu tiên. –

2

Tôi có một giải pháp nửa làm việc cho điều này ... nhưng nó không phải là rất thanh lịch. Tôi thực sự muốn nghĩ ra một cái gì đó tốt hơn, nhưng tôi nghĩ tôi sẽ chia sẻ.

Tôi bắt đầu bằng cách xác định một loạt các kiểu mới, một cho mỗi trang ... tuy nhiên, nhiều trang tôi muốn có thể xử lý. (Ngu ngốc, tôi biết, nhưng tôi không biết làm thế nào để truy cập vào đường dẫn trong interpolations Kẹp giấy để mỗi trang được lưu/xóa trong cửa hàng đúng trừ khi có một phong cách độc đáo cho mỗi hình ảnh)

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

Sau đó, ... Tôi có một bộ xử lý tùy chỉnh mà lớp con từ bộ xử lý hình thu nhỏ, với một số logic bổ sung để chạy lệnh chuyển đổi với trang thích hợp #.

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 
Các vấn đề liên quan