Ask A Question

Notifications

You’re not receiving notifications from this thread.

Count the number of pages in a pdf

Nick Seferos asked in Rails

Hello!

I am trying to count the number of pages in a PDF for a print service using PDF-reader (https://github.com/yob/pdf-reader).

Here's what I have so far but, I only get a hash of {"identified"=>true} from what I have written.

active_stroage.rb

module ActiveStorage
  class Analyzer
    class PDFAnalyzer < Analyzer
      # Only run this analyzer on PDF files
      def self.accept?(blob) = blob.content_type == 'application/pdf'

      def metadata
        pdf_reader = download_blob_to_tempfile { |file| PDF::Reader.new(file) }

        {
          # If there are other things PDF::Reader can tell you about the file, you can add them here.
          page_count: pdf_reader.page_count
        }
      end
    end
  end
end

# Add the analyzer to the default set so that it gets run on every upload
Rails.application.config.active_storage.analyzers << ActiveStorage::Analyzer::PDFAnalyzer

and in the view:

  <p class="text-sm font-medium text-gray-500">Page Count</p>
  <p><%= print_product.print_ready_file.metadata['page_count'] %></p>

(which puts nothing...the hash is {"identified"=>true})

Any pointers would be greatly appreciated!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 82,584+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.