fillable-pdf 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +79 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/itextpdf-5.5.9.jar +0 -0
- data/fillable-pdf.gemspec +25 -0
- data/lib/fillable-pdf.rb +98 -0
- data/lib/fillable-pdf/version.rb +3 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7da9c03f8ee095a61b7a95a206be382fa4b5d1d5
|
4
|
+
data.tar.gz: 1fcfa10679b928734cf5628fe063e0c60b739e23
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0fd914fff82bc757c7c1a521db4c222a314f84cd5e5caf9e10b92286bd877e8b43202b841cb89819efb288be307ae6c8e8dcf5304d21f1f9bce4e60613272b8f
|
7
|
+
data.tar.gz: 51cde33bf6c621b50487249c30d12bcc1e8f9f1caeda458fd73292fea33f8b999c1d8c786d9bbde9e306cc09eda8368c130e948b7122508953d329bde5ef36e0
|
data/.gitignore
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,79 @@
|
|
1
|
+
# FillablePDF
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/fillable-pdf.svg)](https://rubygems.org/gems/fillable-pdf)
|
4
|
+
|
5
|
+
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.
|
6
|
+
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
gem 'fillable-pdf'
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
gem install fillable-pdf
|
21
|
+
|
22
|
+
If you are using this gem in a script, you need to require it manually:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'fillable-xml'
|
26
|
+
```
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
First of all, you should open a fillable PDF file:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
pdf = FillablePDF.new('input.pdf')
|
34
|
+
```
|
35
|
+
|
36
|
+
An instance of `FillablePDF` has the following methods at its disposal:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
pdf.get_field('full_name')
|
40
|
+
|
41
|
+
pdf.set_field('first_name', 'Richard')
|
42
|
+
|
43
|
+
pdf.set_fields({first_name: 'Richard', last_name: 'Rahl'})
|
44
|
+
```
|
45
|
+
|
46
|
+
Once the PDF is filled out you can either overwrite it or save it as another file:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
pdf.save
|
50
|
+
|
51
|
+
pdf.save_as(file)
|
52
|
+
```
|
53
|
+
|
54
|
+
Or if you prefer to flatten the file (i.e. make it non-editable), you can instead use:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
pdf.save(true)
|
58
|
+
|
59
|
+
pdf.save_as(file, true)
|
60
|
+
```
|
61
|
+
|
62
|
+
|
63
|
+
## Example
|
64
|
+
|
65
|
+
For a a fuller usage example of this gem, please see the contents of the `test` directory.
|
66
|
+
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
1. Fork it
|
71
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
72
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
73
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
74
|
+
5. Create new Pull Request
|
75
|
+
|
76
|
+
|
77
|
+
## License
|
78
|
+
|
79
|
+
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
Binary file
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fillable-pdf/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'fillable-pdf'
|
8
|
+
spec.version = FillablePDF::VERSION
|
9
|
+
spec.authors = ['Vadim Kononov']
|
10
|
+
spec.email = ['vadim@poetic.com']
|
11
|
+
|
12
|
+
spec.summary = 'Fill out or extract field values from simple fillable PDF forms using iText.'
|
13
|
+
spec.description = '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.'
|
14
|
+
spec.homepage = 'http://vkononov.github.io/fillable-pdf'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = 'exe'
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = %w(ext lib)
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
23
|
+
spec.add_development_dependency 'rake', '~> 11.2'
|
24
|
+
spec.add_runtime_dependency 'rjb', '~> 1.5'
|
25
|
+
end
|
data/lib/fillable-pdf.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'rjb'
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
# http://github.com/itext/itextpdf/releases/latest
|
5
|
+
Rjb::load(File.expand_path('../ext/itextpdf-5.5.9.jar', __dir__))
|
6
|
+
|
7
|
+
|
8
|
+
class FillablePDF
|
9
|
+
|
10
|
+
# required Java imports
|
11
|
+
BYTE_STREAM = Rjb::import('java.io.ByteArrayOutputStream')
|
12
|
+
FILE_READER = Rjb::import('com.itextpdf.text.pdf.PdfReader')
|
13
|
+
PDF_STAMPER = Rjb::import('com.itextpdf.text.pdf.PdfStamper')
|
14
|
+
|
15
|
+
|
16
|
+
##
|
17
|
+
# Opens a given fillable PDF file and prepares it for modification.
|
18
|
+
#
|
19
|
+
# @param [String] file the name of the PDF file or file path
|
20
|
+
#
|
21
|
+
def initialize(file)
|
22
|
+
@file = file
|
23
|
+
@byte_stream = BYTE_STREAM.new
|
24
|
+
@pdf_stamper = PDF_STAMPER.new FILE_READER.new(@file), @byte_stream
|
25
|
+
@form_fields = @pdf_stamper.getAcroFields
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
##
|
30
|
+
# Retrieves the value of a field given its unique field name.
|
31
|
+
#
|
32
|
+
# @param [String] key the field name
|
33
|
+
#
|
34
|
+
# @return the value of the field
|
35
|
+
#
|
36
|
+
def get_field(key)
|
37
|
+
@form_fields.getField key.to_s
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
##
|
42
|
+
# Sets the value of a field given its unique field name and value.
|
43
|
+
#
|
44
|
+
# @param [String] key the field name
|
45
|
+
# @param [String] value the field value
|
46
|
+
#
|
47
|
+
def set_field(key, value)
|
48
|
+
@form_fields.setField key.to_s, value.to_s
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
##
|
53
|
+
# Sets the values of multiple fields given a set of unique field names and values.
|
54
|
+
#
|
55
|
+
# @param [Hash] fields the set of field names and values
|
56
|
+
#
|
57
|
+
def set_fields(fields)
|
58
|
+
fields.each { |key, value| set_field key, value }
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
##
|
63
|
+
# Overwrites the previously opened PDF file and flattens it if requested.
|
64
|
+
#
|
65
|
+
# @param [bool] flatten true if PDF should be flattened, false otherwise
|
66
|
+
#
|
67
|
+
def save(flatten = false)
|
68
|
+
tmp_file = SecureRandom.uuid
|
69
|
+
save_as(tmp_file, flatten)
|
70
|
+
File.rename tmp_file, @file
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
##
|
75
|
+
# Saves the filled out PDF file with a given file and flattens it if requested.
|
76
|
+
#
|
77
|
+
# @param [String] file the name of the PDF file or file path
|
78
|
+
# @param [bool] flatten true if PDF should be flattened, false otherwise
|
79
|
+
#
|
80
|
+
def save_as(file, flatten = false)
|
81
|
+
File.open(file, 'wb') { |f| f.write finalize flatten and f.close }
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
##
|
88
|
+
# Writes the contents of the modified fields to the previously opened PDF file.
|
89
|
+
#
|
90
|
+
# @param [bool] flatten true if PDF should be flattened, false otherwise
|
91
|
+
#
|
92
|
+
def finalize(flatten)
|
93
|
+
@pdf_stamper.setFormFlattening flatten
|
94
|
+
@pdf_stamper.close
|
95
|
+
@byte_stream.toByteArray
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fillable-pdf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vadim Kononov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-16 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: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '11.2'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '11.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rjb
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
description: FillablePDF is an extremely simple and lightweight utility that bridges
|
56
|
+
iText and Ruby in order to fill out fillable PDF forms or extract field values from
|
57
|
+
previously filled out PDF forms.
|
58
|
+
email:
|
59
|
+
- vadim@poetic.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- ext/itextpdf-5.5.9.jar
|
72
|
+
- fillable-pdf.gemspec
|
73
|
+
- lib/fillable-pdf.rb
|
74
|
+
- lib/fillable-pdf/version.rb
|
75
|
+
homepage: http://vkononov.github.io/fillable-pdf
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- ext
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.6.6
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Fill out or extract field values from simple fillable PDF forms using iText.
|
100
|
+
test_files: []
|