Generate Thumbnail From pdf in rails paperclip
Asked Answered
A

3

12

How can I generate the first page of a pdf as a thumbnail in paperclip?

I tried a lot but it's not working

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

This is giving the name of the pdf as a link but it's not giving the first page of the pdf

<%= link_to 'My PDF', @book.book_url.url %> 
Allerus answered 4/1, 2014 at 11:12 Comment(0)
O
9

Tadas' answer is right, but for those who need more context, you can do something like this: The model below only creates thumbnails for certain kinds of files (e.g. it doesn't make thumbnails of audio files), but does make thumbnails for pdfs, image files, and video files:

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
Orton answered 27/4, 2016 at 19:32 Comment(1)
I just used a stock placeholder image for powerpoint files. My designer made this: github.com/YaleDHLab/voices/blob/master/app/assets/images/… We used similar placeholder images for other file types, like Excel spreadsheets etc: github.com/YaleDHLab/voices/blob/master/app/assets/images/…Orton
A
1

I think I once got it working by enforcing a file type, e.g.

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

of course it's not ideal, because it enforces this filetype for every upload

Apsis answered 4/1, 2014 at 12:0 Comment(1)
no it's not working pdf is there so first we need to convert into the png of the first.it's giving only the name of the file that's itAllerus
A
0

Thanks a million to @duhaime for his beautiful answer.

Since this is the most complete source of information I found to have PDF attached, I'd like to document it further:

Requirements:

  • imagemagick
  • ghostscript (I forgot about this one)
  • (optional) ffmpeg if you want to handle video files

Also I replaced has_default_image? and apply_post_processing? with the single:

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

Finally I created a method for the not-thumbable attachments:

  def thumb
    return self.file.url(:thumb) if self.can_thumbnail?
    if self.is_video?
      '/thumb/video.png'
    else
      '/thumb/default.png'
    end
  end

But thanks again @duhaime

Adenoidal answered 7/11, 2017 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.