如何使用回形针将多页pdf缩略图化 [英] How to thumbnail a multi-page pdf with paperclip

查看:120
本文介绍了如何使用回形针将多页pdf缩略图化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让Paperclip为上载的多页PDF文件的每一页创建2个缩略图.

I'd like to have Paperclip create 2 thumbnails for each page of a multipage PDF file that is uploaded.

我正在运行Paperclip 2.3.1.1,并在我的Asset模型中使用它:

I'm running Paperclip 2.3.1.1 and using this in my Asset model:

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

因此,当我上传一个3页的pdf文件时,我希望这会在每页上创建2个大拇指(一个800x600像素,另一个较小的图像100x100).相反,我创建了3个文件夹(thumb,medium,original)-原始文件夹包含origianl pdf文件,而thumb和medium分别包含一个pdf,而pdf的第一页全部像素化.

So, when I upload a 3 page pdf file, I was hoping this would create 2 thumbs per page (one at 800x600 and a smaller image at 100x100). Instead, I get a 3 folders created (thumb, medium, original) - the original folder contains the origianl pdf file, while thumb and medium each contain a pdf with just the first page of the pdf all pixelated.

我需要做什么才能使回形针为pdf的每一页创建2个大拇指?理想情况下,我希望每页这样一张图片(创建6张图片):

What do I need to do to get paperclip to create 2 thumbs for each page of the pdf? Ideally, I'd like one image per page like this (6 images created):

assets/1/medium/file-0.png

assets/1/medium/file-0.png

assets/1/medium/file-1.png

assets/1/medium/file-1.png

assets/1/medium/file-2.png

assets/1/medium/file-2.png

assets/1/thumb/file-0.png

assets/1/thumb/file-0.png

assets/1/thumb/file-1.png

assets/1/thumb/file-1.png

assets/1/thumb/file-2.png

assets/1/thumb/file-2.png

有人知道该怎么做吗?我需要自定义处理器吗?如果是这样,处理器将是什么样子?

Does anyone know how to do this? Do I need a custom processor? If so, what would the processor look like?

谢谢.

推荐答案

这是我实现类似任务的方式.

Here how I implemented similar task.

文档模型:

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

这篇关于如何使用回形针将多页pdf缩略图化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆