Peter Miehle
Peter Miehle

Reputation: 6080

test if a PDF file is finished in Ruby (on Solaris/Unix)?

i have a server, that generates or copies PDF-Files to a specific folder.

i wrote a ruby script (my first ever), that regularily checks for own PDF-files and displayes them with acrobat. So simple so nice.

But now I have the Problem: how to detect the PDF is complete?

The generated PDF ends with %%EOF\n but the copied ones are generated with some Apple-Magic (Acrobat Writer I think), that has an %%EOF near the beginning of the File, lots of binary Zeros and another %%EOF near the end with a carriage return (or line feed) and a binary zero at the end.

while true
  dir = readpfad
  Dir.foreach(dir) do |f|
    datei = File.join(dir, f)
    if File.file?(datei)
      if File.stat(datei).owned?
        if datei[-9..-1].upcase == "__PDF.PDF"
          if File.stat(datei).size > 5
            test = File.new(datei)
            dummy = test.readlines
            if dummy[-1][0..4] == "%%EOF"
              #move the file, so it will not be shown again
              cmd = "mv " + datei + " " + movepfad
              system(cmd)
              acro = ACROREAD + " " + File.join(movepfad, f) + "&"
              system(acro)
            else
              puts ">>>" + dummy[-1] + "<<<"
            end
          end
        end
      end
    end
  end
  sleep 1
end

Any help or idea? Thanks Peter

Upvotes: 0

Views: 188

Answers (1)

plinth
plinth

Reputation: 49209

All the %%EOF token means is that there should be one within the last 1024 bytes of the physical end of file. The structure of PDF is such that a PDF document may have 1 or more %%EOF tokens within it (the details are in the spec).

As such, "contains %%EOF" is not equivalent to "completely copied". Really, the correct answer is that the server should signal when it's done and your code should be a client of that signal. In general, polling -- especially IO bound polling is the wrong answer to this problem.

Upvotes: 1

Related Questions