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

Initialize the



36
37
38
39
40
41
42
# File 'lib/acrobat/app.rb', line 36

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

Instance Attribute Details

#ole_objObject (readonly)

WIN32_OLE

ole_obj



29
30
31
# File 'lib/acrobat/app.rb', line 29

def ole_obj
  @ole_obj
end

#pdocObject (readonly)

the wrapped [PDoc] PDoc object



32
33
34
# File 'lib/acrobat/app.rb', line 32

def pdoc
  @pdoc
end

Class Method Details

.fill_form(form, output_name:, update_hash:) ⇒ Object

Fills the form with updates in a hash

Examples:

Acrobat::App.fill_form(myform.pdf, output_name: 'filled.pdf
                             , update_hash: { name: 'dom', filled_date: 1/20/2013

Parameters:

  • doc (String)

    the String path of a fillable pdf file

  • output_name (String)

    the name of the saved filled pdf file

  • update_hash (Hash)

    the hash with updates



83
84
85
86
87
88
89
# File 'lib/acrobat/app.rb', line 83

def self.fill_form(form,output_name: , update_hash: )
  self.run do |app|
    doc = app.open(form)
    doc.fill_form(update_hash)
    doc.save_as(output_name)
  end
end

.replace_pages(src, replacement, output_name:, **opts) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/acrobat/app.rb', line 67

def self.replace_pages(src, replacement, output_name: , **opts)
  self.run do |app|
    app.open(src) do |doc|
      doc.replace_pages(replacement, opts)
      doc.save_as(output_name)
    end
  end
end

.runObject

Runs the adobe app and quits at the end

Examples:

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



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/acrobat/app.rb', line 55

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



151
152
153
# File 'lib/acrobat/app.rb', line 151

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



106
107
108
# File 'lib/acrobat/app.rb', line 106

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

#formObject



175
176
177
# File 'lib/acrobat/app.rb', line 175

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

#hideObject

hide the Adobe Acrobat application



99
100
101
# File 'lib/acrobat/app.rb', line 99

def hide
  ole_obj.Hide
end

#merge_pdfs(*pdfs) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/acrobat/app.rb', line 110

def merge_pdfs(*pdfs)
  pdf_array = Array(pdfs)
  raise 'Not enough pdfs to merge' if pdfs.size < 2
  first, *rest = pdf_array
  doc = open(first)
  rest.each do |path|
    doc.merge(path)
  end
  doc
end

#merge_pdfs_in_dir(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



130
131
132
133
134
135
136
# File 'lib/acrobat/app.rb', line 130

def merge_pdfs_in_dir(dir, name: nil , output_dir: nil)
  name = lname || "merged.pdf"
  dir = output_dir || dir
  pdfs = Pathname.glob( dir + '*.pdf')
  doc = merge_pdfs(pdfs)
  doc
end

#open(file) ⇒ PDoc

open the file.

Parameters:

  • file (String #to_path)

Returns:

  • (PDoc)

    the open file as a Pdoc instance

Raises:

  • (FileNotFound)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/acrobat/app.rb', line 158

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
    doc = nil
  end
end

#quitObject

quit the Adobe App.

closes the open adobe documents and quits the program

Returns:

  • nil



141
142
143
144
145
146
147
148
149
# File 'lib/acrobat/app.rb', line 141

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

#showObject

show the Adobe Acrobat application



94
95
96
# File 'lib/acrobat/app.rb', line 94

def show
  ole_obj.Show
end