fillable-pdf 0.9.4 → 0.9.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3295f6a5e77e53fb9ae16f59cb1456483daecf203857cd903fd84622336f9d35
4
- data.tar.gz: 4bb925c8f5908f663d36f93afb701c2dc5dcd744f476a50b2bf17fc73ff97f6d
3
+ metadata.gz: d8b685f435c8356edc53a47ca0581ae1fa71c57cb23876b12e3ba7bef0d4aa99
4
+ data.tar.gz: 84f4dcd7ac4d566493dcc37a3ea3e7d776c6f8a28b1129465304ad198c2a7530
5
5
  SHA512:
6
- metadata.gz: ae5c10683e12cda0c209126d256b6f223d2b4194c85b06be9af9e76c12a710c8fe5d8fe6b3ff646d0b285ff93d0f97a8a0bd0c655474b52746a7c43460fd7925
7
- data.tar.gz: a793a4b88d8034f9d3194fd19c07244c4d1083477f9004307208c1b9b23764fc0dde863f6f2a54c0abb264c90f5b2d05360b850fbc074afe9f259d4f51a449cf
6
+ metadata.gz: 575e161cc77a304578997b4d2f967019df98382be944b0fbfd1f6e171e97545bf265a84c2b70a1a80515de520517c303fcfdf27db48ce64c0bae3a292832e6a7
7
+ data.tar.gz: aaaf3eee62d9886660ca753b3a06495d73ba9092d10a56e415bc5283a0056a9418d53ce9df1bda8d7030ed723e93cd1473a59107515800a6715da05eaacf7da5
@@ -0,0 +1,32 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+
6
+ # GitHub recommends pinning actions to a commit SHA.
7
+ # To get a newer version, you will need to update the SHA.
8
+ # You can also reference a tag or branch, but the action may change without warning.
9
+
10
+ name: test
11
+
12
+ on: [push, pull_request]
13
+
14
+ jobs:
15
+ test:
16
+
17
+ runs-on: ubuntu-latest
18
+
19
+ strategy:
20
+ matrix:
21
+ ruby-version: ['3.1', '3.0', '2.7', '2.6', '2.5', '2.4']
22
+
23
+ steps:
24
+ - uses: actions/checkout@v3
25
+ - name: Set up Ruby ${{ matrix.ruby-version }}
26
+ uses: ruby/setup-ruby@359bebbc29cbe6c87da6bc9ea3bc930432750108
27
+ with:
28
+ ruby-version: ${{ matrix.ruby-version }}
29
+ - name: Install dependencies
30
+ run: bundle install
31
+ - name: Run tests
32
+ run: bundle exec rake test
data/.rubocop.yml CHANGED
@@ -29,6 +29,7 @@ Layout/LineLength:
29
29
  - README.md
30
30
  - fillable-pdf.gemspec
31
31
  Max: 120
32
+ AllowedPatterns: ['^(\s*#)']
32
33
 
33
34
  Layout/SpaceInsideHashLiteralBraces:
34
35
  Enabled: false
@@ -55,5 +56,8 @@ Style/FrozenStringLiteralComment:
55
56
  Style/GuardClause:
56
57
  Enabled: false
57
58
 
59
+ Style/HashSyntax:
60
+ Enabled: false
61
+
58
62
  Style/MutableConstant:
59
63
  Enabled: false
data/README.md CHANGED
@@ -2,30 +2,61 @@
2
2
  # FillablePDF
3
3
 
4
4
  [![Gem Version](https://badge.fury.io/rb/fillable-pdf.svg)](https://rubygems.org/gems/fillable-pdf)
5
- [![Build Status](https://app.travis-ci.com/vkononov/fillable-pdf.svg?branch=master)](http://travis-ci.org/vkononov/fillable-pdf)
5
+ [![Test Status](https://github.com/vkononov/fillable-pdf/actions/workflows/test.yml/badge.svg)](https://github.com/vkononov/fillable-pdf/actions)
6
6
 
7
7
  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.
8
8
 
9
+ [!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png)](https://www.buymeacoffee.com/vkononov)
9
10
 
10
11
  ## Known Issues
11
12
 
12
- 1. This gem currently does not work with Phusion Passenger's [smart spawning](https://www.phusionpassenger.com/library/indepth/ruby/spawn_methods/#the-smart-spawning-method). Please see [Deployment with Phusion Passenger + Nginx](#deployment-with-phusion-passenger--nginx) for more information.
13
+ 1. Phusion Passenger's [smart spawning](https://www.phusionpassenger.com/library/indepth/ruby/spawn_methods/#the-smart-spawning-method) is not supported. Please see [Deployment with Phusion Passenger + Nginx](#deployment-with-phusion-passenger--nginx) for more information.
13
14
 
14
- 2. If the gem hangs in `development`, removing the following gems may fix the issue:
15
+ 2. Puma workers (process forking) is not supposed due to an [issue](https://github.com/arton/rjb/issues/88) with the [rjb](https://github.com/arton/rjb) gem dependency.
16
+
17
+ 3. If the gem hangs in `development`, removing the following gems may fix the issue:
15
18
 
16
19
  ```ruby
17
20
  gem 'spring'
18
21
  gem 'spring-watcher-listen'
19
22
  ```
20
23
 
21
- 3. Read-only, write-protected or encrypted PDF files are currently not supported.
24
+ 4. Read-only, write-protected or encrypted PDF files are currently not supported.
25
+
26
+ 5. Adobe generated field arrays (i.e. fields with names such as `array.0` or `array.1.0`) are not supported.
27
+
28
+
29
+ ## Troubleshooting Issues
30
+
31
+ ### Blank Fields
32
+
33
+ * **Actual Result:**
34
+
35
+ ![Blank](images/blank.png)
36
+
37
+ * **Expected Result:**
38
+
39
+ ![Blank](images/checked.png)
40
+
41
+ If only of the fields are blank, try setting the `generate_appearance` flag to `true` when calling `set_field` or `set_fields`.
42
+
43
+ ### Invalid Checkbox Appearances
44
+
45
+ * **Actual Result:**
46
+
47
+ ![Blank](images/checked.png)
48
+
49
+ * **Expected Result:**
22
50
 
51
+ ![Blank](images/distinct.png)
52
+
53
+ If your checkboxes are showing incorrectly, it's likely because iText is overwriting your checkbox appearances. Try setting the `generate_appearance` flag to `false` when calling `set_field` or `set_fields`.
23
54
 
24
55
  ## Installation
25
56
 
26
57
  **Prerequisites:** Java SE Development Kit v8, v11
27
58
 
28
- - Ensure that your `JAVA_HOME` variable is set before installing this gem (see examples below).**
59
+ - Ensure that your `JAVA_HOME` variable is set before installing this gem (see examples below).
29
60
 
30
61
  * OSX: `/Library/Java/JavaVirtualMachines/jdk-11.0.2.jdk/Contents/Home`
31
62
  * Ubuntu/CentOS: `/usr/lib/jvm/java-1.8.0-openjdk`
@@ -126,19 +157,21 @@ An instance of `FillablePDF` has the following methods at its disposal:
126
157
  pdf.num_fields
127
158
  ```
128
159
 
129
- * `field`
160
+ * `field(key)`
130
161
  *Retrieves the value of a field given its unique field name.*
131
162
 
132
163
  ```ruby
133
164
  pdf.field(:full_name)
165
+ pdf.field('full_name')
134
166
  # output example: 'Richard'
135
167
  ```
136
168
 
137
- * `field_type`
138
- *Retrieves the numeric type of a field given its unique field name.*
169
+ * `field_type(key)`
170
+ *Retrieves the string type of a field given its unique field name.*
139
171
 
140
172
  ```ruby
141
173
  pdf.field_type(:football)
174
+ pdf.field_type('football')
142
175
  # output example: '/Btn'
143
176
 
144
177
  # list of all field types
@@ -148,6 +181,13 @@ An instance of `FillablePDF` has the following methods at its disposal:
148
181
  Field::TEXT ('/Tx')
149
182
  ```
150
183
 
184
+ You can check the field type by using:
185
+
186
+ ```ruby
187
+ pdf.field_type(:football) == Field::BUTTON
188
+ pdf.field_type('football') == Field::BUTTON
189
+ ```
190
+
151
191
  * `fields`
152
192
  *Retrieves a hash of all fields and their values.*
153
193
 
@@ -156,52 +196,71 @@ An instance of `FillablePDF` has the following methods at its disposal:
156
196
  # output example: {first_name: "Richard", last_name: "Rahl"}
157
197
  ```
158
198
 
159
- * `set_field`
160
- *Sets the value of a field given its unique field name and value.*
199
+ * `set_field(key, value, generate_appearance: nil)`
200
+ *Sets the value of a field given its unique field name and value, with an optional `generate_appearance` directive.*
161
201
 
162
202
  ```ruby
163
203
  pdf.set_field(:first_name, 'Richard')
204
+ pdf.set_field('first_name', 'Richard')
164
205
  # result: changes the value of 'first_name' to 'Richard'
165
206
  ```
166
207
 
167
- * `set_fields`
168
- *Sets the values of multiple fields given a set of unique field names and values.*
208
+ Optionally, you can choose to override iText's `generateAppearance` flag to take better control of your field's appearance, using `generate_appearance`. Passing `true` will force the field to generate its own appearance, while setting it to `false` would leave the appearance generation up to the PDF viewer application. Omitting the parameter would allow iText to decide what should happen.
169
209
 
170
210
  ```ruby
171
- pdf.set_fields(first_name: 'Richard', last_name: 'Rahl')
211
+ pdf.set_field(:first_name, 'Richard', generate_appearance: true)
212
+ pdf.set_field('first_name', 'Richard', generate_appearance: false)
213
+ ```
214
+
215
+ * `def set_fields(fields, generate_appearance: nil)`
216
+ *Sets the values of multiple fields given a set of unique field names and values, with an optional `generate_appearance` directive.*
217
+
218
+ ```ruby
219
+ pdf.set_fields({first_name: 'Richard', last_name: 'Rahl'})
172
220
  # result: changes the values of 'first_name' and 'last_name'
173
221
  ```
174
222
 
175
- * `set_image`
223
+ Optionally, you can choose to override iText's `generateAppearance` flag to take better control of your fields' appearance, using `generate_appearance`. Passing `true` will force the field to generate its own appearance, while setting it to `false` would leave the appearance generation up to the PDF viewer application. Omitting the parameter would allow iText to decide what should happen.
224
+
225
+ ```ruby
226
+ pdf.set_fields({first_name: 'Richard', last_name: 'Rahl'}, generate_appearance: true)
227
+ pdf.set_fields({first_name: 'Richard', last_name: 'Rahl'}, generate_appearance: false)
228
+ ```
229
+
230
+ * `set_image(key, file_path)`
176
231
  *Places an image file within the rectangular bounding box of the given form field.*
177
232
 
178
233
  ```ruby
179
234
  pdf.set_image(:signature, 'signature.png')
235
+ pdf.set_image('signature', 'signature.png')
180
236
  # result: the image 'signature.png' is shown in the foreground of the form field
181
237
  ```
182
238
 
183
- * `set_image_base64`
239
+ * `set_image_base64(key, base64_image_data)`
184
240
  *Places a base64 encoded image within the rectangular bounding box of the given form field.*
185
241
 
186
242
  ```ruby
243
+ pdf.set_image_base64('signature', 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==')
187
244
  pdf.set_image_base64(:signature, 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==')
188
245
  # result: the base64 encoded image is shown in the foreground of the form field
189
246
  ```
190
247
 
191
- * `rename_field`
248
+ * `rename_field(old_key, new_key)`
192
249
  *Renames a field given its unique field name and the new field name.*
193
250
 
194
251
  ```ruby
195
252
  pdf.rename_field(:last_name, :surname)
253
+ pdf.rename_field('last_name', 'surname')
196
254
  # result: renames field name 'last_name' to 'surname'
197
255
  # NOTE: this action does not take effect until the document is saved
198
256
  ```
199
257
 
200
- * `remove_field`
258
+ * `remove_field(key)`
201
259
  *Removes a field from the document given its unique field name.*
202
260
 
203
261
  ```ruby
204
262
  pdf.remove_field(:last_name)
263
+ pdf.remove_field('last_name')
205
264
  # result: physically removes field 'last_name' from document
206
265
  ```
207
266
 
@@ -221,7 +280,7 @@ An instance of `FillablePDF` has the following methods at its disposal:
221
280
  # output example: ["Rahl", "Richard"]
222
281
  ```
223
282
 
224
- * `save`
283
+ * `save(flatten: false)`
225
284
  *Overwrites the previously opened PDF document and flattens it if requested.*
226
285
 
227
286
  ```ruby
@@ -231,7 +290,7 @@ An instance of `FillablePDF` has the following methods at its disposal:
231
290
  # result: document is saved with flattening
232
291
  ```
233
292
 
234
- * `save_as`
293
+ * `save_as(file_path, flatten: false)`
235
294
  *Saves the filled out PDF document in a given path and flattens it if requested.*
236
295
 
237
296
  ```ruby
@@ -333,8 +392,8 @@ end
333
392
  puts
334
393
 
335
394
  # setting form fields
336
- pdf.set_fields(first_name: 'Richard', last_name: 'Rahl')
337
- pdf.set_fields(football: 'Yes', baseball: 'Yes', basketball: 'Yes', nascar: 'Yes', hockey: 'Yes')
395
+ pdf.set_fields({first_name: 'Richard', last_name: 'Rahl'})
396
+ pdf.set_fields({football: 'Yes', baseball: 'Yes', basketball: 'Yes', nascar: 'Yes', hockey: 'Yes', rugby: 'Yes'}, generate_appearance: false)
338
397
  pdf.set_field(:date, Time.now.strftime('%B %e, %Y'))
339
398
  pdf.set_field(:newsletter, 'Off') # uncheck the checkbox
340
399
  pdf.set_field(:language, 'dart') # select a radio button option
@@ -357,7 +416,7 @@ puts "Values: #{pdf.values}"
357
416
  puts
358
417
 
359
418
  # Checking field type
360
- if pdf.field_type(:football) == Field::BUTTON
419
+ if pdf.field_type(:rugby) == Field::BUTTON
361
420
  puts "Field 'football' is of type BUTTON"
362
421
  else
363
422
  puts "Field 'football' is not of type BUTTON"
@@ -372,8 +431,8 @@ puts "Renamed field 'last_name' to 'surname'"
372
431
  puts
373
432
 
374
433
  # Removing field
375
- pdf.remove_field :nascar
376
- puts "Removed field 'nascar'"
434
+ pdf.remove_field :marketing
435
+ puts "Removed field 'marketing'"
377
436
 
378
437
  # saving the filled out PDF in another file
379
438
  pdf.save_as('output.pdf')
Binary file
Binary file
Binary file
Binary file
data/images/blank.png ADDED
Binary file
Binary file
Binary file
@@ -1,3 +1,3 @@
1
1
  class FillablePDF
2
- VERSION = '0.9.4'
2
+ VERSION = '0.9.5.1'
3
3
  end
data/lib/fillable-pdf.rb CHANGED
@@ -10,7 +10,7 @@ class FillablePDF # rubocop:disable Metrics/ClassLength
10
10
  #
11
11
  # @param [String|Symbol] file_path the name of the PDF file or file path
12
12
  #
13
- def initialize(file_path) # rubocop:disable Metrics/MethodLength
13
+ def initialize(file_path)
14
14
  raise IOError, "File <#{file_path}> is not found" unless File.exist?(file_path)
15
15
  @file_path = file_path
16
16
  begin
@@ -20,7 +20,6 @@ class FillablePDF # rubocop:disable Metrics/ClassLength
20
20
  @pdf_doc = ITEXT::PdfDocument.new @pdf_reader, @pdf_writer
21
21
  @pdf_form = ITEXT::PdfAcroForm.getAcroForm(@pdf_doc, true)
22
22
  @form_fields = @pdf_form.getFormFields
23
- @pdf_form.setGenerateAppearance false
24
23
  rescue StandardError => e
25
24
  raise "#{e.message} (Input file may be corrupt, incompatible, read-only, write-protected, encrypted, or may not have any form fields)" # rubocop:disable Layout/LineLength
26
25
  end
@@ -58,7 +57,7 @@ class FillablePDF # rubocop:disable Metrics/ClassLength
58
57
  end
59
58
 
60
59
  ##
61
- # Retrieves the numeric type of a field given its unique field name.
60
+ # Retrieves the string type of a field given its unique field name.
62
61
  #
63
62
  # @param [String|Symbol] key the field name
64
63
  #
@@ -88,9 +87,14 @@ class FillablePDF # rubocop:disable Metrics/ClassLength
88
87
  #
89
88
  # @param [String|Symbol] key the field name
90
89
  # @param [String|Symbol] value the field value
90
+ # @param [NilClass|TrueClass|FalseClass] generate_appearance true to generate appearance, false to let the PDF viewer application generate form field appearance, nil (default) to let iText decide what's appropriate
91
91
  #
92
- def set_field(key, value)
93
- pdf_field(key).setValue(value.to_s)
92
+ def set_field(key, value, generate_appearance: nil)
93
+ if generate_appearance.nil?
94
+ pdf_field(key).setValue(value.to_s)
95
+ else
96
+ pdf_field(key).setValue(value.to_s, generate_appearance)
97
+ end
94
98
  end
95
99
 
96
100
  ##
@@ -155,9 +159,10 @@ class FillablePDF # rubocop:disable Metrics/ClassLength
155
159
  # Sets the values of multiple fields given a set of unique field names and values.
156
160
  #
157
161
  # @param [Hash] fields the set of field names and values
162
+ # @param [NilClass|TrueClass|FalseClass] generate_appearance true to generate appearance, false to let the PDF viewer application generate form field appearance, nil (default) to let iText decide what's appropriate
158
163
  #
159
- def set_fields(fields)
160
- fields.each { |key, value| set_field key, value }
164
+ def set_fields(fields, generate_appearance: nil)
165
+ fields.each { |key, value| set_field key, value, generate_appearance: generate_appearance }
161
166
  end
162
167
 
163
168
  ##
@@ -218,7 +223,7 @@ class FillablePDF # rubocop:disable Metrics/ClassLength
218
223
  # Saves the filled out PDF document in a given path and flattens it if requested.
219
224
  #
220
225
  # @param [String] file_path the name of the PDF file or file path
221
- # @param [Hash] flatten: true if PDF should be flattened, false otherwise
226
+ # @param [TrueClass|FalseClass] flatten true if PDF should be flattened, false otherwise
222
227
  #
223
228
  def save_as(file_path, flatten: false)
224
229
  if @file_path == file_path
@@ -243,7 +248,7 @@ class FillablePDF # rubocop:disable Metrics/ClassLength
243
248
  ##
244
249
  # Writes the contents of the modified fields to the previously opened PDF file.
245
250
  #
246
- # @param [Hash] flatten: true if PDF should be flattened, false otherwise
251
+ # @param [TrueClass|FalseClass] flatten: true if PDF should be flattened, false otherwise
247
252
  #
248
253
  def finalize(flatten: false)
249
254
  @pdf_form.flattenFields if flatten
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fillable-pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.9.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vadim Kononov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-14 00:00:00.000000000 Z
11
+ date: 2022-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -145,24 +145,27 @@ executables: []
145
145
  extensions: []
146
146
  extra_rdoc_files: []
147
147
  files:
148
+ - ".github/workflows/test.yml"
148
149
  - ".gitignore"
149
150
  - ".rubocop.yml"
150
- - ".travis.yml"
151
151
  - Gemfile
152
152
  - LICENSE.md
153
153
  - README.md
154
154
  - Rakefile
155
155
  - bin/console
156
156
  - bin/setup
157
- - ext/commons-7.2.3.jar
158
- - ext/font-asian-7.2.3.jar
159
- - ext/forms-7.2.3.jar
160
- - ext/io-7.2.3.jar
161
- - ext/kernel-7.2.3.jar
162
- - ext/layout-7.2.3.jar
163
- - ext/slf4j-api-1.7.32.jar
164
- - ext/slf4j-simple-1.7.32.jar
157
+ - ext/commons-7.2.4.jar
158
+ - ext/font-asian-7.2.4.jar
159
+ - ext/forms-7.2.4.jar
160
+ - ext/io-7.2.4.jar
161
+ - ext/kernel-7.2.4.jar
162
+ - ext/layout-7.2.4.jar
163
+ - ext/slf4j-api-2.0.4.jar
164
+ - ext/slf4j-simple-2.0.4.jar
165
165
  - fillable-pdf.gemspec
166
+ - images/blank.png
167
+ - images/checked.png
168
+ - images/distinct.png
166
169
  - lib/field.rb
167
170
  - lib/fillable-pdf.rb
168
171
  - lib/fillable-pdf/itext.rb
data/.travis.yml DELETED
@@ -1,10 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 3.0.3
7
- jdk:
8
- - openjdk8
9
- before_install:
10
- - gem install bundler
Binary file
Binary file
Binary file