Skip to content

Commit

Permalink
Check all the PDFs pages for oversized page dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
michelpmcdonald committed Dec 19, 2024
1 parent 2fcb31f commit 1aa3213
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
18 changes: 16 additions & 2 deletions lib/pdf_info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def self.read(file_or_path)

def initialize(path)
@stdout = []
Open3.popen2e(Settings.binaries.pdfinfo, path) do |_stdin, stdout, wait|
Open3.popen2e(Settings.binaries.pdfinfo, '-l', '-1', path) do |_stdin, stdout, wait|
stdout.each_line do |line|
@stdout.push(force_utf8_encoding(line))
end
Expand All @@ -43,7 +43,7 @@ def pages
end

def page_size
width, height = self['Page size'].scan(/\d+/).map(&:to_i)
width, height = self['Page 1 size'].scan(/\d+/).map(&:to_i)
{ width:, height: }
end

Expand All @@ -55,6 +55,20 @@ def page_size_inches
}
end

def oversized_pages(max_width, max_height)
results = []
(1..pages).each do |page_num|
page_key = page_num < 10_000 ? format('%4d', page_num) : page_num
pw_pts, ph_pts = self["Page #{page_key} size"].scan(/\d+/).map(&:to_i)
pw_inches = convert_pts_to_inches(pw_pts)
ph_inches = convert_pts_to_inches(ph_pts)
if pw_inches > max_width || ph_inches > max_height
results << "Page #{page_num}, Width: #{pw_inches}, Height: #{ph_inches}"
end
end
results
end

def file_size
self['File size'].scan(/\d+/).first.to_i
end
Expand Down
17 changes: 9 additions & 8 deletions lib/pdf_utilities/pdf_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,15 @@ def check_encryption
end

def check_page_size
if @pdf_metadata.present?
dimensions = @pdf_metadata.page_size_inches
width_limit = @options[:width_limit_in_inches]
height_limit = @options[:height_limit_in_inches]

if dimensions[:width] > width_limit || dimensions[:height] > height_limit
@result.add_error("#{PAGE_SIZE_LIMIT_EXCEEDED_MSG} of #{width_limit} in. x #{height_limit} in.")
end
return if @pdf_metadata.blank?

width_limit = @options[:width_limit_in_inches]
height_limit = @options[:height_limit_in_inches]

oversized_pages = @pdf_metadata.oversized_pages(width_limit, height_limit)

if oversized_pages.present?
@result.add_error("#{PAGE_SIZE_LIMIT_EXCEEDED_MSG} of #{width_limit} in. x #{height_limit} in.")
end
end
end
Expand Down
Binary file modified modules/vba_documents/spec/fixtures/10x102.pdf
Binary file not shown.

0 comments on commit 1aa3213

Please sign in to comment.