Sha256: be3c77bf1926c7aa8f090fc606732788b02a9debd00f1aa1e0fcdde11c58ad4b

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 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

    class PdfaValidator
      include ::Libis::Tools::Logger

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

      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(Libis::Format::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
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
libis-format-0.9.41 lib/libis/format/pdfa_validator.rb
libis-format-0.9.40 lib/libis/format/pdfa_validator.rb
libis-format-0.9.39 lib/libis/format/pdfa_validator.rb