aboutsummaryrefslogtreecommitdiffstats
path: root/app/services
diff options
context:
space:
mode:
Diffstat (limited to 'app/services')
-rw-r--r--app/services/pdf_processor_service.rb20
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}"