diff --git a/lib/pdf_info.rb b/lib/pdf_info.rb index 6f6d9dd9624..5b6b6657ea0 100644 --- a/lib/pdf_info.rb +++ b/lib/pdf_info.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/pdf_utilities/pdf_validator.rb b/lib/pdf_utilities/pdf_validator.rb index 7511c1058e1..0c8b2b19528 100644 --- a/lib/pdf_utilities/pdf_validator.rb +++ b/lib/pdf_utilities/pdf_validator.rb @@ -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 diff --git a/modules/vba_documents/lib/vba_documents/pdf_inspector.rb b/modules/vba_documents/lib/vba_documents/pdf_inspector.rb index d0610d668f8..6330d3238b6 100644 --- a/modules/vba_documents/lib/vba_documents/pdf_inspector.rb +++ b/modules/vba_documents/lib/vba_documents/pdf_inspector.rb @@ -91,7 +91,7 @@ def pdf_metadata(pdf) dimensions: { height: dimensions[:height].round(2), width: dimensions[:width].round(2), - oversized_pdf: dimensions[:height] > max_height || dimensions[:width] > max_width + oversized_pdf: metadata.oversized_pages(max_width, max_height).any? }, file_size: metadata.file_size, sha256_checksum: Digest::SHA256.file(pdf).hexdigest diff --git a/modules/vba_documents/spec/fixtures/10x102.pdf b/modules/vba_documents/spec/fixtures/10x102.pdf index 68a51b26f54..43cfda1b59f 100644 Binary files a/modules/vba_documents/spec/fixtures/10x102.pdf and b/modules/vba_documents/spec/fixtures/10x102.pdf differ diff --git a/spec/lib/pdf_info/metadata_spec.rb b/spec/lib/pdf_info/metadata_spec.rb index f9537c66763..afc6e12a272 100644 --- a/spec/lib/pdf_info/metadata_spec.rb +++ b/spec/lib/pdf_info/metadata_spec.rb @@ -20,7 +20,7 @@ JavaScript: no Pages: 4 Encrypted: no - Page size: 612 x 792 pts (letter) + Page 1 size: 612 x 792 pts (letter) Page rot: 0 File size: 1099807 bytes Optimized: no @@ -51,7 +51,7 @@ describe '::read' do context 'when passed a string' do it 'shells out with the string as the file path' do - expect(Open3).to receive(:popen2e).with('pdfinfo', '/tmp/file.pdf').and_yield('', result, good_exit) + expect(Open3).to receive(:popen2e).with('pdfinfo', '-l', '-1', '/tmp/file.pdf').and_yield('', result, good_exit) described_class.read('/tmp/file.pdf') end end @@ -60,14 +60,14 @@ it 'shells out with the file object path' do file = double(File) allow(file).to receive(:path).and_return('/tmp/file.pdf') - expect(Open3).to receive(:popen2e).with('pdfinfo', '/tmp/file.pdf').and_yield('', result, good_exit) + expect(Open3).to receive(:popen2e).with('pdfinfo', '-l', '-1', '/tmp/file.pdf').and_yield('', result, good_exit) described_class.read(file) end end context 'when the command errors' do it 'raises a PdfInfo::MetadataReadError' do - expect(Open3).to receive(:popen2e).with('pdfinfo', '/tmp/file.pdf').and_yield('', result, bad_exit) + expect(Open3).to receive(:popen2e).with('pdfinfo', '-l', '-1', '/tmp/file.pdf').and_yield('', result, bad_exit) expect { described_class.read('/tmp/file.pdf') }.to raise_error(PdfInfo::MetadataReadError, /pdfinfo exited/) end end