class Libis::Format::Converter::PdfProtecter

noinspection DuplicatedCode

Constants

OPTIONS_TABLE

Public Class Methods

input_types() click to toggle source
# File lib/libis/format/converter/pdf_protecter.rb, line 16
def self.input_types
  [:PDF]
end
new() click to toggle source
Calls superclass method Libis::Format::Converter::Base::new
# File lib/libis/format/converter/pdf_protecter.rb, line 29
def initialize
  super
  @options[:edit_password] = SecureRandom.urlsafe_base64(31)
end
output_types(format = nil) click to toggle source
# 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

Public Instance Methods

assist(v) click to toggle source
# File lib/libis/format/converter/pdf_protecter.rb, line 42
def assist(v)
  @flags[:assist] = !!v
end
comments(v) click to toggle source
# File lib/libis/format/converter/pdf_protecter.rb, line 54
def comments(v)
  @flags[:comments] = !!v
end
convert(source, target, format, opts = {}) click to toggle source
Calls superclass method Libis::Format::Converter::Base#convert
# 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) click to toggle source
# 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) click to toggle source
# File lib/libis/format/converter/pdf_protecter.rb, line 46
def copy(v)
  @flags[:copy] = !!v
end
edit(v) click to toggle source
# File lib/libis/format/converter/pdf_protecter.rb, line 66
def edit(v)
  @flags[:edit] = !!v
end
edit_password(pwd) click to toggle source
# File lib/libis/format/converter/pdf_protecter.rb, line 34
def edit_password(pwd)
  @options[:edit_password] = pwd
end
fillin(v) click to toggle source
# File lib/libis/format/converter/pdf_protecter.rb, line 58
def fillin(v)
  @flags[:fillin] = !!v
end
manage(v) click to toggle source
# File lib/libis/format/converter/pdf_protecter.rb, line 62
def manage(v)
  @flags[:manage] = !!v
end
open_password(pwd) click to toggle source
# File lib/libis/format/converter/pdf_protecter.rb, line 38
def open_password(pwd)
  @options[:open_password] = pwd
end
pdf_protect(_) click to toggle source
# File lib/libis/format/converter/pdf_protecter.rb, line 25
def pdf_protect(_)
  #force usage of this converter
end
pdf_to_pdfa(source, target) click to toggle source
# 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
print(v) click to toggle source