Sha256: 1308254ec714ade8e8388e703029a57928183209974468a15761f520c8fbfff5

Contents?: true

Size: 1.81 KB

Versions: 7

Compression:

Stored size: 1.81 KB

Contents

require "fileutils"

require "libis/tools/extend/string"
require "libis/tools/logger"
require "libis/tools/command"

require "libis/format/config"

module Libis
  module Format
    module Tool
      class PdfaValidator
        include ::Libis::Tools::Logger

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

        def run(source)
          src_file = File.absolute_path(source)

          timeout = Libis::Format::Config[:timeouts][:pdfa_validator]
          if (pdfa = Libis::Format::Config[:pdfa_cmd])
            # 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,
              timeout: timeout,
              kill_after: timeout * 2
            )

            raise "#{self.class} took too long (> #{timeout} seconds) to complete" if result[:timeout]
            raise "#{self.class} errors: #{result[:err].join("\n")}" unless result[:status] == 0 && result[:err].empty?

            Dir.chdir(previous_wd)

            out, err = result[:out].partition { |line| line =~ /^VLD-\[PASS\]/ }
            result[:out] = out
            result[:err] += err

            result
          else
            jar = Libis::Format::Config[:preflight_jar]
            result = Libis::Tools::Command.run(
              Libis::Format::Config[:java_cmd],
              "-jar", jar,
              src_file,
              timeout: timeout,
              kill_after: timeout * 2
            )

            raise "#{self.class} took too long (> #{timeout} seconds) to complete" if result[:timeout]

            result
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
libis-format-1.2.7 lib/libis/format/tool/pdfa_validator.rb
libis-format-1.2.6 lib/libis/format/tool/pdfa_validator.rb
libis-format-1.2.5 lib/libis/format/tool/pdfa_validator.rb
libis-format-1.2.3 lib/libis/format/tool/pdfa_validator.rb
libis-format-1.2.2 lib/libis/format/tool/pdfa_validator.rb
libis-format-1.2.1 lib/libis/format/tool/pdfa_validator.rb
libis-format-1.2.0 lib/libis/format/tool/pdfa_validator.rb