Search code examples
rubyfilepdfcomparison

How to compare two PDF files in Ruby


I am trying to compare two PDF files using UTF-8, but I get the error "Invalid encoding" when I execute the code below:

 encoding = 'utf-8'
 base_path = set_up
 tear_down do
   f1 = File.read("#{TMP_DIR}/#{current_file_name}", encoding: encoding)
   f2 = File.read("#{base_path}/#{expected_file_name}", encoding: encoding)
   expect(f1).to eql f2
 end

I tried to use:

f1.force_encoding("UTF-8")
f2.force_encoding("UTF-8")

I tried this too:

f1.force_encoding("BINARY")

but, I get another error:

Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8

Solution

  • Instead of comparing the file as strings I would just compare the files' MD5 hash:

    require 'digest'
    
    tear_down do
      md5_1 = Digest::MD5.file("#{TMP_DIR}/#{current_file_name}")
      md5_2 = Digest::MD5.file("#{base_path}/#{expected_file_name}")
    
      expect(md5_1).to eql md5_2
    end