tags:

views:

40

answers:

1

hi,

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

+1  A: 

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.

plinth
Thanks for the answer, but in my case it does not help: the "server" is an process, which prints text on a presentation manager device (a postscript printer), sends this to a printer queue which asynchronously calls ghostscript to generate a PDF (yes, it is OS/2)
Peter Miehle
can't you wrap the call to ghostscript with a script that writes the pdf and then does a "touch filename.done"?
klochner