Class: PDFDetach

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfdetach/main.rb,
lib/pdfdetach/version.rb,
lib/pdfdetach/configuration.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
'0.20.3'
LIB_TARGET =
'20.04'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ PDFDetach

Returns a new instance of PDFDetach.

Parameters:

  • filepath (String)


9
10
11
12
13
# File 'lib/pdfdetach/main.rb', line 9

def initialize(filepath)
  @src = filepath
  @base_path = Pathname.new("#{__dir__}/../../bin/#{LIB_TARGET}").cleanpath
  @binary_path = PDFDetach.configuration.binary_path
end

Class Method Details

.configurationObject



24
25
26
# File 'lib/pdfdetach/configuration.rb', line 24

def self.configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

On a initializer or somewhere else in your code, if you want to use a different version or binary instead of the binary provided in this gem, you can assign a path for a binary like this:

PDFDetach.configure do |config|
  config.binary_path = 'path/to/pdfdetach'
end

Yields:



20
21
22
# File 'lib/pdfdetach/configuration.rb', line 20

def self.configure
  yield configuration
end

Instance Method Details

#get_opts(options) ⇒ Object

Parameters:

  • options (Hash)


16
17
18
19
20
21
22
23
# File 'lib/pdfdetach/main.rb', line 16

def get_opts(options)
  args = String.new
  args << " -o #{options[:output]} " if options[:output]
  args << " -enc #{options[:encoding]} " if options[:encoding]
  args << " -opw #{options[:owner_password]} " if options[:owner_password]
  args << " -upw #{options[:user_password]} " if options[:user_password]
  args
end

#listArray

List all files attached

Returns:

  • (Array)


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pdfdetach/main.rb', line 29

def list
  result = run("-list #{@src}")

  list ||= if result[:ok]
             result[:out].split("\n").map do |line|
               next line.match(/^(\d+): (.+?)$/) { |m| m[2] } if line =~ /^(\d+):/
             end
           else
             []
           end
  list.compact
end

#saveall(options = {}) ⇒ Object

Save all attached files in the pdf

Parameters:

  • options (Hash) (defaults to: {})


46
47
48
# File 'lib/pdfdetach/main.rb', line 46

def saveall(options = {})
  run("-saveall #{get_opts(options)} #{@src}")[:ok]
end