Class: Libis::Format::Converter::PdfProtecter

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

Overview

noinspection DuplicatedCode

Constant Summary collapse

OPTIONS_TABLE =
{
  edit_password: 'edit-password',
  open_password: 'open-password',
  fillin: 'fill-in',
}

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

#initializePdfProtecter

Returns a new instance of PdfProtecter.



29
30
31
32
# File 'lib/libis/format/converter/pdf_protecter.rb', line 29

def initialize
  super
  @options[:edit_password] = SecureRandom.urlsafe_base64(31)
end

Class Method Details

.input_typesObject



16
17
18
# File 'lib/libis/format/converter/pdf_protecter.rb', line 16

def self.input_types
  [:PDF]
end

.output_types(format = nil) ⇒ Object



20
21
22
23
# File 'lib/libis/format/converter/pdf_protecter.rb', line 20

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

Instance Method Details

#assist(v) ⇒ Object



42
43
44
# File 'lib/libis/format/converter/pdf_protecter.rb', line 42

def assist(v)
  @flags[:assist] = !!v
end

#comments(v) ⇒ Object



54
55
56
# File 'lib/libis/format/converter/pdf_protecter.rb', line 54

def comments(v)
  @flags[:comments] = !!v
end

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/libis/format/converter/pdf_protecter.rb', line 70

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

  result = nil

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

  if format == :PDFA and source
    result = pdf_to_pdfa(source, target)
  end

  result

end

#convert_pdf(source, target) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/libis/format/converter/pdf_protecter.rb', line 95

def convert_pdf(source, target)

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

end

#copy(v) ⇒ Object



46
47
48
# File 'lib/libis/format/converter/pdf_protecter.rb', line 46

def copy(v)
  @flags[:copy] = !!v
end

#edit(v) ⇒ Object



66
67
68
# File 'lib/libis/format/converter/pdf_protecter.rb', line 66

def edit(v)
  @flags[:edit] = !!v
end

#edit_password(pwd) ⇒ Object



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

def edit_password(pwd)
  @options[:edit_password] = pwd
end

#fillin(v) ⇒ Object



58
59
60
# File 'lib/libis/format/converter/pdf_protecter.rb', line 58

def fillin(v)
  @flags[:fillin] = !!v
end

#manage(v) ⇒ Object



62
63
64
# File 'lib/libis/format/converter/pdf_protecter.rb', line 62

def manage(v)
  @flags[:manage] = !!v
end

#open_password(pwd) ⇒ Object



38
39
40
# File 'lib/libis/format/converter/pdf_protecter.rb', line 38

def open_password(pwd)
  @options[:open_password] = pwd
end

#pdf_protect(_) ⇒ Object



25
26
27
# File 'lib/libis/format/converter/pdf_protecter.rb', line 25

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

#pdf_to_pdfa(source, target) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/libis/format/converter/pdf_protecter.rb', line 128

def pdf_to_pdfa(source, target)

  using_temp(target) do |tmpname|
    result = Libis::Format::Tool::PdfToPdfa.run source, tmpname
    if result[:status] != 0
      error("Pdf/A conversion encountered errors:\n%s", result[:err].join("\n"))
      next nil
    else
      warn("Pdf/A conversion warnings:\n%s", result[:err].join("\n")) unless result[:err].empty?
    end
    tmpname
  end

end


50
51
52
# File 'lib/libis/format/converter/pdf_protecter.rb', line 50

def print(v)
  @flags[:print] = !!v
end