Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check all the PDF's pages for oversized page dimensions #19987

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's worth adding a comment here to explain what this option with the -1 magic number passed in does.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also looks like we are the only team using this library (vba_documents and appeals_api modules), so we won't be affecting anyone else's implementations.

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
2 changes: 1 addition & 1 deletion modules/vba_documents/lib/vba_documents/pdf_inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def pdf_metadata(pdf)
dimensions: {
height: dimensions[:height].round(2),
width: dimensions[:width].round(2),
Copy link
Contributor

@kristen-brown kristen-brown Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how useful these height and width values in the metadata are anymore, since they're only for the first page. 🤔

Not suggesting any specific changes, just something I'm thinking about.

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
Expand Down
Binary file modified modules/vba_documents/spec/fixtures/10x102.pdf
Binary file not shown.
8 changes: 4 additions & 4 deletions spec/lib/pdf_info/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading