ruby-on-rails - 在 rails 回形针中从 pdf 生成缩略图

标签 ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-4 paperclip

如何将 pdf 的第一页生成为回形针中的缩略图?

我尝试了很多但它不起作用

  has_attached_file :book_url, :styles => {
      :thumb => "100x100#",
      :small  => "150x150>",
      :medium => "200x200" }

这是将 pdf 的名称作为链接,但它没有提供 pdf 的第一页
<%= link_to 'My PDF', @book.book_url.url %> 

最佳答案

Tadas 的回答是正确的,但对于那些需要更多上下文的人,您可以执行以下操作: 下面的模型仅为某些类型的文件创建缩略图(例如,它不制作音频文件的缩略图),但确实为pdf、图像文件和视频文件:

class Record < ActiveRecord::Base
  print self # for logging on heroku

  belongs_to :user

  # Ensure user has provided the required fields
  validates :title, presence: true
  validates :file_upload, presence: true
  validates :description, presence: true

  # Use the has_attached_file method to add a file_upload property to the Record
  # class. 
  has_attached_file :file_upload,
    # In order to determine the styles of the image we want to save
    # e.g. a small style copy of the image, plus a large style copy
    # of the image, call the check_file_type method
    styles: lambda { |a| a.instance.check_file_type },

    processors: lambda { 
      |a| a.is_video? ? [ :ffmpeg ] : [ :thumbnail ] 
    }

  # Validate that we accept the type of file the user is uploading
  # by explicitly listing the mimetypes we are willing to accept
  validates_attachment_content_type :file_upload,
    :content_type => [
      "video/mp4", 
      "video/quicktime",

      "image/jpg", 
      "image/jpeg", 
      "image/png", 
      "image/gif",
      "application/pdf",

      "audio/mpeg", 
      "audio/x-mpeg", 
      "audio/mp3", 
      "audio/x-mp3", 

      "file/txt",
      "text/plain",

      "application/doc",
      "application/msword", 

      "application/vnd.ms-excel",     
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",

      "application/vnd.ms-powerpoint"
      ],
    :message => "Sorry! We do not accept the attached file type"

  # Before applying the Imagemagick post processing to this record
  # check to see if we indeed wish to process the file. In the case
  # of audio files, we don't want to apply post processing
  before_post_process :apply_post_processing?

  # Helper method that uses the =~ regex method to see if 
  # the current file_upload has a content_type 
  # attribute that contains the string "image" / "video", or "audio"
  def is_image?
    self.file_upload.content_type =~ %r(image)
  end

  def is_video?
    self.file_upload.content_type =~ %r(video)
  end

  def is_audio?
    self.file_upload.content_type =~ /\Aaudio\/.*\Z/
  end

  def is_plain_text?
    self.file_upload_file_name =~ %r{\.(txt)$}i
  end

  def is_excel?
    self.file_upload_file_name =~ %r{\.(xls|xlt|xla|xlsx|xlsm|xltx|xltm|xlsb|xlam|csv|tsv)$}i
  end

  def is_word_document?
    self.file_upload_file_name =~ %r{\.(docx|doc|dotx|docm|dotm)$}i
  end

  def is_powerpoint?
    self.file_upload_file_name =~ %r{\.(pptx|ppt|potx|pot|ppsx|pps|pptm|potm|ppsm|ppam)$}i
  end

  def is_pdf?
    self.file_upload_file_name =~ %r{\.(pdf)$}i
  end

  def has_default_image?
    is_audio?
    is_plain_text?
    is_excel?
    is_word_document?
  end

  # If the uploaded content type is an audio file,
  # return false so that we'll skip audio post processing
  def apply_post_processing?
    if self.has_default_image?
      return false    
    else
      return true
    end
  end

  # Method to be called in order to determine what styles we should
  # save of a file.
  def check_file_type
    if self.is_image?
      {
        :thumb => "200x200>", 
        :medium => "500x500>"
      }
    elsif self.is_pdf?
      {
        :thumb => ["200x200>", :png], 
        :medium => ["500x500>", :png]
      }

    elsif self.is_video?
      {
        :thumb => { 
          :geometry => "200x200>", 
          :format => 'jpg', 
          :time => 0
        }, 
        :medium => { 
          :geometry => "500x500>", 
          :format => 'jpg', 
          :time => 0
        }
      }
    elsif self.is_audio?
      {
        :audio => {
          :format => "mp3"
        }
      }
    else
      {}
    end
  end

end

关于ruby-on-rails - 在 rails 回形针中从 pdf 生成缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20920212/

相关文章:

ruby-on-rails - 创建哈希迭代对象数组

ruby-on-rails - 在 Rails 中将列数据类型从 bool 值更改为整数

ruby-on-rails - Rails 3 应用程序中未初始化的常量 UsersController::PostComment

ruby-on-rails-3 - 如何在不使用表单的情况下填充数据库/模型?

ruby-on-rails-3 - 在 rails 3.x 中的 youtube 上上传带字幕的视频

ruby-on-rails - 我如何总结多对多的值(value)?

ruby-on-rails - 在 Rails 中按字母顺序排序

ruby-on-rails-3 - 如何在 Rails 3 中使用 Paperclip 上传音频文件?

ruby-on-rails - 无法在Rails 3中跳过验证?

ruby - 带有设计的自定义密码字段( ruby )