gbdev-pdf_filler 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pdf_filler/fill_pdf_collection.rb +79 -0
- data/lib/pdf_filler/fill_pdf_template.rb +65 -0
- data/lib/pdf_filler/util_methods.rb +12 -0
- data/tasks/build_gem.rake +58 -0
- metadata +5 -1
@@ -0,0 +1,79 @@
|
|
1
|
+
module Gbdev
|
2
|
+
|
3
|
+
module PdfOperations
|
4
|
+
|
5
|
+
class FillCollection
|
6
|
+
|
7
|
+
# Fill a PDF form based on the options specified.
|
8
|
+
#
|
9
|
+
# Hash pdf_options : A hash of parameters for filling the PDF form.
|
10
|
+
# [data] An array of hashes of fields to fill the pdf template with. Example: [{:field => 'value'}, {:field => 'value'}].
|
11
|
+
# [template] The full path to the PDF form to fill.
|
12
|
+
# [dir] The directory to write filled form to.
|
13
|
+
# [file_name] (optional) The name of the output file to create. If not specified a random
|
14
|
+
# file name will be used.
|
15
|
+
#
|
16
|
+
# Exceptons :
|
17
|
+
# 1) If any of the required hash options from pdf_options is missing.
|
18
|
+
def initialize(pdf_options = {})
|
19
|
+
if !pdf_options.has_key?(:data) or !pdf_options.has_key?(:template) or !pdf_options.has_key?(:dir)
|
20
|
+
raise 'All options parameters (data, template, dir) are required'
|
21
|
+
end
|
22
|
+
|
23
|
+
unless File.exist?(pdf_options[:template])
|
24
|
+
raise 'Template file does not exist or path is wrong'
|
25
|
+
end
|
26
|
+
|
27
|
+
if pdf_options[:data].class.to_s != 'Array' or
|
28
|
+
(pdf_options[:data].class.to_s == 'Array' and pdf_options[:data][0].class.to_s != 'Hash')
|
29
|
+
raise 'Data must array of hashes'
|
30
|
+
end
|
31
|
+
|
32
|
+
unless File.directory?(pdf_options[:dir])
|
33
|
+
raise 'Output directory does not exist or path is wrong'
|
34
|
+
end
|
35
|
+
|
36
|
+
@data = pdf_options[:data]
|
37
|
+
@template = pdf_options[:template]
|
38
|
+
@dir = pdf_options[:dir]
|
39
|
+
@file_name = pdf_options.has_key?(:file_name) ? pdf_options[:file_name] : build_random_file_name
|
40
|
+
end
|
41
|
+
|
42
|
+
def compile_pdf
|
43
|
+
temp_dir = [@dir, 'collection_temp'].join('/')
|
44
|
+
Dir.mkdir(temp_dir)
|
45
|
+
@data.each do |hash_data|
|
46
|
+
opts = {:data => hash_data,
|
47
|
+
:template => @template,
|
48
|
+
:dir => temp_dir}
|
49
|
+
p = Gbdev::PdfOperations::FillTemplate.new(opts)
|
50
|
+
p.compile_pdf
|
51
|
+
end
|
52
|
+
|
53
|
+
temp_files = Dir[[temp_dir,'*'].join('/')]
|
54
|
+
|
55
|
+
document = Document.new
|
56
|
+
copier = PdfCopy.new(document, FileOutputStream.new([@dir,@file_name].join('/')))
|
57
|
+
|
58
|
+
document.open
|
59
|
+
temp_files.each do |read_target|
|
60
|
+
reader = PdfReader.new(read_target)
|
61
|
+
n_pages = reader.getNumberOfPages
|
62
|
+
n_pages.times do |i|
|
63
|
+
copier.addPage( copier.getImportedPage(reader, i+1)) if copier
|
64
|
+
end
|
65
|
+
end
|
66
|
+
document.close
|
67
|
+
|
68
|
+
FileUtils.rm_rf(temp_dir, {:secure => true})
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
|
73
|
+
include Gbdev::Utils::PrivateMethods
|
74
|
+
|
75
|
+
end # End FillCollection
|
76
|
+
|
77
|
+
end # End PdfOperations
|
78
|
+
|
79
|
+
end # End Gbdev
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Gbdev
|
2
|
+
|
3
|
+
module PdfOperations
|
4
|
+
|
5
|
+
class FillTemplate
|
6
|
+
|
7
|
+
# Fills a PDF form based on the options specified.
|
8
|
+
#
|
9
|
+
# Hash pdf_options : A hash of parameters for filling the PDF form.
|
10
|
+
# [data] Hash of fields to fill and what to fill them with. Example: {:field => 'value'}.
|
11
|
+
# [template] The full path to the PDF form to fill.
|
12
|
+
# [dir] The directory to write filled form to.
|
13
|
+
# [file_name] (optional) The name of the output file to create. If not specified a random
|
14
|
+
# file name will be used.
|
15
|
+
#
|
16
|
+
# Exceptons :
|
17
|
+
# 1) If any of the required hash options from pdf_options is missing.
|
18
|
+
def initialize(pdf_options = {})
|
19
|
+
if !pdf_options.has_key?(:data) or !pdf_options.has_key?(:template) or !pdf_options.has_key?(:dir)
|
20
|
+
raise 'Option parameters data, template, dir are required'
|
21
|
+
end
|
22
|
+
|
23
|
+
unless File.exist?(pdf_options[:template])
|
24
|
+
raise 'Template file does not exist or path is wrong'
|
25
|
+
end
|
26
|
+
|
27
|
+
if ['Hash'].index(pdf_options[:data].class.to_s).nil?
|
28
|
+
raise 'Data must be a hash'
|
29
|
+
end
|
30
|
+
|
31
|
+
unless File.directory?(pdf_options[:dir])
|
32
|
+
raise 'Output directory does not exist or path is wrong'
|
33
|
+
end
|
34
|
+
|
35
|
+
@data = pdf_options[:data]
|
36
|
+
@template = pdf_options[:template]
|
37
|
+
@dir = pdf_options[:dir]
|
38
|
+
@file_name = pdf_options.has_key?(:file_name) ? pdf_options[:file_name] : build_random_file_name
|
39
|
+
end
|
40
|
+
|
41
|
+
# Compiles the new PDF from the template and data given
|
42
|
+
def compile_pdf
|
43
|
+
field_cache = HashMap.new
|
44
|
+
reader = PdfReader.new(@template)
|
45
|
+
stamper = PdfStamper.new( reader, FileOutputStream.new([@dir, @file_name].join('/')) )
|
46
|
+
form = stamper.getAcroFields()
|
47
|
+
form.setFieldCache(field_cache)
|
48
|
+
|
49
|
+
@data.each do |field,value|
|
50
|
+
form.setField(field.to_s, value.to_s)
|
51
|
+
end
|
52
|
+
|
53
|
+
stamper.setFormFlattening(true)
|
54
|
+
stamper.close
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
include Gbdev::Utils::PrivateMethods
|
60
|
+
|
61
|
+
end # End FillTemplate
|
62
|
+
|
63
|
+
end # End PdfOperations
|
64
|
+
|
65
|
+
end # End Gbdev
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Gbdev
|
2
|
+
module Utils
|
3
|
+
module PrivateMethods
|
4
|
+
|
5
|
+
def build_random_file_name # :nodoc:
|
6
|
+
letters_array = [('a'..'z'),('A'..'Z')].collect{|i| i.to_a}.flatten
|
7
|
+
(1..10).collect{ letters_array[ rand(letters_array.length) ] }.join << '.pdf'
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = 'pdf_filler'
|
8
|
+
gem.version = '0.0.2'
|
9
|
+
gem.date = '2009-07-24'
|
10
|
+
|
11
|
+
gem.summary = "A Rails plugin to fill a PDF form using RJB and iText."
|
12
|
+
gem.description = "PDF Filler makes it easy to file your PDFs."
|
13
|
+
|
14
|
+
gem.authors = ['Wes Hays', 'Darrne Johnson']
|
15
|
+
gem.email = ['weshays@gbdev.com', 'djohnson@gbdev.com']
|
16
|
+
gem.homepage = 'http://wiki.github.com/gbdev/pdf-filler'
|
17
|
+
|
18
|
+
gem.has_rdoc = true
|
19
|
+
gem.rdoc_options << '--title' << gem.name << '--main' << 'README.rdoc' << '--line-numbers' << '--inline-source'
|
20
|
+
gem.extra_rdoc_files = ['README.rdoc']
|
21
|
+
|
22
|
+
gem.files = ['LICENSE',
|
23
|
+
'README.rdoc',
|
24
|
+
'Rakefile',
|
25
|
+
'init.rb',
|
26
|
+
'lib/iText-2.1.7.jar',
|
27
|
+
'lib/pdf_filler.rb',
|
28
|
+
'lib/pdf_filler/fill_pdf_collection.rb',
|
29
|
+
'lib/pdf_filler/fill_pdf_template.rb',
|
30
|
+
'lib/pdf_filler/util_methods.rb',
|
31
|
+
'tasks/documentation.rake',
|
32
|
+
'tasks/build_gem.rake',
|
33
|
+
'spec/spec_helper.rb',
|
34
|
+
'spec/lib/pdf_builder_spec.rb',
|
35
|
+
'spec/lib/pdf_collection_builder_spec.rb',
|
36
|
+
'spec/output',
|
37
|
+
'spec/templates/certificate_template.pdf']
|
38
|
+
|
39
|
+
gem.test_files = ['spec/lib/pdf_builder_spec.rb',
|
40
|
+
'spec/lib/pdf_collection_builder_spec.rb']
|
41
|
+
|
42
|
+
gem.add_dependency 'rjb', '>= 1.1.7'
|
43
|
+
end
|
44
|
+
rescue LoadError
|
45
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
46
|
+
end
|
47
|
+
|
48
|
+
require 'spec/rake/spectask'
|
49
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
50
|
+
spec.libs << 'lib' << 'spec'
|
51
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
52
|
+
end
|
53
|
+
|
54
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
55
|
+
spec.libs << 'lib' << 'spec'
|
56
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
57
|
+
spec.rcov = true
|
58
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gbdev-pdf_filler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wes Hays
|
@@ -40,10 +40,14 @@ files:
|
|
40
40
|
- init.rb
|
41
41
|
- lib/iText-2.1.7.jar
|
42
42
|
- lib/pdf_filler.rb
|
43
|
+
- lib/pdf_filler/fill_pdf_collection.rb
|
44
|
+
- lib/pdf_filler/fill_pdf_template.rb
|
45
|
+
- lib/pdf_filler/util_methods.rb
|
43
46
|
- spec/lib/pdf_builder_spec.rb
|
44
47
|
- spec/lib/pdf_collection_builder_spec.rb
|
45
48
|
- spec/spec_helper.rb
|
46
49
|
- spec/templates/certificate_template.pdf
|
50
|
+
- tasks/build_gem.rake
|
47
51
|
- tasks/documentation.rake
|
48
52
|
has_rdoc: false
|
49
53
|
homepage: http://wiki.github.com/gbdev/pdf-filler
|