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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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|
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.
Loading