How to determine the number of pages in a PDF application in a Rails application?

In my application, a user can download a PDF as an attachment.

I want to let you know how many pages it has.

Is there a Ruby library that can help with this?

+2


source to share


1 answer


  require 'rubygems'
  require 'pdf/reader'

  class PageReceiver
    attr_accessor :pages

    # Called when page parsing ends
    def page_count(arg)
      @pages = arg
    end
  end

  receiver = PageReceiver.new
  pdf = PDF::Reader.file("somefile.pdf", receiver, :pages => false)
  puts "#{receiver.pages} pages"

      



+1


source







All Articles