fillable-pdf-th 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rubocop.yml +35 -0
- data/.travis.yml +10 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +248 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/forms-7.1.7.jar +0 -0
- data/ext/io-7.1.7.jar +0 -0
- data/ext/kernel-7.1.7.jar +0 -0
- data/ext/layout-7.1.7.jar +0 -0
- data/ext/slf4j-api-1.7.28.jar +0 -0
- data/ext/slf4j-simple-1.7.28.jar +0 -0
- data/fillable-pdf-th.gemspec +28 -0
- data/lib/default-font/angsana_new.ttf +0 -0
- data/lib/field.rb +25 -0
- data/lib/fillable-pdf.rb +203 -0
- data/lib/fillable-pdf/itext.rb +3 -0
- data/lib/fillable-pdf/version.rb +3 -0
- metadata +123 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '0883aae0f2aa55f745ae1a862fd387900434694853fb0b5420742fcb140697b0'
|
4
|
+
data.tar.gz: 948d65d76a5fcf0a4cd11d7db614e46675841913e04d2c69709332da2af7c990
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 782a06991cf25df8a9506fa2a1b748c3cc327f653ad993c71f414e2c16b5ed875ae566b97c31248cc1e1ff8cc415257bd9d8d52e0424081612ca3e1e75a3798c
|
7
|
+
data.tar.gz: 845ac00ff2db6fa63d9fbb98b382a47b9382ab5439a9c94cc9e2f140a9d0e7e5e1df701c4791eaf7cdb16580b51badd7923eb412e7974f8c97bdd41ff3ea803e
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require:
|
2
|
+
- rubocop-performance
|
3
|
+
|
4
|
+
AllCops:
|
5
|
+
Exclude:
|
6
|
+
- .git/**/*
|
7
|
+
|
8
|
+
Layout/EmptyLineAfterGuardClause:
|
9
|
+
Enabled: false
|
10
|
+
|
11
|
+
Layout/SpaceInsideHashLiteralBraces:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Metrics/LineLength:
|
15
|
+
Exclude:
|
16
|
+
- fillable-pdf.gemspec
|
17
|
+
Max: 120
|
18
|
+
|
19
|
+
Naming/AccessorMethodName:
|
20
|
+
Enabled: false
|
21
|
+
|
22
|
+
Naming/FileName:
|
23
|
+
Enabled: false
|
24
|
+
|
25
|
+
Style/Documentation:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
Style/FrozenStringLiteralComment:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Style/GuardClause:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
Style/MutableConstant:
|
35
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Vadim Kononov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,248 @@
|
|
1
|
+
# FillablePDF
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/fillable-pdf.svg)](https://rubygems.org/gems/fillable-pdf)
|
4
|
+
[![Build Status](https://api.travis-ci.org/vkononov/fillable-pdf.svg?branch=master)](http://travis-ci.org/vkononov/fillable-pdf)
|
5
|
+
|
6
|
+
FillablePDF is an extremely simple and lightweight utility that bridges iText and Ruby in order to fill out fillable PDF forms or extract field values from previously filled out PDF forms.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
**Ensure that your `JAVA_HOME` variable is set before installing this gem (see examples below).**
|
11
|
+
|
12
|
+
* OSX: `/Library/Java/JavaVirtualMachines/jdk-12.0.2.jdk/Contents/Home`
|
13
|
+
* Ubuntu/CentOS: `/usr/lib/jvm/java-1.8.0-openjdk`
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
gem 'fillable-pdf'
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
gem install fillable-pdf
|
26
|
+
|
27
|
+
If you are using this gem in a script, you need to require it manually:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'fillable-pdf'
|
31
|
+
```
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
First of all, you should open a fillable PDF file:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
pdf = FillablePDF.new 'input.pdf'
|
39
|
+
```
|
40
|
+
|
41
|
+
An instance of `FillablePDF` has the following methods at its disposal:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
fillable-pdf
|
45
|
+
# output example: true
|
46
|
+
pdf.any_fields?
|
47
|
+
```
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
# get the total number of fillable form fields
|
51
|
+
# output example: 10
|
52
|
+
pdf.num_fields
|
53
|
+
```
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
# retrieve a single field value by field name
|
57
|
+
# output example: 'Richard'
|
58
|
+
pdf.field(:full_name)
|
59
|
+
```
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
# retrieve a field type by field name
|
63
|
+
# numeric types should
|
64
|
+
# output example: 4
|
65
|
+
pdf.field_type(:football)
|
66
|
+
|
67
|
+
# list of all field types
|
68
|
+
Field::BUTTON
|
69
|
+
Field::CHOICE
|
70
|
+
Field::SIGNATURE
|
71
|
+
Field::TEXT
|
72
|
+
```
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
# retrieve a hash of field name and values
|
76
|
+
# output example: {:last_name=>"Rahl", :first_name=>"Richard"}
|
77
|
+
pdf.fields
|
78
|
+
```
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# set the value of a single field by field name
|
82
|
+
# result: changes the value of 'first_name' to 'Richard'
|
83
|
+
pdf.set_field(:first_name, 'Richard')
|
84
|
+
```
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
# set the values of multiple fields by field names
|
88
|
+
# result: changes the values of 'first_name' and 'last_name'
|
89
|
+
pdf.set_fields(first_name: 'Richard', last_name: 'Rahl')
|
90
|
+
```
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
# rename field (i.e. change the name of the field)
|
94
|
+
# result: renames field name 'last_name' to 'surname'
|
95
|
+
# NOTE: this action does not take effect until the document is saved
|
96
|
+
pdf.rename_field(:last_name, :surname)
|
97
|
+
```
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
# remove field (i.e. delete field and its value)
|
101
|
+
# result: physically removes field 'last_name' from document
|
102
|
+
pdf.remove_field(:last_name)
|
103
|
+
```
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
# get an array of all field names in the document
|
107
|
+
# output example: [:first_name, :last_name]
|
108
|
+
pdf.names
|
109
|
+
```
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
# get an array of all field values in the document
|
113
|
+
# output example: ["Rahl", "Richard"]
|
114
|
+
pdf.values
|
115
|
+
```
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
# set font in pdf form
|
119
|
+
# default is angsana_new.tiff
|
120
|
+
# your font path: 'font/path/myfont.tiff'
|
121
|
+
pdf.set_font(your_font_path)
|
122
|
+
```
|
123
|
+
|
124
|
+
```ruby
|
125
|
+
# set font size in pdf form
|
126
|
+
# default is 12.00
|
127
|
+
pdf.set_size(15)
|
128
|
+
```
|
129
|
+
|
130
|
+
Once the PDF is filled out you can either overwrite it or save it as another file:
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
pdf.save
|
134
|
+
pdf.save_as('output.pdf')
|
135
|
+
```
|
136
|
+
|
137
|
+
Or if you prefer to flatten the file (i.e. make it non-editable), you can instead use:
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
pdf.save(flatten: true)
|
141
|
+
pdf.save_as('output.pdf', flatten: true)
|
142
|
+
```
|
143
|
+
|
144
|
+
**NOTE:** Saving the file automatically closes the input file, so you would need to reinitialize the `FillabePDF` class before making any more changes or saving another copy.
|
145
|
+
|
146
|
+
## Example
|
147
|
+
|
148
|
+
The following example [example.rb](example/run.rb) and the input file [input.pdf](example/input.pdf) are located in the `test` directory. It uses all of the methods that are described above and generates the output files [output.pdf](example/output.pdf) and [output.flat.pdf](example/output.flat.pdf).
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
require 'fillable-pdf'
|
152
|
+
|
153
|
+
# opening a fillable PDF
|
154
|
+
pdf = FillablePDF.new('input.pdf')
|
155
|
+
|
156
|
+
# total number of fields
|
157
|
+
if pdf.any_fields?
|
158
|
+
puts "The form has a total of #{pdf.num_fields} fields."
|
159
|
+
else
|
160
|
+
puts 'The form is not fillable.'
|
161
|
+
end
|
162
|
+
|
163
|
+
puts
|
164
|
+
|
165
|
+
# setting form fields
|
166
|
+
pdf.set_fields(first_name: 'Richard', last_name: 'Rahl')
|
167
|
+
pdf.set_fields(football: 'Yes', baseball: 'Yes',
|
168
|
+
basketball: 'Yes', nascar: 'Yes', hockey: 'Yes')
|
169
|
+
pdf.set_field(:date, Time.now.strftime('%B %e, %Y'))
|
170
|
+
|
171
|
+
# list of fields
|
172
|
+
puts "Fields hash: #{pdf.fields}"
|
173
|
+
|
174
|
+
puts
|
175
|
+
|
176
|
+
# list of field names
|
177
|
+
puts "Keys: #{pdf.names}"
|
178
|
+
|
179
|
+
puts
|
180
|
+
|
181
|
+
# list of field values
|
182
|
+
puts "Values: #{pdf.values}"
|
183
|
+
|
184
|
+
puts
|
185
|
+
|
186
|
+
# Checking field type
|
187
|
+
if pdf.field_type(:football) == Field::BUTTON
|
188
|
+
puts "Field 'football' is of type BUTTON"
|
189
|
+
else
|
190
|
+
puts "Field 'football' is not of type BUTTON"
|
191
|
+
end
|
192
|
+
|
193
|
+
puts
|
194
|
+
|
195
|
+
# Renaming field
|
196
|
+
pdf.rename_field :last_name, :surname
|
197
|
+
puts "Renamed field 'last_name' to 'surname'"
|
198
|
+
|
199
|
+
puts
|
200
|
+
|
201
|
+
# Removing field
|
202
|
+
pdf.remove_field :nascar
|
203
|
+
puts "Removed field 'nascar'"
|
204
|
+
puts
|
205
|
+
|
206
|
+
# printing the name of the person used inside the PDF
|
207
|
+
puts "Signatory: #{pdf.field(:first_name)} #{pdf.field(:last_name)}"
|
208
|
+
|
209
|
+
# saving the filled out PDF in another file
|
210
|
+
pdf.save_as('output.pdf')
|
211
|
+
|
212
|
+
# saving another copy of the filled out PDF in another file and making it non-editable
|
213
|
+
pdf = FillablePDF.new('output.pdf')
|
214
|
+
pdf.save_as 'output.flat.pdf', flatten: true
|
215
|
+
```
|
216
|
+
|
217
|
+
The example above produces the following output and also generates the output file [output.pdf](example/output.pdf).
|
218
|
+
|
219
|
+
```
|
220
|
+
The form has a total of 8 fields.
|
221
|
+
|
222
|
+
Fields hash: {:last_name=>"Rahl", :first_name=>"Richard", :football=>"Yes", :baseball=>"Yes", :basketball=>"Yes", :nascar=>"Yes", :hockey=>"Yes", :date=>"August 30, 2019"}
|
223
|
+
|
224
|
+
Keys: [:last_name, :first_name, :football, :baseball, :basketball, :nascar, :hockey, :date]
|
225
|
+
|
226
|
+
Values: ["Rahl", "Richard", "Yes", "Yes", "Yes", "Yes", "Yes", "August 30, 2019"]
|
227
|
+
|
228
|
+
Field 'football' is of type BUTTON
|
229
|
+
|
230
|
+
Renamed field 'last_name' to 'surname'
|
231
|
+
|
232
|
+
Removed field 'nascar'
|
233
|
+
|
234
|
+
Signatory: Richard Rahl
|
235
|
+
```
|
236
|
+
|
237
|
+
## Contributing
|
238
|
+
|
239
|
+
1. Fork it
|
240
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
241
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
242
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
243
|
+
5. Create new Pull Request
|
244
|
+
|
245
|
+
|
246
|
+
## License
|
247
|
+
|
248
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'fillable-pdf'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require 'pry'
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/ext/forms-7.1.7.jar
ADDED
Binary file
|
data/ext/io-7.1.7.jar
ADDED
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'fillable-pdf/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'fillable-pdf-th'
|
7
|
+
spec.version = FillablePDF::VERSION
|
8
|
+
spec.authors = ['Swiftlet']
|
9
|
+
spec.email = ['swiftlet.dev@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'Fill out or extract field values from simple fillable PDF forms using iText.'
|
12
|
+
spec.description = 'fillable-pdf-th is an extension from fillable-pdf(vkononov) but supported Thai language.'
|
13
|
+
spec.homepage = 'https://github.com/swiftlet-dev/fillable-pdf-th'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
17
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(example|test|spec|features)/}) }
|
18
|
+
end
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = %w[ext lib]
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
24
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
|
27
|
+
spec.add_runtime_dependency 'rjb', '~> 1.6'
|
28
|
+
end
|
Binary file
|
data/lib/field.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require_relative 'fillable-pdf/itext'
|
2
|
+
|
3
|
+
class Field
|
4
|
+
class << self
|
5
|
+
def pdf_name
|
6
|
+
@pdf_nanme ||= Rjb.import('com.itextpdf.kernel.pdf.PdfName')
|
7
|
+
end
|
8
|
+
|
9
|
+
def button
|
10
|
+
@button = pdf_name.Btn.toString
|
11
|
+
end
|
12
|
+
|
13
|
+
def choice
|
14
|
+
@choice = pdf_name.Ch.toString
|
15
|
+
end
|
16
|
+
|
17
|
+
def signature
|
18
|
+
@signature = pdf_name.Sig.toString
|
19
|
+
end
|
20
|
+
|
21
|
+
def text
|
22
|
+
@text = pdf_name.Tx.toString
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/fillable-pdf.rb
ADDED
@@ -0,0 +1,203 @@
|
|
1
|
+
require_relative 'fillable-pdf/itext'
|
2
|
+
require_relative 'field'
|
3
|
+
require 'securerandom'
|
4
|
+
|
5
|
+
class FillablePDF
|
6
|
+
# required Java imports
|
7
|
+
BYTE_STREAM = Rjb.import 'com.itextpdf.io.source.ByteArrayOutputStream'
|
8
|
+
PDF_READER = Rjb.import 'com.itextpdf.kernel.pdf.PdfReader'
|
9
|
+
PDF_WRITER = Rjb.import 'com.itextpdf.kernel.pdf.PdfWriter'
|
10
|
+
PDF_DOCUMENT = Rjb.import 'com.itextpdf.kernel.pdf.PdfDocument'
|
11
|
+
PDF_ACRO_FORM = Rjb.import 'com.itextpdf.forms.PdfAcroForm'
|
12
|
+
PDF_FORM_FIELD = Rjb.import 'com.itextpdf.forms.fields.PdfFormField'
|
13
|
+
PDF_FONT_ENCODE = Rjb.import 'com.itextpdf.io.font.PdfEncodings'
|
14
|
+
PDF_FONT = Rjb.import 'com.itextpdf.kernel.font.PdfFont'
|
15
|
+
PDF_FONT_FACTORY = Rjb.import 'com.itextpdf.kernel.font.PdfFontFactory'
|
16
|
+
FONT = './default-font/angsana_new.ttf'
|
17
|
+
##
|
18
|
+
# Opens a given fillable-pdf PDF file and prepares it for modification.
|
19
|
+
#
|
20
|
+
# @param [String|Symbol] file_path the name of the PDF file or file path
|
21
|
+
#
|
22
|
+
def initialize(file_path)
|
23
|
+
raise IOError, "File at `#{file_path}' is not found" unless File.exist?(file_path)
|
24
|
+
@file_path = file_path
|
25
|
+
@byte_stream = BYTE_STREAM.new
|
26
|
+
@pdf_reader = PDF_READER.new @file_path
|
27
|
+
@pdf_writer = PDF_WRITER.new @byte_stream
|
28
|
+
@pdf_doc = PDF_DOCUMENT.new @pdf_reader, @pdf_writer
|
29
|
+
@pdf_form = PDF_ACRO_FORM.getAcroForm(@pdf_doc, true)
|
30
|
+
@size = 12.00
|
31
|
+
set_font
|
32
|
+
@form_fields = @pdf_form.getFormFields
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Set font in PDF file
|
37
|
+
#
|
38
|
+
def set_font(font_path=FONT)
|
39
|
+
@pdf_font = PDF_FONT_FACTORY.createFont(font_path, PDF_FONT_ENCODE.IDENTITY_H)
|
40
|
+
end
|
41
|
+
|
42
|
+
def set_size(size=@size)
|
43
|
+
@size = size.to_f
|
44
|
+
end
|
45
|
+
##
|
46
|
+
# Determines whether the form has any fields.
|
47
|
+
#
|
48
|
+
# @return true if form has fields, false otherwise
|
49
|
+
#
|
50
|
+
def any_fields?
|
51
|
+
num_fields.positive?
|
52
|
+
end
|
53
|
+
|
54
|
+
##
|
55
|
+
# Returns the total number of form fields.
|
56
|
+
#
|
57
|
+
# @return the number of fields
|
58
|
+
#
|
59
|
+
def num_fields
|
60
|
+
@form_fields.size
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# Retrieves the value of a field given its unique field name.
|
65
|
+
#
|
66
|
+
# @param [String|Symbol] key the field name
|
67
|
+
#
|
68
|
+
# @return the value of the field
|
69
|
+
#
|
70
|
+
def field(key)
|
71
|
+
pdf_field(key).getValueAsString
|
72
|
+
rescue NoMethodError
|
73
|
+
raise "unknown key name `#{key}'"
|
74
|
+
end
|
75
|
+
|
76
|
+
##
|
77
|
+
# Retrieves the numeric type of a field given its unique field name.
|
78
|
+
#
|
79
|
+
# @param [String|Symbol] key the field name
|
80
|
+
#
|
81
|
+
# @return the type of the field
|
82
|
+
#
|
83
|
+
def field_type(key)
|
84
|
+
pdf_field(key).getFormType.toString
|
85
|
+
end
|
86
|
+
|
87
|
+
##
|
88
|
+
# Retrieves a hash of all fields and their values.
|
89
|
+
#
|
90
|
+
# @return the hash of field keys and values
|
91
|
+
#
|
92
|
+
def fields
|
93
|
+
iterator = @form_fields.keySet.iterator
|
94
|
+
map = {}
|
95
|
+
while iterator.hasNext
|
96
|
+
key = iterator.next.toString
|
97
|
+
map[key.to_sym] = field(key)
|
98
|
+
end
|
99
|
+
map
|
100
|
+
end
|
101
|
+
|
102
|
+
##
|
103
|
+
# Sets the value of a field given its unique field name and value.
|
104
|
+
#
|
105
|
+
# @param [String|Symbol] key the field name
|
106
|
+
# @param [String|Symbol] value the field value
|
107
|
+
#
|
108
|
+
def set_field(key, value)
|
109
|
+
pdf_field(key).setValue(value.to_s, @pdf_font, @size)
|
110
|
+
end
|
111
|
+
|
112
|
+
##
|
113
|
+
# Sets the values of multiple fields given a set of unique field names and values.
|
114
|
+
#
|
115
|
+
# @param [Hash] fields the set of field names and values
|
116
|
+
#
|
117
|
+
def set_fields(fields)
|
118
|
+
fields.each { |key, value| set_field key, value }
|
119
|
+
end
|
120
|
+
|
121
|
+
##
|
122
|
+
# Renames a field given its unique field name and the new field name.
|
123
|
+
#
|
124
|
+
# @param [String|Symbol] old_key the field name
|
125
|
+
# @param [String|Symbol] new_key the field name
|
126
|
+
#
|
127
|
+
def rename_field(old_key, new_key)
|
128
|
+
pdf_field(old_key).setFieldName(new_key.to_s)
|
129
|
+
end
|
130
|
+
|
131
|
+
##
|
132
|
+
# Removes a field from the document given its unique field name.
|
133
|
+
#
|
134
|
+
# @param [String|Symbol] key the field name
|
135
|
+
#
|
136
|
+
def remove_field(key)
|
137
|
+
@pdf_form.removeField(key.to_s)
|
138
|
+
end
|
139
|
+
|
140
|
+
##
|
141
|
+
# Returns a list of all field keys used in the document.
|
142
|
+
#
|
143
|
+
# @return array of field names
|
144
|
+
#
|
145
|
+
def names
|
146
|
+
iterator = @form_fields.keySet.iterator
|
147
|
+
set = []
|
148
|
+
set << iterator.next.toString.to_sym while iterator.hasNext
|
149
|
+
set
|
150
|
+
end
|
151
|
+
|
152
|
+
##
|
153
|
+
# Returns a list of all field values used in the document.
|
154
|
+
#
|
155
|
+
# @return array of field values
|
156
|
+
#
|
157
|
+
def values
|
158
|
+
iterator = @form_fields.keySet.iterator
|
159
|
+
set = []
|
160
|
+
set << field(iterator.next.toString) while iterator.hasNext
|
161
|
+
set
|
162
|
+
end
|
163
|
+
|
164
|
+
##
|
165
|
+
# Overwrites the previously opened PDF file and flattens it if requested.
|
166
|
+
#
|
167
|
+
# @param [bool] flatten true if PDF should be flattened, false otherwise
|
168
|
+
#
|
169
|
+
def save(flatten: false)
|
170
|
+
tmp_file = SecureRandom.uuid
|
171
|
+
save_as(tmp_file, flatten: flatten)
|
172
|
+
File.rename tmp_file, @file_path
|
173
|
+
end
|
174
|
+
|
175
|
+
##
|
176
|
+
# Saves the filled out PDF file with a given file and flattens it if requested.
|
177
|
+
#
|
178
|
+
# @param [String] file_path the name of the PDF file or file path
|
179
|
+
# @param [Hash] flatten: true if PDF should be flattened, false otherwise
|
180
|
+
#
|
181
|
+
def save_as(file_path, flatten: false)
|
182
|
+
File.open(file_path, 'wb') { |f| f.write(finalize(flatten: flatten)) && f.close }
|
183
|
+
end
|
184
|
+
|
185
|
+
private
|
186
|
+
|
187
|
+
##
|
188
|
+
# Writes the contents of the modified fields to the previously opened PDF file.
|
189
|
+
#
|
190
|
+
# @param [Hash] flatten: true if PDF should be flattened, false otherwise
|
191
|
+
#
|
192
|
+
def finalize(flatten: false)
|
193
|
+
@pdf_form.flattenFields if flatten
|
194
|
+
@pdf_doc.close
|
195
|
+
@byte_stream.toByteArray
|
196
|
+
end
|
197
|
+
|
198
|
+
def pdf_field(key)
|
199
|
+
field = @form_fields.get(key.to_s)
|
200
|
+
raise "unknown key name `#{key}'" if field.nil?
|
201
|
+
field
|
202
|
+
end
|
203
|
+
end
|
metadata
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fillable-pdf-th
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Swiftlet
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-10-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rjb
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.6'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.6'
|
69
|
+
description: fillable-pdf-th is an extension from fillable-pdf(vkononov) but supported
|
70
|
+
Thai language.
|
71
|
+
email:
|
72
|
+
- swiftlet.dev@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- ext/forms-7.1.7.jar
|
87
|
+
- ext/io-7.1.7.jar
|
88
|
+
- ext/kernel-7.1.7.jar
|
89
|
+
- ext/layout-7.1.7.jar
|
90
|
+
- ext/slf4j-api-1.7.28.jar
|
91
|
+
- ext/slf4j-simple-1.7.28.jar
|
92
|
+
- fillable-pdf-th.gemspec
|
93
|
+
- lib/default-font/angsana_new.ttf
|
94
|
+
- lib/field.rb
|
95
|
+
- lib/fillable-pdf.rb
|
96
|
+
- lib/fillable-pdf/itext.rb
|
97
|
+
- lib/fillable-pdf/version.rb
|
98
|
+
homepage: https://github.com/swiftlet-dev/fillable-pdf-th
|
99
|
+
licenses:
|
100
|
+
- MIT
|
101
|
+
metadata: {}
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- ext
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
requirements: []
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.7.9
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: Fill out or extract field values from simple fillable PDF forms using iText.
|
123
|
+
test_files: []
|