Class: Libis::Format::Converter::PdfSelecter

Inherits:
Base
  • Object
show all
Defined in:
lib/libis/format/converter/pdf_selecter.rb

Overview

noinspection DuplicatedCode

Instance Attribute Summary

Attributes inherited from Base

#flags, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

category, #check_file_exist, inherited, using_temp, #using_temp

Constructor Details

#initializePdfSelecter

Returns a new instance of PdfSelecter.



27
28
29
30
# File 'lib/libis/format/converter/pdf_selecter.rb', line 27

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

Class Method Details

.input_typesObject



14
15
16
# File 'lib/libis/format/converter/pdf_selecter.rb', line 14

def self.input_types
  [:PDF]
end

.output_types(format = nil) ⇒ Object



18
19
20
21
# File 'lib/libis/format/converter/pdf_selecter.rb', line 18

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

Instance Method Details

#convert(source, target, format, opts = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/libis/format/converter/pdf_selecter.rb', line 51

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

  result = nil

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

  result

end

#convert_pdf(source, target) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/libis/format/converter/pdf_selecter.rb', line 65

def convert_pdf(source, target)

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

end

#pdf_select(_) ⇒ Object



23
24
25
# File 'lib/libis/format/converter/pdf_selecter.rb', line 23

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

#range(selection) ⇒ Object

Select a partial list of pages

Parameters:

  • selection (String)

    as described in com.itextpdf.text.pdf.SequenceList: [!][o][e]start-end



34
35
36
# File 'lib/libis/format/converter/pdf_selecter.rb', line 34

def range(selection)
  @options[:ranges] += selection.split(/\s*,\s*/) unless selection.blank?
end

#ranges(selection) ⇒ Object

Select a partial list of pages

Parameters:

  • selection (String|Array<String>)

    as described in com.itextpdf.text.pdf.SequenceList: [!][o][e]start-end



40
41
42
43
44
45
46
47
48
49
# File 'lib/libis/format/converter/pdf_selecter.rb', line 40

def ranges(selection)
  case selection
  when Array
    @options[:ranges] += selection.to_s unless selection.empty?
  when String
    range([selection])
  else
    # nothing
  end
end