Sha256: 0c7236dd3e518c86f393c739db7da87f5d15e2d1925dc6f101232195cbc09c98

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

# encoding: utf-8

require_relative 'base'

require 'libis/format/tool/pdf_select'

module Libis
  module Format
    module Converter

      # noinspection DuplicatedCode
      class PdfSelecter < Libis::Format::Converter::Base

        def self.input_types
          [:PDF]
        end

        def self.output_types(format = nil)
          return [] unless input_types.include?(format) if format
          [:PDF]
        end

        def pdf_select(_)
          #force usage of this converter
        end

        def initialize
          super
          @options[:range] = []
        end

        # Select a partial list of pages
        # @param [String] selection as described in com.itextpdf.text.pdf.SequenceList: [!][o][odd][e][even]start-end
        def range(selection)
          @options[:range] << selection unless selection.blank?
        end

        def convert(source, target, format, opts = {})
          super

          result = nil

          unless @options.empty?
            result = convert_pdf(source, target)
            return nil unless result
          end

          result

        end

        def convert_pdf(source, target)

          using_temp(target) do |tmpname|
            result = Libis::Format::Tool::PdfSelect.run(
              source, tmpname,
              @options.map { |k, v|
                if v.nil?
                  nil
                else
                  ["--#{k}", v]
                end }.compact.flatten
            )
            unless result[:err].empty?
              error("Pdf selection encountered errors:\n%s", result[:err].join(join("\n")))
              next nil
            end
            tmpname
          end

        end

      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
libis-format-2.0.1 lib/libis/format/converter/pdf_selecter.rb