acrobat 0.0.8 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/acrobat CHANGED
@@ -1,3 +1,3 @@
1
- #!/usr/bin/env ruby
2
-
3
- abort "you need to write me"
1
+ #!/usr/bin/env ruby
2
+
3
+ abort "you need to write me"
data/lib/acrobat.rb CHANGED
@@ -1,48 +1,48 @@
1
- require 'win32ole'
2
- require 'acrobat/app'
3
-
4
-
5
- module Acrobat
6
- VERSION = "0.0.8"
7
- end
8
-
9
-
10
- if $0 == __FILE__
11
- require 'pry'
12
-
13
- app = Acrobat::App.run do |app|
14
-
15
- data = Pathname(__dir__).parent + 'data'
16
- antenna_form = data + 'faa.6030.17.antenna.pdf'
17
-
18
-
19
- doc1 = app.open(antenna_form)
20
- doc1.show
21
-
22
- fields = {'city' => 'OGD', 'state' => 'Utah',
23
- 'lid' => 'OGD',
24
- 'fac' => 'RTR',
25
- }
26
- doc1.fill_form(fields)
27
-
28
-
29
- doc1.save_as(name: 'ogd.rtr.pdf', dir: 'tmp')
30
- binding.pry
31
- jso = doc1.jso
32
- jso.show_console
33
- puts "field count: #{jso.field_count}"
34
- puts "field names: \n#{jso.field_names}"
35
- binding.pry
36
-
37
-
38
-
39
- doc2 = app.open(data + 'faa.6030.17.cm300.uhf.tx.pdf')
40
- doc2.show
41
- doc2.fill_form(fields)
42
-
43
- doc1.merge(doc2)
44
- doc1.save_as(name: 'ogd.merged_antenna_tx.pdf', dir: 'tmp')
45
-
46
- end
47
-
48
- end
1
+ require 'win32ole'
2
+ require 'acrobat/app'
3
+
4
+
5
+ module Acrobat
6
+ VERSION = "0.1.0"
7
+ end
8
+
9
+
10
+ if $0 == __FILE__
11
+ require 'pry'
12
+
13
+ app = Acrobat::App.run do |app|
14
+
15
+ data = Pathname(__dir__).parent + 'data'
16
+ antenna_form = data + 'faa.6030.17.antenna.pdf'
17
+
18
+
19
+ doc1 = app.open(antenna_form)
20
+ doc1.show
21
+
22
+ fields = {'city' => 'OGD', 'state' => 'Utah',
23
+ 'lid' => 'OGD',
24
+ 'fac' => 'RTR',
25
+ }
26
+ doc1.fill_form(fields)
27
+
28
+
29
+ doc1.save_as(name: 'ogd.rtr.pdf', dir: 'tmp')
30
+ binding.pry
31
+ jso = doc1.jso
32
+ jso.show_console
33
+ puts "field count: #{jso.field_count}"
34
+ puts "field names: \n#{jso.field_names}"
35
+ binding.pry
36
+
37
+
38
+
39
+ doc2 = app.open(data + 'faa.6030.17.cm300.uhf.tx.pdf')
40
+ doc2.show
41
+ doc2.fill_form(fields)
42
+
43
+ doc1.merge(doc2)
44
+ doc1.save_as(name: 'ogd.merged_antenna_tx.pdf', dir: 'tmp')
45
+
46
+ end
47
+
48
+ end
data/lib/acrobat/app.rb CHANGED
@@ -1,187 +1,187 @@
1
- require 'pry'
2
- require 'win32ole'
3
- require_relative 'pdoc'
4
- require_relative 'jso'
5
-
6
- module FileSystemObject
7
-
8
- @instance = nil
9
- def FileSystemObject.instance
10
- unless @instance
11
- @instance = WIN32OLE.new('Scripting.FileSystemObject')
12
- end
13
- return @instance
14
- end
15
-
16
- def FileSystemObject.windows_path(path)
17
- FileSystemObject.instance.GetAbsolutePathname(path.to_s)
18
- end
19
-
20
- end
21
-
22
- module ACRO; end
23
-
24
- module Acrobat
25
-
26
- class App
27
-
28
- # [WIN32_OLE] ole_obj
29
- attr_reader :ole_obj
30
-
31
- # the wrapped [PDoc] PDoc object
32
- attr_reader :pdoc
33
-
34
- # Initialize the
35
- # @return [App] return an instance of App
36
- def initialize()
37
- @ole_obj = WIN32OLE.new('AcroExch.App')
38
- load_constants(@ole_obj)
39
- @ole_obj
40
- @docs = []
41
- self
42
- end
43
-
44
- # Runs the adobe app and quits at the end
45
- # @yield app [App]
46
- #
47
- # @example
48
- # Acrobat::App.run do |app|
49
- # doc = app.open('doc.pdf')
50
- # doc.fill_form( city: 'City', state: 'ST')
51
- # doc.save_as('filled.pdf')
52
- # end
53
- #
54
- # @return nil
55
- def self.run
56
- begin
57
- the_app = new
58
- yield the_app
59
- ensure
60
- the_app.quit unless the_app.nil?
61
- the_app = nil
62
- GC.start
63
- nil
64
- end
65
- end
66
-
67
- def self.replace_pages(src, replacement, output_name: , **opts)
68
- self.run do |app|
69
- app.open(src) do |doc|
70
- doc.replace_pages(replacement, opts)
71
- doc.save_as(output_name)
72
- end
73
- end
74
- end
75
-
76
- # Fills the form with updates in a hash
77
- # @example
78
- # Acrobat::App.fill_form(myform.pdf, output_name: 'filled.pdf
79
- # , update_hash: { name: 'dom', filled_date: 1/20/2013
80
- # @param doc [String] the String path of a fillable pdf file
81
- # @param output_name [String] the name of the saved filled pdf file
82
- # @param update_hash [Hash] the hash with updates
83
- def self.fill_form(form,output_name: , update_hash: )
84
- self.run do |app|
85
- doc = app.open(form)
86
- doc.fill_form(update_hash)
87
- doc.save_as(output_name)
88
- end
89
- end
90
-
91
-
92
-
93
- # show the Adobe Acrobat application
94
- def show
95
- ole_obj.Show
96
- end
97
-
98
- # hide the Adobe Acrobat application
99
- def hide
100
- ole_obj.Hide
101
- end
102
-
103
- # Finds the pdfs in a dir
104
- # @param dir [String] the directory to find pdfs in
105
- # @return [Array] of pdf files
106
- def find_pdfs_in_dir(dir)
107
- Pathname.glob( dir + '*.pdf')
108
- end
109
-
110
- def merge_pdfs(*pdfs)
111
- pdf_array = Array(pdfs)
112
- raise 'Not enough pdfs to merge' if pdfs.size < 2
113
- first, *rest = pdf_array
114
- doc = open(first)
115
- rest.each do |path|
116
- doc.merge(path)
117
- end
118
- doc
119
- end
120
-
121
-
122
-
123
- # merges the pdfs in directory
124
- # @param dir [String] the path of the directory
125
- # @param name [String,Nil] the name of the returned pdf file
126
- # if the name is nil, the name is "merged.pdf"
127
- # @param output_dir [String,Nil] the name of the output dir
128
- # if the output_dir is nil, the output dir is the dir param
129
- # return [Boolean] if the merge was successful or not
130
- def merge_pdfs_in_dir(dir, name: nil , output_dir: nil)
131
- name = lname || "merged.pdf"
132
- dir = output_dir || dir
133
- pdfs = Pathname.glob( dir + '*.pdf')
134
- doc = merge_pdfs(pdfs)
135
- doc
136
- end
137
-
138
- # quit the Adobe App.
139
- # closes the open adobe documents and quits the program
140
- # @return nil
141
- def quit
142
- begin
143
- docs.each{ |d| d.close}
144
- ole_obj.Exit
145
- rescue
146
- return nil
147
- end
148
- nil
149
- end
150
-
151
- def docs
152
- @docs
153
- end
154
-
155
- # open the file.
156
- # @param file [String #to_path]
157
- # @return [PDoc] the open file as a Pdoc instance
158
- def open(file)
159
- filepath = Pathname(file).expand_path
160
- raise FileNotFound unless filepath.file?
161
- pdoc = WIN32OLE.new('AcroExch.PDDoc')
162
- is_opened = pdoc.open FileSystemObject.windows_path(filepath)
163
- doc = PDoc.new(self, pdoc, filepath) if is_opened
164
- docs << doc if is_opened
165
- return doc unless block_given?
166
- begin
167
- yield doc
168
- ensure
169
- doc.close
170
- doc = nil
171
- end
172
- end
173
-
174
-
175
- def form
176
- Form.new(self,WIN32OLE.new("AFormAut.App"))
177
- end
178
-
179
- private
180
-
181
- def load_constants(ole_obj)
182
- WIN32OLE.const_load(ole_obj, ACRO) unless ACRO.constants.size > 0
183
- end
184
-
185
- end
186
-
187
- end
1
+ require 'pry'
2
+ require 'win32ole'
3
+ require_relative 'pdoc'
4
+ require_relative 'jso'
5
+
6
+ module FileSystemObject
7
+
8
+ @instance = nil
9
+ def FileSystemObject.instance
10
+ unless @instance
11
+ @instance = WIN32OLE.new('Scripting.FileSystemObject')
12
+ end
13
+ return @instance
14
+ end
15
+
16
+ def FileSystemObject.windows_path(path)
17
+ FileSystemObject.instance.GetAbsolutePathname(path.to_s)
18
+ end
19
+
20
+ end
21
+
22
+ module ACRO; end
23
+
24
+ module Acrobat
25
+
26
+ class App
27
+
28
+ # [WIN32_OLE] ole_obj
29
+ attr_reader :ole_obj
30
+
31
+ # the wrapped [PDoc] PDoc object
32
+ attr_reader :pdoc
33
+
34
+ # Initialize the
35
+ # @return [App] return an instance of App
36
+ def initialize()
37
+ @ole_obj = WIN32OLE.new('AcroExch.App')
38
+ load_constants(@ole_obj)
39
+ @ole_obj
40
+ @docs = []
41
+ self
42
+ end
43
+
44
+ # Runs the adobe app and quits at the end
45
+ # @yield app [App]
46
+ #
47
+ # @example
48
+ # Acrobat::App.run do |app|
49
+ # doc = app.open('doc.pdf')
50
+ # doc.fill_form( city: 'City', state: 'ST')
51
+ # doc.save_as('filled.pdf')
52
+ # end
53
+ #
54
+ # @return nil
55
+ def self.run
56
+ begin
57
+ the_app = new
58
+ yield the_app
59
+ ensure
60
+ the_app.quit unless the_app.nil?
61
+ the_app = nil
62
+ GC.start
63
+ nil
64
+ end
65
+ end
66
+
67
+ def self.replace_pages(src, replacement, output_name: , **opts)
68
+ self.run do |app|
69
+ app.open(src) do |doc|
70
+ doc.replace_pages(replacement, **opts)
71
+ doc.save_as(output_name)
72
+ end
73
+ end
74
+ end
75
+
76
+ # Fills the form with updates in a hash
77
+ # @example
78
+ # Acrobat::App.fill_form(myform.pdf, output_name: 'filled.pdf
79
+ # , update_hash: { name: 'dom', filled_date: 1/20/2013
80
+ # @param doc [String] the String path of a fillable pdf file
81
+ # @param output_name [String] the name of the saved filled pdf file
82
+ # @param update_hash [Hash] the hash with updates
83
+ def self.fill_form(form,output_name: , update_hash: )
84
+ self.run do |app|
85
+ doc = app.open(form)
86
+ doc.fill_form(update_hash)
87
+ doc.save_as(output_name)
88
+ end
89
+ end
90
+
91
+
92
+
93
+ # show the Adobe Acrobat application
94
+ def show
95
+ ole_obj.Show
96
+ end
97
+
98
+ # hide the Adobe Acrobat application
99
+ def hide
100
+ ole_obj.Hide
101
+ end
102
+
103
+ # Finds the pdfs in a dir
104
+ # @param dir [String] the directory to find pdfs in
105
+ # @return [Array] of pdf files
106
+ def find_pdfs_in_dir(dir)
107
+ Pathname.glob( dir + '*.pdf')
108
+ end
109
+
110
+ def merge_pdfs(*pdfs)
111
+ pdf_array = Array(pdfs)
112
+ raise 'Not enough pdfs to merge' if pdfs.size < 2
113
+ first, *rest = pdf_array
114
+ doc = open(first)
115
+ rest.each do |path|
116
+ doc.merge(path)
117
+ end
118
+ doc
119
+ end
120
+
121
+
122
+
123
+ # merges the pdfs in directory
124
+ # @param dir [String] the path of the directory
125
+ # @param name [String,Nil] the name of the returned pdf file
126
+ # if the name is nil, the name is "merged.pdf"
127
+ # @param output_dir [String,Nil] the name of the output dir
128
+ # if the output_dir is nil, the output dir is the dir param
129
+ # return [Boolean] if the merge was successful or not
130
+ def merge_pdfs_in_dir(dir, name: nil , output_dir: nil)
131
+ name = lname || "merged.pdf"
132
+ dir = output_dir || dir
133
+ pdfs = Pathname.glob( dir + '*.pdf')
134
+ doc = merge_pdfs(pdfs)
135
+ doc
136
+ end
137
+
138
+ # quit the Adobe App.
139
+ # closes the open adobe documents and quits the program
140
+ # @return nil
141
+ def quit
142
+ begin
143
+ docs.each{ |d| d.close}
144
+ ole_obj.Exit
145
+ rescue
146
+ return nil
147
+ end
148
+ nil
149
+ end
150
+
151
+ def docs
152
+ @docs
153
+ end
154
+
155
+ # open the file.
156
+ # @param file [String #to_path]
157
+ # @return [PDoc] the open file as a Pdoc instance
158
+ def open(file)
159
+ filepath = Pathname(file).expand_path
160
+ raise FileNotFound unless filepath.file?
161
+ pdoc = WIN32OLE.new('AcroExch.PDDoc')
162
+ is_opened = pdoc.open FileSystemObject.windows_path(filepath)
163
+ doc = PDoc.new(self, pdoc, filepath) if is_opened
164
+ docs << doc if is_opened
165
+ return doc unless block_given?
166
+ begin
167
+ yield doc
168
+ ensure
169
+ doc.close
170
+ doc = nil
171
+ end
172
+ end
173
+
174
+
175
+ def form
176
+ Form.new(self,WIN32OLE.new("AFormAut.App"))
177
+ end
178
+
179
+ private
180
+
181
+ def load_constants(ole_obj)
182
+ WIN32OLE.const_load(ole_obj, ACRO) unless ACRO.constants.size > 0
183
+ end
184
+
185
+ end
186
+
187
+ end