gbdev-pdf_filler 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,19 +17,18 @@ PdfStamper = Rjb::import('com.lowagie.text.pdf.PdfStamper')
17
17
  HashMap = Rjb::import('java.util.HashMap')
18
18
 
19
19
  require 'pdf_filler/util_methods'
20
- require 'pdf_filler/fill_pdf_template'
21
- require 'pdf_filler/fill_pdf_collection'
20
+ require 'pdf_filler/page'
21
+ require 'pdf_filler/book'
22
22
 
23
23
 
24
24
  module Kernel
25
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
26
+ def PDFPage(template)
27
+ GBDev::PDF::Page.new(template)
33
28
  end
34
29
 
30
+ def PDFBook()
31
+ GBDev::PDF::Book.new
32
+ end
33
+
35
34
  end
@@ -0,0 +1,59 @@
1
+ module GBDev
2
+
3
+ module PDF
4
+
5
+ # A book that represents collection of PDF page.
6
+ class Book
7
+
8
+ def initialize()
9
+ @pages = []
10
+ end
11
+
12
+ # Add a page to the book
13
+ def add_page(new_page)
14
+ @pages << new_page
15
+ end
16
+
17
+ # Renders the book with the given pages and saves to the given filename path.
18
+ #
19
+ # String filename : A path to the file to be created.
20
+ def save_to(filename)
21
+ dir = File.dirname(filename)
22
+ temp_dir = [dir, "collection_temp_#{build_random_string}"].join('/')
23
+ Dir.mkdir(temp_dir)
24
+ @pages.each_with_index do |page, indx|
25
+ page.save_to([temp_dir, "#{indx}_#{build_random_file_name}"].join('/'))
26
+ end
27
+ temp_files = Dir[[temp_dir,'*'].join('/')].sort
28
+
29
+ document = Document.new
30
+ copier = PdfCopy.new(document, FileOutputStream.new(filename))
31
+
32
+ document.open
33
+ temp_files.each do |read_target|
34
+ reader = PdfReader.new(read_target)
35
+ n_pages = reader.getNumberOfPages
36
+ n_pages.times do |i|
37
+ copier.addPage( copier.getImportedPage(reader, i+1)) if copier
38
+ end
39
+ end
40
+ document.close
41
+
42
+ FileUtils.rm_rf(temp_dir, {:secure => true})
43
+ end
44
+
45
+ def display
46
+ end
47
+
48
+ def save_and_display(filename)
49
+ save(filename)
50
+ display
51
+ end
52
+
53
+ include Gbdev::Utils::PrivateMethods
54
+
55
+ end # End Filler
56
+
57
+ end # End PDF
58
+
59
+ end # End GBDev
@@ -0,0 +1,69 @@
1
+ module GBDev
2
+
3
+ module PDF
4
+
5
+ # A page that represents a PDF page.
6
+ class Page
7
+
8
+ # String template : A path to the template file
9
+ def initialize(template)
10
+ @template = template
11
+ @fields = {}
12
+ @images = {}
13
+ end
14
+
15
+ # Sets a known text field in the PDF template.
16
+ #
17
+ # key : A known field in the PDF.
18
+ # value : The value to apply to the know field.
19
+ def set_text(key, value)
20
+ @fields[key] = value
21
+ end
22
+
23
+ # Alias for set_text method
24
+ alias :text :set_text
25
+
26
+ # Sets a know image area in the PDF template.
27
+ #
28
+ # key : A known field in the PDF.
29
+ # value : The image to apply to the know image area.
30
+ def set_image(key, value)
31
+ @images[key] = value
32
+ end
33
+
34
+ # Alias for the set_image method
35
+ alias :image :set_image
36
+
37
+ # Renders the template with the given data and saves to the given filename path.
38
+ #
39
+ # String filename : A path to the file to be created.
40
+ def save_to(filename)
41
+ field_cache = HashMap.new
42
+ reader = PdfReader.new(@template)
43
+ stamper = PdfStamper.new( reader, FileOutputStream.new(filename) )
44
+ form = stamper.getAcroFields()
45
+ form.setFieldCache(field_cache)
46
+
47
+ @fields.each do |field, value|
48
+ form.setField(field.to_s, value.to_s)
49
+ end
50
+
51
+ stamper.setFormFlattening(true)
52
+ stamper.close
53
+ end
54
+
55
+ def display
56
+ end
57
+
58
+ def save_and_display(filename)
59
+ save(filename)
60
+ display
61
+ end
62
+
63
+ include Gbdev::Utils::PrivateMethods
64
+
65
+ end # End Page
66
+
67
+ end # End PDF
68
+
69
+ end # End GBDev
@@ -1,10 +1,15 @@
1
1
  module Gbdev
2
2
  module Utils
3
+
3
4
  module PrivateMethods
4
5
 
5
- def build_random_file_name # :nodoc:
6
+ def build_random_string # :nodoc:
6
7
  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
+ (1..10).collect{ letters_array[ rand(letters_array.length) ] }.join
9
+ end
10
+
11
+ def build_random_file_name # :nodoc:
12
+ build_random_string << '.pdf'
8
13
  end
9
14
 
10
15
  end
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe 'Book' do
4
+
5
+ it 'should create the book of pages and write to the output directory' do
6
+ book = GBDev::PDF::Book.new
7
+
8
+ template_file = File.expand_path(File.dirname(__FILE__) + '/../templates/certificate_template.pdf')
9
+ pdf_book = File.expand_path(File.dirname(__FILE__) + '/../output/certs.pdf')
10
+
11
+ page1 = GBDev::PDF::Page.new(template_file)
12
+ page1.set_text(:full_name, 'Wes Hays')
13
+
14
+ page2 = GBDev::PDF::Page.new(template_file)
15
+ page2.set_text(:full_name, 'Darren Johnson')
16
+
17
+ page3 = GBDev::PDF::Page.new(template_file)
18
+ page3.set_text(:full_name, 'John Dell')
19
+
20
+ book.add_page(page1)
21
+ book.add_page(page2)
22
+ book.add_page(page3)
23
+
24
+ book.save_to(pdf_book)
25
+
26
+ File.exist?(pdf_book).should be_true
27
+ end
28
+
29
+ it 'should create the book of pages and write to the output directory with Kernel method' do
30
+ book = PDFBook()
31
+
32
+ template_file = File.expand_path(File.dirname(__FILE__) + '/../templates/certificate_template.pdf')
33
+ pdf_book = File.expand_path(File.dirname(__FILE__) + '/../output/certs.pdf')
34
+
35
+ page1 = GBDev::PDF::Page.new(template_file)
36
+ page1.set_text(:full_name, 'Wes Hays')
37
+
38
+ page2 = GBDev::PDF::Page.new(template_file)
39
+ page2.set_text(:full_name, 'Darren Johnson')
40
+
41
+ page3 = GBDev::PDF::Page.new(template_file)
42
+ page3.set_text(:full_name, 'John Dell')
43
+
44
+ book.add_page(page1)
45
+ book.add_page(page2)
46
+ book.add_page(page3)
47
+
48
+ book.save_to(pdf_book)
49
+
50
+ File.exist?(pdf_book).should be_true
51
+ end
52
+
53
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe 'Page' do
4
+
5
+ it 'should create the page and write to the output directory' do
6
+ page = GBDev::PDF::Page.new(File.expand_path(File.dirname(__FILE__) + '/../templates/certificate_template.pdf'))
7
+ page.set_text(:full_name, 'Wes Hays')
8
+ page.save_to(File.expand_path(File.dirname(__FILE__) + '/../output/wes_hays.pdf'))
9
+ File.exist?(File.dirname(__FILE__) + '/../output/wes_hays.pdf').should be_true
10
+ end
11
+
12
+ it 'should create the page and write to the output directory with Kernel method' do
13
+ page = PDFPage(File.expand_path(File.dirname(__FILE__) + '/../templates/certificate_template.pdf'))
14
+ page.set_text(:full_name, 'Wes Hays')
15
+ page.save_to(File.expand_path(File.dirname(__FILE__) + '/../output/wes_hays.pdf'))
16
+ File.exist?(File.dirname(__FILE__) + '/../output/wes_hays.pdf').should be_true
17
+ end
18
+
19
+ end
@@ -25,19 +25,19 @@ begin
25
25
  'init.rb',
26
26
  'lib/iText-2.1.7.jar',
27
27
  'lib/pdf_filler.rb',
28
- 'lib/pdf_filler/fill_pdf_collection.rb',
29
- 'lib/pdf_filler/fill_pdf_template.rb',
28
+ 'lib/pdf_filler/page.rb',
29
+ 'lib/pdf_filler/book.rb',
30
30
  'lib/pdf_filler/util_methods.rb',
31
31
  'tasks/documentation.rake',
32
32
  'tasks/build_gem.rake',
33
33
  'spec/spec_helper.rb',
34
- 'spec/lib/pdf_builder_spec.rb',
35
- 'spec/lib/pdf_collection_builder_spec.rb',
34
+ 'spec/lib/pdf_page_spec.rb',
35
+ 'spec/lib/pdf_book_spec.rb',
36
36
  'spec/output',
37
37
  'spec/templates/certificate_template.pdf']
38
38
 
39
- gem.test_files = ['spec/lib/pdf_builder_spec.rb',
40
- 'spec/lib/pdf_collection_builder_spec.rb']
39
+ gem.test_files = ['spec/lib/pdf_page_spec.rb',
40
+ 'spec/lib/pdf_book_spec.rb']
41
41
 
42
42
  gem.add_dependency 'rjb', '>= 1.1.7'
43
43
  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.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wes Hays
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-07-24 00:00:00 -07:00
13
+ date: 2009-08-01 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -40,17 +40,18 @@ 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
43
+ - lib/pdf_filler/book.rb
44
+ - lib/pdf_filler/page.rb
45
45
  - lib/pdf_filler/util_methods.rb
46
- - spec/lib/pdf_builder_spec.rb
47
- - spec/lib/pdf_collection_builder_spec.rb
46
+ - spec/lib/pdf_book_spec.rb
47
+ - spec/lib/pdf_page_spec.rb
48
48
  - spec/spec_helper.rb
49
49
  - spec/templates/certificate_template.pdf
50
50
  - tasks/build_gem.rake
51
51
  - tasks/documentation.rake
52
52
  has_rdoc: false
53
53
  homepage: http://wiki.github.com/gbdev/pdf-filler
54
+ licenses:
54
55
  post_install_message:
55
56
  rdoc_options:
56
57
  - --charset=UTF-8
@@ -77,10 +78,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
78
  requirements: []
78
79
 
79
80
  rubyforge_project:
80
- rubygems_version: 1.2.0
81
+ rubygems_version: 1.3.5
81
82
  signing_key:
82
83
  specification_version: 3
83
84
  summary: A Rails plugin to fill a PDF form using RJB and iText.
84
85
  test_files:
85
- - spec/lib/pdf_builder_spec.rb
86
- - spec/lib/pdf_collection_builder_spec.rb
86
+ - spec/lib/pdf_page_spec.rb
87
+ - spec/lib/pdf_book_spec.rb
@@ -1,79 +0,0 @@
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
@@ -1,65 +0,0 @@
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
@@ -1,60 +0,0 @@
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
@@ -1,12 +0,0 @@
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