acrobat 0.1.0 → 0.3.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/lib/acrobat/app.rb CHANGED
@@ -1,187 +1,197 @@
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
+ # rbs_inline: enable
2
+
3
+ require "win32ole"
4
+ require_relative "pdoc"
5
+ require_relative "avdoc"
6
+ require_relative "jso"
7
+
8
+ module Acrobat
9
+ class FileNotFound < StandardError
10
+ def initialize(path)
11
+ super("File not found: #{path}")
12
+ end
13
+ end
14
+ end
15
+
16
+ module FileSystemObject
17
+ @instance = nil
18
+ def self.instance
19
+ @instance ||= WIN32OLE.new("Scripting.FileSystemObject")
20
+ @instance
21
+ end
22
+
23
+ def self.windows_path(path)
24
+ FileSystemObject.instance.GetAbsolutePathname(path.to_s)
25
+ end
26
+ end
27
+
28
+ module ACRO; end
29
+
30
+ module Acrobat
31
+ class App
32
+ # [WIN32_OLE] ole_obj
33
+ attr_reader :ole_obj # :WIN32OLE
34
+
35
+ def initialize # : Void
36
+ @ole_obj = WIN32OLE.new("AcroExch.App")
37
+ unless @ole_obj
38
+ @ole_obj = WIN32OLE.connect("AcroExch.App")
39
+ end
40
+ load_constants(@ole_obj)
41
+ @docs = []
42
+ end
43
+
44
+ def self.close(path)
45
+ name = File.basename(path)
46
+ run do |app|
47
+ av = app.avdocs.find{|a| a.pdoc_name == name}
48
+ av&.close
49
+ end
50
+ end
51
+
52
+
53
+
54
+ # Runs the adobe app and quits at the end
55
+ # @example
56
+ # Acrobat::App.run do |app|
57
+ # doc = app.open('doc.pdf')
58
+ # doc.fill_form( city: 'City', state: 'ST')
59
+ # doc.save_as('filled.pdf')
60
+ # end
61
+ #
62
+ # @rbs return Void
63
+ def self.run
64
+ the_app = new
65
+ yield the_app
66
+ ensure
67
+ the_app&.quit
68
+ GC.start
69
+ end
70
+
71
+ def self.replace_pages(pdf_file, replacement, output_name:, **opts)
72
+ run do |app|
73
+ app.open(pdf_file) do |doc|
74
+ doc.replace_pages(replacement, **opts)
75
+ doc.save_as(output_name)
76
+ end
77
+ end
78
+ end
79
+
80
+ # Fills the form with updates in a hash
81
+ # @example
82
+ # Acrobat::App.fill_form(myform.pdf, output_name: 'filled.pdf
83
+ # , update_hash: { name: 'dom', filled_date: 1/20/2013
84
+ # @rbs pdf_form: String -- the String path of a fillable pdf file
85
+ # @rbs output_name: String -- the name of the saved filled pdf file
86
+ # @rbs update_hash: Hash -- the hash with updates
87
+ def self.fill_form(pdf_form, output_name:, update_hash:)
88
+ run do |app|
89
+ doc = app.open(pdf_form)
90
+ doc.fill_form(update_hash)
91
+ doc.save_as(output_name)
92
+ end
93
+ end
94
+
95
+ # @rbs return Array[AvDoc]
96
+ # @rbs &: (AvDoc) -> Void
97
+ def avdocs
98
+ count = ole_obj.GetNumAVDocs
99
+ return [] if count == 0
100
+ return to_enum(:avdocs) unless block_given?
101
+ av_array = []
102
+ count.times do |i|
103
+ ole = ole_obj.GetAVDoc(i)
104
+ raise "Wrong index for GetNumAVDocs #{i -1}" unless ole
105
+ av = AvDoc.new(ole)
106
+ if block_given?
107
+ yield av
108
+ else
109
+ av_array << av
110
+ end
111
+ end
112
+ av_array unless block_given?
113
+ end
114
+
115
+
116
+ # show the Adobe Acrobat application
117
+ def show # : Void
118
+ ole_obj.Show
119
+ end
120
+
121
+ # hide the Adobe Acrobat application
122
+ def hide # : Void
123
+ ole_obj.Hide
124
+ end
125
+
126
+ # Finds the pdfs in a dir
127
+ # @rbs dir: String -- the directory to find pdfs in
128
+ # @rbs return Array[Pathname] -- of pdf files
129
+ def find_pdfs_in_dir(dir)
130
+ Pathname.glob(dir + "/*.pdf")
131
+ end
132
+
133
+ def merge_pdfs(*pdfs)
134
+ pdf_array = Array(pdfs).flatten
135
+ raise "Not enough pdfs to merge" if pdf_array.size < 2
136
+ first, *rest = pdf_array
137
+ doc = self.open(first)
138
+ rest.each do |path|
139
+ doc.merge(path)
140
+ end
141
+ doc
142
+ end
143
+
144
+ # merges the pdfs in directory
145
+ # @rbs dir: String -- the path of the directory
146
+ # @rbs name: String | Nil -- the name of the returned pdf file
147
+ # if the name is nil, the name is "merged.pdf"
148
+ # @rbs output_dir: String | Nil -- the name of the output dir
149
+ # if the output_dir is nil, the output dir is the dir param
150
+ # return [Boolean] if the merge was successful or not
151
+ def merge_pdfs_in_dir(dir, name: nil, output_dir: nil)
152
+ name || "merged.pdf"
153
+ dir = output_dir || dir
154
+ merge_pdfs(find_pdfs_in_dir(dir))
155
+ end
156
+
157
+ # quit the Adobe App.
158
+ # closes the open adobe documents and quits the program
159
+ # @rbs return nil
160
+ def quit
161
+ begin
162
+ docs.each { |d| d.close }
163
+ ole_obj.Exit
164
+ rescue
165
+ return nil
166
+ end
167
+ nil
168
+ end
169
+
170
+ attr_reader :docs
171
+
172
+ # open the file.
173
+ # @rbs file: String | Pathname -- #to_path
174
+ # @rbs &: (PDoc) -> Nil
175
+ # @rbs return PDoc -- the open file as a Pdoc instance
176
+ def open(file)
177
+ doc = PDoc.from_path(file)
178
+ docs << doc
179
+ return doc unless block_given?
180
+ begin
181
+ yield doc
182
+ ensure
183
+ doc.close
184
+ end
185
+ end
186
+
187
+ def form
188
+ Form.new(self, WIN32OLE.new("AFormAut.App"))
189
+ end
190
+
191
+ private
192
+
193
+ def load_constants(ole_obj)
194
+ WIN32OLE.const_load(ole_obj, ACRO) unless ACRO.constants.size > 0
195
+ end
196
+ end
197
+ end
@@ -0,0 +1,22 @@
1
+ module Acrobat
2
+ class AvDoc
3
+ attr_reader :ole_obj #: WIN32OLE
4
+
5
+ attr_reader :pdoc_name #: String
6
+
7
+ def initialize( ole)
8
+ @ole_obj = ole
9
+ @pdoc_name = ole.GetPDDoc.GetFileName
10
+ end
11
+
12
+ # Close the AvDoc
13
+ # @rbs save: Boolean -- if true asks to save if false closes without saving
14
+ def close(save = false)
15
+ ole_save = save ? 0 : 1
16
+ @ole_obj.Close(ole_save)
17
+ @ole_obj = nil
18
+ true
19
+ end
20
+
21
+ end
22
+ end
data/lib/acrobat/jso.rb CHANGED
@@ -1,94 +1,87 @@
1
- module Acrobat
2
-
3
- class Jso
4
-
5
- attr_reader :doc, :ole_obj
6
-
7
- def initialize(doc,ole)
8
- @doc = doc
9
- @ole_obj = ole
10
- end
11
-
12
- def find_field(name_or_number)
13
- case name_or_number
14
- when String,Symbol
15
- ole_get_field(name_or_number.to_s)
16
- when Number
17
- ole_get_field(name_or_number)
18
- end
19
- end
20
-
21
- def ole_get_field(field)
22
- ole_obj.getField(field)
23
- end
24
-
25
- def console
26
- @console ||= ole_obj.console
27
- end
28
-
29
- def show_console
30
- console.show
31
- end
32
-
33
- def field_names
34
- result = []
35
- count = field_count
36
- 0.upto(count-1) do |i|
37
- result << ole_obj.getNthFieldName(i)
38
- end
39
- result
40
- end
41
-
42
- def export_as_fdf(name)
43
- end
44
-
45
- def import_fdf(path)
46
- end
47
-
48
-
49
- def fields_hash
50
- result = {}
51
- field_names.each_with_object( result ) do |name, h|
52
- h[name] = get_field(name)
53
- end
54
- end
55
-
56
-
57
- # // Enumerate through all of the fields in the document.
58
- # for (var i = 0; i < this.numFields; i++)
59
- # console.println("Field[" + i + "] = " + this.getNthFieldName(i));
60
-
61
- def set_field(name,value)
62
- begin
63
- field = find_field(name)
64
- field.Value = value.to_s if field
65
- rescue
66
- require 'pry'
67
- binding.pry
68
- nil
69
- end
70
- end
71
-
72
- def get_field(name)
73
- field = find_field(name)
74
- field.Value if field
75
- end
76
-
77
- def field_count
78
- ole_obj.numFields().to_int
79
- end
80
-
81
- def clear_form
82
- ole_obj.resetForm
83
- end
84
-
85
- def fill_form(hash)
86
- clear_form
87
- hash.each do |k,v|
88
- set_field(k,v)
89
- end
90
- end
91
-
92
- end
93
-
94
- end
1
+ module Acrobat
2
+ class Jso
3
+ attr_reader :doc, :ole_obj
4
+
5
+ def initialize(doc, ole)
6
+ @doc = doc
7
+ @ole_obj = ole
8
+ end
9
+
10
+ def find_field(name_or_number)
11
+ case name_or_number
12
+ when String, Symbol
13
+ ole_get_field(name_or_number.to_s)
14
+ when Number
15
+ ole_get_field(name_or_number)
16
+ end
17
+ end
18
+
19
+ def ole_get_field(field)
20
+ ole_obj.getField(field)
21
+ end
22
+
23
+ def console
24
+ @console ||= ole_obj.console
25
+ end
26
+
27
+ def show_console
28
+ console.show
29
+ end
30
+
31
+ def field_names
32
+ result = []
33
+ count = field_count
34
+ 0.upto(count - 1) do |i|
35
+ result << ole_obj.getNthFieldName(i)
36
+ end
37
+ result
38
+ end
39
+
40
+ def export_as_fdf(name)
41
+ end
42
+
43
+ def import_fdf(path)
44
+ end
45
+
46
+ def fields_hash
47
+ result = {}
48
+ field_names.each_with_object(result) do |name, h|
49
+ h[name] = get_field(name)
50
+ end
51
+ end
52
+
53
+
54
+ # // Enumerate through all of the fields in the document.
55
+ # for (var i = 0; i < this.numFields; i++)
56
+ # console.println("Field[" + i + "] = " + this.getNthFieldName(i));
57
+
58
+ def set_field(name, value)
59
+ field = find_field(name)
60
+ field.Value = value.to_s if field
61
+ rescue
62
+ require "pry"
63
+ binding.pry
64
+ nil
65
+ end
66
+
67
+ def get_field(name)
68
+ field = find_field(name)
69
+ field&.Value
70
+ end
71
+
72
+ def field_count
73
+ ole_obj.numFields.to_int
74
+ end
75
+
76
+ def clear_form
77
+ ole_obj.resetForm
78
+ end
79
+
80
+ def fill_form(hash)
81
+ clear_form
82
+ hash.each do |k, v|
83
+ set_field(k, v)
84
+ end
85
+ end
86
+ end
87
+ end