| 1 |
#!/usr/bin/env ruby |
| 2 |
|
| 3 |
require 'tmpdir' |
| 4 |
require 'fileutils' |
| 5 |
|
| 6 |
def parse_options(args) |
| 7 |
options = { resolution: 150, threshold: 100, fuzz: 5 } |
| 8 |
|
| 9 |
args.each do |arg| |
| 10 |
case arg |
| 11 |
when /^--res=(\d+)$/ |
| 12 |
options[:resolution] = $1.to_i |
| 13 |
when /^--threshold=(\d+)$/ |
| 14 |
options[:threshold] = $1.to_i |
| 15 |
when /^--fuzz=(\d+)$/ |
| 16 |
options[:fuzz] = $1.to_i |
| 17 |
end |
| 18 |
end |
| 19 |
|
| 20 |
options |
| 21 |
end |
| 22 |
|
| 23 |
def usage |
| 24 |
puts "Usage: #{$0} <pdf1> <pdf2> [options]" |
| 25 |
puts "Compares two PDF files by converting to PNG and analyzing differences" |
| 26 |
puts "\nOptions:" |
| 27 |
puts " --res=N DPI value for conversion (default: 150)" |
| 28 |
puts " --threshold=N Difference threshold in pixels (default: 100)" |
| 29 |
puts " --fuzz=N Fuzz factor percentage (default: 5)" |
| 30 |
exit 1 |
| 31 |
end |
| 32 |
|
| 33 |
def convert_pdf_to_png(pdf_path, output_prefix, resolution=150) |
| 34 |
system("magick -density #{resolution} \"#{pdf_path}\" \"#{output_prefix}-%d.png\"") |
| 35 |
unless $?.success? |
| 36 |
puts "Error converting #{pdf_path} to PNG" |
| 37 |
exit 1 |
| 38 |
end |
| 39 |
# Return the number of pages (count PNG files created) |
| 40 |
Dir["#{output_prefix}-*.png"].length |
| 41 |
end |
| 42 |
|
| 43 |
def create_diff_visualization(image1, image2, diff_output_base) |
| 44 |
system("magick '(' \"#{image1}\" -flatten -grayscale Rec709Luminance ')' " \ |
| 45 |
"'(' \"#{image2}\" -flatten -grayscale Rec709Luminance ')' " \ |
| 46 |
"'(' -clone 0-1 -compose darken -composite ')' " \ |
| 47 |
"-channel RGB -combine \"#{diff_output_base}\"") |
| 48 |
end |
| 49 |
|
| 50 |
def compare_images(image1, image2, diff_output_base, fuzz=5) |
| 51 |
# First create both types of difference visualizations |
| 52 |
base_name = File.join(File.dirname(diff_output_base), |
| 53 |
File.basename(diff_output_base)) |
| 54 |
|
| 55 |
compare_output = "#{base_name}-compare.png" |
| 56 |
visual_output = "#{base_name}-visual.png" |
| 57 |
|
| 58 |
# Generate standard comparison diff |
| 59 |
result = `magick compare -fuzz #{fuzz}% -metric AE "#{image1}" "#{image2}" "#{compare_output}" 2>&1` |
| 60 |
|
| 61 |
# Generate alternative visualization |
| 62 |
create_diff_visualization(image1, image2, visual_output) |
| 63 |
|
| 64 |
difference = result.to_i |
| 65 |
return difference |
| 66 |
rescue |
| 67 |
return -1 # Return -1 if comparison fails (e.g., different image sizes) |
| 68 |
end |
| 69 |
|
| 70 |
def compare_all_pages(dir1, dir2, diff_dir, threshold=100, fuzz=5) |
| 71 |
differences = {} |
| 72 |
total_differences = 0 |
| 73 |
|
| 74 |
# Get all pages from first PDF |
| 75 |
Dir["#{dir1}/*.png"].sort.each_with_index do |page1, idx| |
| 76 |
page_num = idx + 1 |
| 77 |
page2 = File.join(dir2, File.basename(page1)) |
| 78 |
|
| 79 |
if File.exist?(page2) |
| 80 |
diff_output_base = File.join(diff_dir, "diff-page-#{page_num}-") |
| 81 |
diff = compare_images(page1, page2, diff_output_base) |
| 82 |
|
| 83 |
if diff == -1 |
| 84 |
puts "Page #{page_num}: Error comparing pages (possibly different dimensions)" |
| 85 |
differences[page_num] = "error" |
| 86 |
elsif diff <= threshold |
| 87 |
if diff == 0 |
| 88 |
puts "Page #{page_num}: Identical" |
| 89 |
else |
| 90 |
puts "Page #{page_num}: Minor differences (#{diff} pixels) - below threshold" |
| 91 |
end |
| 92 |
Dir["#{diff_output_base}*"].each { |file| File.delete(file) if File.exist?(file) } |
| 93 |
differences[page_num] = 0 |
| 94 |
else |
| 95 |
base_name = File.join(File.dirname(diff_output_base), |
| 96 |
File.basename(diff_output_base)) |
| 97 |
puts "Page #{page_num}: Significant differences (#{diff} pixels)" |
| 98 |
puts "Difference images saved as:" |
| 99 |
puts " - Compare method: #{base_name}-compare.png" |
| 100 |
puts " - Visual method: #{base_name}-visual.png" |
| 101 |
differences[page_num] = diff |
| 102 |
total_differences += diff |
| 103 |
end |
| 104 |
else |
| 105 |
puts "Page #{page_num}: Missing in second PDF" |
| 106 |
differences[page_num] = "missing" |
| 107 |
end |
| 108 |
end |
| 109 |
|
| 110 |
# Check for extra pages in second PDF |
| 111 |
Dir["#{dir2}/*.png"].each do |page2| |
| 112 |
base_name = File.basename(page2) |
| 113 |
unless File.exist?(File.join(dir1, base_name)) |
| 114 |
puts "Extra page in second PDF: #{base_name}" |
| 115 |
differences[base_name] = "extra" |
| 116 |
end |
| 117 |
end |
| 118 |
|
| 119 |
[differences, total_differences] |
| 120 |
end |
| 121 |
|
| 122 |
# Main execution |
| 123 |
usage if ARGV.length < 2 |
| 124 |
|
| 125 |
pdf1_path = ARGV.shift |
| 126 |
pdf2_path = ARGV.shift |
| 127 |
options = parse_options(ARGV) |
| 128 |
|
| 129 |
unless File.exist?(pdf1_path) && File.exist?(pdf2_path) |
| 130 |
puts "Error: One or both PDF files do not exist" |
| 131 |
exit 1 |
| 132 |
end |
| 133 |
|
| 134 |
Dir.mktmpdir do |tmp_dir| |
| 135 |
# Create subdirectories for each PDF's pages |
| 136 |
pdf1_dir = File.join(tmp_dir, "pdf1") |
| 137 |
pdf2_dir = File.join(tmp_dir, "pdf2") |
| 138 |
diff_dir = "diff_output" |
| 139 |
|
| 140 |
FileUtils.rm_rf(diff_dir) |
| 141 |
FileUtils.mkdir_p([pdf1_dir, pdf2_dir, diff_dir]) |
| 142 |
|
| 143 |
puts "Converting PDFs to PNGs (#{options[:resolution]} DPI)..." |
| 144 |
pages1 = convert_pdf_to_png(pdf1_path, File.join(pdf1_dir, "page"), options[:resolution]) |
| 145 |
pages2 = convert_pdf_to_png(pdf2_path, File.join(pdf2_dir, "page"), options[:resolution]) |
| 146 |
|
| 147 |
puts "\nPDF 1 has #{pages1} pages" |
| 148 |
puts "PDF 2 has #{pages2} pages" |
| 149 |
|
| 150 |
puts "\nComparing pages..." |
| 151 |
differences, total_diff = compare_all_pages(pdf1_dir, pdf2_dir, diff_dir, |
| 152 |
options[:threshold], options[:fuzz]) |
| 153 |
|
| 154 |
puts "\nSummary:" |
| 155 |
if differences.all? { |_, v| v == 0 } |
| 156 |
puts "The PDFs are identical" |
| 157 |
FileUtils.rm_rf(diff_dir) |
| 158 |
else |
| 159 |
puts "The PDFs are different" |
| 160 |
puts "Total pixels different across all pages: #{total_diff}" |
| 161 |
puts "Difference images saved in: #{diff_dir}/" |
| 162 |
end |
| 163 |
end |