在Rails Paperclip中生成PDF缩略图

12

如何在Paperclip中生成PDF的第一页作为缩略图?

我尝试了很多次,但都没有成功。

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

这将会以链接的形式给出PDF的名称,但不会给出PDF的第一页。

<%= link_to 'My PDF', @book.book_url.url %> 
3个回答

9
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

1
我只是在PowerPoint文件中使用了一个存货占位图像。我的设计师制作了这个:https://github.com/YaleDHLab/voices/blob/master/app/assets/images/placeholder_images/ppt.png 我们还为其他文件类型使用类似的占位图像,例如Excel电子表格等:https://github.com/YaleDHLab/voices/blob/master/app/assets/images/placeholder_images/xls.png - duhaime

1

我想我曾经通过强制文件类型来使其正常工作,例如:

:thumb => ["100x100#", :png]

当然,这并不是理想的,因为它会强制针对每个上传使用此文件类型。

不,它不起作用。有一个PDF文件,所以我们首先需要将其转换为第一个文件的PNG格式。它只提供文件的名称,仅此而已。 - Murali K

0

非常感谢 @duhaime 提供的精美答案。

由于这是我找到的最完整的信息来源,还附带了 PDF,所以我想进一步记录它:

要求:

  • imagemagick
  • ghostscript(我忘记了这个)
  • (可选)如果您想处理视频文件,则需要 ffmpeg

此外,我用单个代码替换了 has_default_image?apply_post_processing?

  def can_thumbnail?
    self.check_file_type.try(:{], :thumb).present?
  end

最后,我创建了一个方法来处理不可拇指化的附件:
  def thumb
    return self.file.url(:thumb) if self.can_thumbnail?
    if self.is_video?
      '/thumb/video.png'
    else
      '/thumb/default.png'
    end
  end

但再次感谢@duhaime


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接