Class: Libis::Format::PdfaValidator

Inherits:
Object
  • Object
show all
Includes:
Tools::Logger
Defined in:
lib/libis/format/pdfa_validator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run(source) ⇒ Object



15
16
17
# File 'lib/libis/format/pdfa_validator.rb', line 15

def self.run(source)
  self.new.run source
end

Instance Method Details

#run(source) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
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
# File 'lib/libis/format/pdfa_validator.rb', line 19

def run(source)

  src_file = File.absolute_path(source)

  if (pdfa = Libis::Format::Config[:pdfa_path])
    # Keep it clean: tool generates fontconfig/ cache dir in current working dir
    previous_wd = Dir.getwd
    Dir.chdir(Dir.tmpdir)

    result = Libis::Tools::Command.run(
        pdfa,
        '--noxml',
        '--level', 'B',
        '--verb', '0',
        src_file
    )

    Dir.chdir(previous_wd)

    unless result[:out].any? { |line| line =~ /^VLD-\[PASS\]/ }
      warn "Validator failed to validate the PDF file '%s' against PDF/A-1B constraints:\n%s", source,
           result[:out].join("\n")
      return false
    end
  else
    jar = File.join(ROOT_DIR, 'tools', 'pdfbox', 'preflight-app-1.8.10.jar')
    result = Libis::Tools::Command.run(
        Libis::Format::Config[:java_path],
        '-jar', jar,
        src_file
    )
    unless result[:status] == 0
      warn "Validator failed to validate the PDF file '%s' against PDF/A-1B constraints:\n%s", source,
           result[:out].join("\n")
      return false
    end
  end
  true
end