Class: PDF::Margins::Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf/margins/checker.rb

Constant Summary collapse

MM_TO_PTS =
2.83464567
DEFAULT_RESOLUTION =
72
SCALE_MULTIPLIER =

If we find that we need higher resolution for better precision then we can adjust the SCALE_MULTIPLIER at the cost of speed.

1
RESOLUTION =
DEFAULT_RESOLUTION * SCALE_MULTIPLIER

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, top_margin, right_margin, bottom_margin, left_margin, spreads = false) ⇒ Checker

Dimensions are in mm, to be converted to PDF points later. Pass spreads as true to check left and right margins of spreads, not pages.



22
23
24
25
26
27
28
29
# File 'lib/pdf/margins/checker.rb', line 22

def initialize(file_path, top_margin, right_margin, bottom_margin, left_margin, spreads=false)
  @file_path     = file_path
  @top_margin    = top_margin
  @right_margin  = right_margin
  @bottom_margin = bottom_margin
  @left_margin   = left_margin
  @spreads       = spreads
end

Instance Attribute Details

#bottom_marginObject (readonly)

Returns the value of attribute bottom_margin.



18
19
20
# File 'lib/pdf/margins/checker.rb', line 18

def bottom_margin
  @bottom_margin
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



18
19
20
# File 'lib/pdf/margins/checker.rb', line 18

def file_path
  @file_path
end

#left_marginObject (readonly)

Returns the value of attribute left_margin.



18
19
20
# File 'lib/pdf/margins/checker.rb', line 18

def left_margin
  @left_margin
end

#right_marginObject (readonly)

Returns the value of attribute right_margin.



18
19
20
# File 'lib/pdf/margins/checker.rb', line 18

def right_margin
  @right_margin
end

#spreadsObject (readonly)

Returns the value of attribute spreads.



18
19
20
# File 'lib/pdf/margins/checker.rb', line 18

def spreads
  @spreads
end

#top_marginObject (readonly)

Returns the value of attribute top_margin.



18
19
20
# File 'lib/pdf/margins/checker.rb', line 18

def top_margin
  @top_margin
end

Instance Method Details

#issuesObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pdf/margins/checker.rb', line 31

def issues
  temp_dir_path = Dir.mktmpdir("pdf_margins")

  begin
    # This produces greyscale PNGs - we throw the colour away because we
    # don't need it to check for margins.
    system("mudraw -g -b 0 -r #{RESOLUTION} -o #{temp_dir_path}/%d.png #{file_path}") || raise(PDF::Margins::MuDrawCommandError)

    issues = []

    files = Dir.glob("#{temp_dir_path}/*.png")
    # ensure the files are sorted naturally
    files = files.sort_by{ |f| f.split('/').last.to_i }

    files.each_with_index do |png_path, index|
      image = ChunkyPNG::Image.from_file(png_path)
      page_number = index + 1

      if dirty_top_margin?(image, top_margin)
        issues << Issue.new(page_number, :top)
      end

      if dirty_bottom_margin?(image, bottom_margin)
        issues << Issue.new(page_number, :bottom)
      end

      if (!spreads || page_number % 2 == 0) && dirty_left_margin?(image, left_margin)
        issues << Issue.new(page_number, :left)
      end
      
      if (!spreads || page_number % 2 != 0) && dirty_right_margin?(image, right_margin)
        issues << Issue.new(page_number, :right)
      end
    end

  ensure
    FileUtils.remove_entry(temp_dir_path)
  end

  return issues
end