diff options
author | Matthew Lemon <y@yulqen.org> | 2024-11-14 12:01:49 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-11-14 12:01:49 +0000 |
commit | 1b64b0b709c5704de48120e20bdfad32f34b0b5d (patch) | |
tree | 8cbb68c52a79d9cf12dfd861fee5fc3d0ac74b99 /app/services/pdf_processor_service.rb | |
parent | 06222c54c248a03d0b019db12bc365ab187e317a (diff) |
Adds credits and cleaner handling of page count using ruby
Diffstat (limited to 'app/services/pdf_processor_service.rb')
-rw-r--r-- | app/services/pdf_processor_service.rb | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/app/services/pdf_processor_service.rb b/app/services/pdf_processor_service.rb index 3f97611..dd3718a 100644 --- a/app/services/pdf_processor_service.rb +++ b/app/services/pdf_processor_service.rb @@ -1,3 +1,5 @@ +require 'pdf-reader' + class PdfProcessorService DEFAULT_OPTIONS = { quality: 40, @@ -37,24 +39,16 @@ class PdfProcessorService def get_page_count(pdf_path) Rails.logger.debug "Checking PDF: #{pdf_path}" - Rails.logger.debug "File exists: #{File.exist?(pdf_path)}" - Rails.logger.debug "File size: #{File.size(pdf_path)}" - Rails.logger.debug "File type: #{`file -b #{pdf_path}`}" - # Try multiple methods to get page count - identify_output = `identify -format %n "#{pdf_path}" 2>&1` - gs_output = `gs -q -dNODISPLAY -c "#{pdf_path} (r) file runpdfbegin pdfpagecount = quit" 2>&1` + reader = PDF::Reader.new(pdf_path) + count = reader.page_count - Rails.logger.debug "Identify output: #{identify_output}" - Rails.logger.debug "Ghostscript output: #{gs_output}" + Rails.logger.debug "PDF page count: #{count}" - count = identify_output.to_i - if count <= 0 - count = gs_output.to_i + if count <= 0 || count > 1000 # Reasonable maximum page limit + raise "Invalid page count: #{count}" end - Rails.logger.debug "Final page count: #{count}" - raise "Invalid page count: #{count}" unless count.positive? count rescue StandardError => e Rails.logger.error "Failed to get page count: #{e.message}" |