gbdev-pdf_filler 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +84 -0
- data/Rakefile +4 -0
- data/init.rb +1 -0
- data/lib/iText-2.1.7.jar +0 -0
- data/lib/pdf_filler.rb +35 -0
- data/spec/lib/pdf_builder_spec.rb +60 -0
- data/spec/lib/pdf_collection_builder_spec.rb +12 -0
- data/spec/spec_helper.rb +62 -0
- data/spec/templates/certificate_template.pdf +0 -0
- data/tasks/documentation.rake +32 -0
- metadata +82 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Great Basin Solutions, LLC dba Great Basin Development
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
= pdf_filler
|
2
|
+
|
3
|
+
The <b>pdf_filler</b> Rails plugin makes it easy to update existing PDFs that have form fields defined.
|
4
|
+
Rather then building PDFs in Ruby using something like PDF-Writer, it is much easier to create a PDF in Acrobat,
|
5
|
+
define fields, and update those fields using pdf_filler. An example would be a First/Last name field on a PDF that is possible to update.
|
6
|
+
|
7
|
+
|
8
|
+
== Requirements
|
9
|
+
|
10
|
+
This plugin requires RJB (Ruby Java Bridge), so you will need to set your JAVA_HOME environment variable to point your installation of Java.
|
11
|
+
|
12
|
+
If your JAVA_HOME environment variable is not setup correctly you will get something like:
|
13
|
+
|
14
|
+
|
15
|
+
Building native extensions. This could take a while...
|
16
|
+
ERROR: Error installing pdf_filler-0.1.0.gem:
|
17
|
+
ERROR: Failed to build gem native extension.
|
18
|
+
|
19
|
+
/opt/local/bin/ruby extconf.rb
|
20
|
+
*** extconf.rb failed ***
|
21
|
+
Could not create Makefile due to some reason, probably lack of
|
22
|
+
necessary libraries and/or headers. Check the mkmf.log file for more
|
23
|
+
details. You may need configuration options.
|
24
|
+
|
25
|
+
Provided configuration options:
|
26
|
+
--with-opt-dir
|
27
|
+
--without-opt-dir
|
28
|
+
--with-opt-include
|
29
|
+
--without-opt-include=${opt-dir}/include
|
30
|
+
--with-opt-lib
|
31
|
+
--without-opt-lib=${opt-dir}/lib
|
32
|
+
--with-make-prog
|
33
|
+
--without-make-prog
|
34
|
+
--srcdir=.
|
35
|
+
--curdir
|
36
|
+
--ruby=/opt/local/bin/ruby
|
37
|
+
extconf.rb:45: JAVA_HOME is not set. (RuntimeError)
|
38
|
+
|
39
|
+
Gem files will remain installed in /opt/local/lib/ruby/gems/1.8/gems/rjb-1.1.7 for inspection.
|
40
|
+
Results logged to /opt/local/lib/ruby/gems/1.8/gems/rjb-1.1.7/ext/gem_make.out
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
<b>On my Mac I set JAVA_HOME to be "/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Home".</b>
|
45
|
+
|
46
|
+
|
47
|
+
== Installing
|
48
|
+
|
49
|
+
The recommended method to enable pdf_filler in your project is adding the pdf_filler gem to your environment. Add the following code to your Rails configuration in <tt>config/environment.rb</tt>:
|
50
|
+
|
51
|
+
Rails::Initializer.run do |config|
|
52
|
+
...
|
53
|
+
config.gem 'gbdev-pdf_filler', :lib => 'pdf_filler', source => 'http://gems.github.com/'
|
54
|
+
end
|
55
|
+
|
56
|
+
Run <tt>sudo rake gems:install</tt> to install the gem.
|
57
|
+
|
58
|
+
Another alternative is to install pdf_filler as a Rails plugin:
|
59
|
+
|
60
|
+
script/plugin install git://github.com/gbdev/pdf_filler.git
|
61
|
+
|
62
|
+
|
63
|
+
== Usage
|
64
|
+
|
65
|
+
1. You will need a PDF that has form fields to fill.
|
66
|
+
|
67
|
+
2. A hash of parameters for filling the PDF form. The hash must contain all of the required options:
|
68
|
+
* :data (required) - A hash or array of hashes of fields to fill the pdf template with. Example: [{:field => 'value'}, {:field => 'value'}].
|
69
|
+
* :template (required) - The full path to the PDF form to fill.
|
70
|
+
* :dir (required) - The directory to write filled form to.
|
71
|
+
* :file_name (optional) The name of the output file to create. If not specified a random file name will be used.
|
72
|
+
|
73
|
+
3. Example
|
74
|
+
opts = {:data => {:full_name => 'Wes Hays'},
|
75
|
+
:template => '/path/to/templates/certificate_template.pdf'),
|
76
|
+
:dir => '/path/to/templates/output/'),
|
77
|
+
:file_name => 'wes_hays.pdf'}
|
78
|
+
PdfFiller(opts)
|
79
|
+
|
80
|
+
|
81
|
+
== License
|
82
|
+
|
83
|
+
This plugin is released under the MIT license. Please contact weshays (http://github.com/weshays)
|
84
|
+
or djohnson (http://github.com/djohnson) for any questions.
|
data/Rakefile
ADDED
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'form_filler'
|
data/lib/iText-2.1.7.jar
ADDED
Binary file
|
data/lib/pdf_filler.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'rjb'
|
3
|
+
|
4
|
+
load_path = File.expand_path(File.dirname(__FILE__) + '/iText-2.1.7.jar')
|
5
|
+
options = ['-Djava.awt.headless=true']
|
6
|
+
Rjb::load load_path, options
|
7
|
+
|
8
|
+
FileOutputStream = Rjb::import('java.io.FileOutputStream')
|
9
|
+
PdfWriter = Rjb::import('com.lowagie.text.pdf.PdfWriter')
|
10
|
+
PdfReader = Rjb::import('com.lowagie.text.pdf.PdfReader')
|
11
|
+
PdfCopy = Rjb::import('com.lowagie.text.pdf.PdfCopy')
|
12
|
+
PdfImportedPage = Rjb::import('com.lowagie.text.pdf.PdfImportedPage')
|
13
|
+
Document = Rjb::import('com.lowagie.text.Document')
|
14
|
+
Paragraph = Rjb::import('com.lowagie.text.Paragraph')
|
15
|
+
AcroFields = Rjb::import('com.lowagie.text.pdf.AcroFields')
|
16
|
+
PdfStamper = Rjb::import('com.lowagie.text.pdf.PdfStamper')
|
17
|
+
HashMap = Rjb::import('java.util.HashMap')
|
18
|
+
|
19
|
+
require 'pdf_filler/util_methods'
|
20
|
+
require 'pdf_filler/fill_pdf_template'
|
21
|
+
require 'pdf_filler/fill_pdf_collection'
|
22
|
+
|
23
|
+
|
24
|
+
module Kernel
|
25
|
+
|
26
|
+
def PdfFiller(pdf_options = {})
|
27
|
+
if(pdf_options.has_key?(:data) and pdf_options[:data].class.to_s == 'Array')
|
28
|
+
p = Gbdev::PdfOperations::FillCollection.new(pdf_options)
|
29
|
+
else
|
30
|
+
p = Gbdev::PdfOperations::FillTemplate.new(pdf_options)
|
31
|
+
end
|
32
|
+
p.compile_pdf
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
2
|
+
|
3
|
+
describe 'PdfFiller' do
|
4
|
+
it 'should be initialized without errors' do
|
5
|
+
opts = {:data => {:full_name => 'Wes Hays'},
|
6
|
+
:template => File.expand_path(File.dirname(__FILE__) + '/../templates/certificate_template.pdf'),
|
7
|
+
:dir => File.expand_path(File.dirname(__FILE__) + '/../output/')}
|
8
|
+
ff = PdfFiller(opts)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should be use the output filename specified' do
|
12
|
+
opts = {:data => {:full_name => 'Wes Hays'},
|
13
|
+
:template => File.expand_path(File.dirname(__FILE__) + '/../templates/certificate_template.pdf'),
|
14
|
+
:dir => File.expand_path(File.dirname(__FILE__) + '/../output/'),
|
15
|
+
:file_name => 'wes_hays.pdf'}
|
16
|
+
ff = PdfFiller(opts)
|
17
|
+
File.exist?([opts[:dir], opts[:file_name]].join('/')).should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should raise exception if options[:data] not set' do
|
21
|
+
opts = {:template => File.expand_path(File.dirname(__FILE__) + '/../templates/certificate_template.pdf'),
|
22
|
+
:dir => File.expand_path(File.dirname(__FILE__) + '/../output/')}
|
23
|
+
doing {PdfFiller(opts)}.should raise_error('Option parameters data, template, dir are required')
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should raise exception if options[:template] not set' do
|
27
|
+
opts = {:data => {:full_name => 'Wes Hays'},
|
28
|
+
:dir => File.expand_path(File.dirname(__FILE__) + '/../output/')}
|
29
|
+
doing {PdfFiller(opts)}.should raise_error('Option parameters data, template, dir are required')
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should raise exception if options[:dir] not set' do
|
33
|
+
opts = {:data => {:full_name => 'Wes Hays'},
|
34
|
+
:template => File.expand_path(File.dirname(__FILE__) + '/../templates/certificate_template.pdf')}
|
35
|
+
doing {PdfFiller(opts)}.should raise_error('Option parameters data, template, dir are required')
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
it 'should raise exception if options[:data] is not a hash or an array of hashes' do
|
40
|
+
opts = {:data => 'dog',
|
41
|
+
:template => File.expand_path(File.dirname(__FILE__) + '/../templates/certificate_template.pdf'),
|
42
|
+
:dir => File.expand_path(File.dirname(__FILE__) + '/../output/')}
|
43
|
+
doing {PdfFiller(opts)}.should raise_error('Data must be a hash')
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should raise exception if certificate template does not exist' do
|
47
|
+
opts = {:data => {:full_name => 'Wes Hays'},
|
48
|
+
:template => 'dog/templates/certificate_template.pdf',
|
49
|
+
:dir => File.expand_path(File.dirname(__FILE__) + '/../output/')}
|
50
|
+
doing {PdfFiller(opts)}.should raise_error('Template file does not exist or path is wrong')
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'should raise exception if options[:dir] is not a directory' do
|
54
|
+
opts = {:data => {:full_name => 'Wes Hays'},
|
55
|
+
:template => File.expand_path(File.dirname(__FILE__) + '/../templates/certificate_template.pdf'),
|
56
|
+
:dir => '/../output/'}
|
57
|
+
doing {PdfFiller(opts)}.should raise_error('Output directory does not exist or path is wrong')
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
|
2
|
+
|
3
|
+
describe 'FormCollection' do
|
4
|
+
it 'should be initialized without errors' do
|
5
|
+
opts = {:data => [{:full_name => 'Wes Hays'}, {:full_name => 'Darren Johnson'}],
|
6
|
+
:template => File.expand_path(File.dirname(__FILE__) + '/../templates/certificate_template.pdf'),
|
7
|
+
:dir => File.expand_path(File.dirname(__FILE__) + '/../output/'),
|
8
|
+
:file_name => 'people.pdf'}
|
9
|
+
ff = PdfFiller(opts)
|
10
|
+
File.exist?([opts[:dir], opts[:file_name]].join('/')).should be_true
|
11
|
+
end
|
12
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'spec'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'ruby-debug'
|
4
|
+
require 'activerecord'
|
5
|
+
|
6
|
+
##################
|
7
|
+
#### <CUSTOM> ####
|
8
|
+
##################
|
9
|
+
# Aliased "lambda" with "doing" so that when checking
|
10
|
+
# whether or not something raises an exception it will
|
11
|
+
# read like other rspec operations. For example:
|
12
|
+
# instead of
|
13
|
+
# lambda { ... }.should_not raise_error
|
14
|
+
# you can have
|
15
|
+
# doing { ... }.should_not raise_error
|
16
|
+
alias :doing :lambda
|
17
|
+
###################
|
18
|
+
#### </CUSTOM> ####
|
19
|
+
###################
|
20
|
+
|
21
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
22
|
+
ActiveRecord::Base.configurations = true
|
23
|
+
|
24
|
+
ActiveRecord::Schema.verbose = false
|
25
|
+
ActiveRecord::Schema.define(:version => 1) do
|
26
|
+
create_table :clients do |t|
|
27
|
+
t.string :first_name
|
28
|
+
t.string :last_name
|
29
|
+
end
|
30
|
+
|
31
|
+
create_table :addresses do |t|
|
32
|
+
t.string :address1
|
33
|
+
t.string :address2
|
34
|
+
t.string :city
|
35
|
+
t.string :state
|
36
|
+
t.string :postal_code
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
41
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
42
|
+
require 'pdf_filler'
|
43
|
+
|
44
|
+
Spec::Runner.configure do |config|
|
45
|
+
config.before(:each) do
|
46
|
+
class Client < ActiveRecord::Base
|
47
|
+
has_many :addresses, :dependent => :destroy
|
48
|
+
end
|
49
|
+
|
50
|
+
class Address < ActiveRecord::Base
|
51
|
+
belongs_to :client
|
52
|
+
end
|
53
|
+
|
54
|
+
Client.destroy_all
|
55
|
+
Address.destroy_all
|
56
|
+
end
|
57
|
+
|
58
|
+
config.after(:each) do
|
59
|
+
Object.send(:remove_const, :Client)
|
60
|
+
Object.send(:remove_const, :Address)
|
61
|
+
end
|
62
|
+
end
|
Binary file
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
|
4
|
+
desc 'Generate documentation for PDF-Filler.'
|
5
|
+
Rake::RDocTask.new do |rdoc|
|
6
|
+
rdoc.rdoc_dir = 'doc/html'
|
7
|
+
rdoc.title = 'scoped_search'
|
8
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
9
|
+
rdoc.main = 'README.rdoc'
|
10
|
+
rdoc.rdoc_files.include('LICENSE', 'lib/')
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
begin
|
15
|
+
require 'rcov/rcovtask'
|
16
|
+
Rcov::RcovTask.new do |t|
|
17
|
+
t.test_files = Dir[ "spec/**/*_spec.rb" ]
|
18
|
+
end
|
19
|
+
rescue LoadError
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
|
23
|
+
begin
|
24
|
+
require 'rcov/rcovtask'
|
25
|
+
desc 'Runs spec:rcov and then displays the coverage/index.html file in the browser.'
|
26
|
+
task :rcov_display => [:clobber_rcov, :rcov] do
|
27
|
+
system("open coverage/index.html")
|
28
|
+
end
|
29
|
+
rescue LoadError
|
30
|
+
nil
|
31
|
+
end
|
32
|
+
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gbdev-pdf_filler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Wes Hays
|
8
|
+
- Darrne Johnson
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-07-24 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rjb
|
18
|
+
type: :runtime
|
19
|
+
version_requirement:
|
20
|
+
version_requirements: !ruby/object:Gem::Requirement
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: 1.1.7
|
25
|
+
version:
|
26
|
+
description: PDF Filler makes it easy to file your PDFs.
|
27
|
+
email:
|
28
|
+
- weshays@gbdev.com
|
29
|
+
- djohnson@gbdev.com
|
30
|
+
executables: []
|
31
|
+
|
32
|
+
extensions: []
|
33
|
+
|
34
|
+
extra_rdoc_files:
|
35
|
+
- README.rdoc
|
36
|
+
files:
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- init.rb
|
41
|
+
- lib/iText-2.1.7.jar
|
42
|
+
- lib/pdf_filler.rb
|
43
|
+
- spec/lib/pdf_builder_spec.rb
|
44
|
+
- spec/lib/pdf_collection_builder_spec.rb
|
45
|
+
- spec/spec_helper.rb
|
46
|
+
- spec/templates/certificate_template.pdf
|
47
|
+
- tasks/documentation.rake
|
48
|
+
has_rdoc: false
|
49
|
+
homepage: http://wiki.github.com/gbdev/pdf-filler
|
50
|
+
post_install_message:
|
51
|
+
rdoc_options:
|
52
|
+
- --charset=UTF-8
|
53
|
+
- --title
|
54
|
+
- pdf_filler
|
55
|
+
- --main
|
56
|
+
- README.rdoc
|
57
|
+
- --line-numbers
|
58
|
+
- --inline-source
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: "0"
|
66
|
+
version:
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: "0"
|
72
|
+
version:
|
73
|
+
requirements: []
|
74
|
+
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 1.2.0
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: A Rails plugin to fill a PDF form using RJB and iText.
|
80
|
+
test_files:
|
81
|
+
- spec/lib/pdf_builder_spec.rb
|
82
|
+
- spec/lib/pdf_collection_builder_spec.rb
|