Class: Acrobat::App

Inherits:
Object
  • Object
show all
Defined in:
lib/acrobat/app.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



29
30
31
32
33
34
35
# File 'lib/acrobat/app.rb', line 29

def initialize()
  @ole_obj = WIN32OLE.new('AcroExch.App')
  load_constants(@ole_obj)
  @ole_obj
  @docs = []
  self
end

Instance Attribute Details

#ole_objObject (readonly)

Returns the value of attribute ole_obj.



27
28
29
# File 'lib/acrobat/app.rb', line 27

def ole_obj
  @ole_obj
end

#pdocObject (readonly)

Returns the value of attribute pdoc.



27
28
29
# File 'lib/acrobat/app.rb', line 27

def pdoc
  @pdoc
end

Class Method Details

.runObject

Runs the adobe app and quits at the end Acrobat::App.run do |app|

doc = app.open('doc.pdf')
doc.fill_form( city: 'City', state: 'ST')
doc.save_as('filled.pdf')

end

Returns:

  • nil



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/acrobat/app.rb', line 47

def self.run
  begin
    the_app = new
    yield the_app
  ensure
    the_app.quit unless the_app.nil?
    the_app = nil
    GC.start
    nil
  end
end

Instance Method Details

#docsObject



113
114
115
# File 'lib/acrobat/app.rb', line 113

def docs
  @docs
end

#find_pdfs_in_dir(dir) ⇒ Array

Finds the pdfs in a dir

Parameters:

  • dir (String)

    the directory to find pdfs in

Returns:

  • (Array)

    of pdf files



72
73
74
# File 'lib/acrobat/app.rb', line 72

def find_pdfs_in_dir(dir)
  Pathname.glob( dir + '*.pdf')
end

#formObject



137
138
139
# File 'lib/acrobat/app.rb', line 137

def form
  Form.new(self,WIN32OLE.new("AFormAut.App"))
end

#hideObject

hide the Adobe Acrobat application



65
66
67
# File 'lib/acrobat/app.rb', line 65

def hide
  ole_obj.Hide
end

#merge_pdfs(dir, name: nil, output_dir: nil) ⇒ Object

merges the pdfs in directory return [Boolean] if the merge was successful or not

Parameters:

  • dir (String)

    the path of the directory

  • name (String, Nil) (defaults to: nil)

    the name of the returned pdf file if the name is nil, the name is “merged.pdf”

  • output_dir (String, Nil) (defaults to: nil)

    the name of the output dir if the output_dir is nil, the output dir is the dir param



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/acrobat/app.rb', line 83

def merge_pdfs(dir, name: nil , output_dir: nil)
  name = lname || "merged.pdf"
  dir = output_dir || dir
  pdfs = Pathname.glob( dir + '*.pdf')
  raise 'Not enough pdfs to merge' if pdfs.size < 2
  first, *rest = pdfs
  open(first) do |pdf|
    rest.each do |path|
      open(path) do |pdfnext|
        pdf.merge_doc(pdfnext)
      end
    end

    pdf.save_as(name: name, dir: dir)
  end
end

#open(file) ⇒ PDoc

open the file.

Parameters:

  • file (String #to_path)

Returns:

  • (PDoc)

    the open file as a Pdoc instance

Raises:

  • (FileNotFound)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/acrobat/app.rb', line 120

def open(file)
  filepath = Pathname(file).expand_path
  raise FileNotFound unless filepath.file?
  pdoc = WIN32OLE.new('AcroExch.PDDoc')
  is_opened = pdoc.open FileSystemObject.windows_path(filepath)
  doc = PDoc.new(self, pdoc, filepath) if is_opened
  docs << doc if is_opened
  return doc unless block_given?
  begin
    yield doc
  ensure
    doc.close unless doc.closed?
    doc = nil
  end
end

#quitObject

quit the Adobe App.

closes the open adobe documents and quits the program

Returns:

  • nil



103
104
105
106
107
108
109
110
111
# File 'lib/acrobat/app.rb', line 103

def quit
  begin
    docs.each{ |d| d.close}
    ole_obj.Exit
  rescue
    return nil
  end
  nil
end

#showObject

show the Adobe Acrobat application



60
61
62
# File 'lib/acrobat/app.rb', line 60

def show
  ole_obj.Show
end