Save 36% for Black Friday! Learn more

Nick Seferos

Joined

50 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in Count the number of pages in a pdf

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!