prawn-fillform 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in prawn-fillform.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ module Prawn
2
+ module Fillform
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,124 @@
1
+ require 'prawn-fillform/version'
2
+
3
+ module Prawn
4
+ module Fillform
5
+ def form_fields(page=nil)
6
+ specifications = acroform_specifications.first
7
+ if page
8
+ page_number = "page_#{page}".to_sym
9
+ page = specifications.fetch(page_number)
10
+ fields = page.fetch(:fields)
11
+ else
12
+ specifications
13
+ end
14
+ end
15
+
16
+ def fill_form_with(data={})
17
+
18
+ 1.upto(acroform_number_of_pages).each do |page|
19
+ fields = form_fields(page)
20
+
21
+ page_number = "page_#{page}".to_sym
22
+
23
+ fields.each do |field|
24
+ x = field[:x]
25
+ y = field[:y]
26
+ value = data[page_number][field.fetch(:name)] rescue nil
27
+ go_to_page(field[:page_number])
28
+
29
+ if field[:type] == :image
30
+ image value, :at => [x, y], :position => :center, :vposition => :center, :fit => [field[:width], field[:height]] if value
31
+ else
32
+ text_box value, :at => [x, y], :align => field[:align],
33
+ :width => field[:width], :height => field[:height],
34
+ :valign => :center, :align => :left, :single_line => !field[:multi_line], :size => field[:size] if value
35
+ end
36
+ end
37
+ end
38
+
39
+ # delete acroform references
40
+ acroform_specifications.last.each do |reference|
41
+ reference[:acroform_fields].delete(reference[:field])
42
+ end
43
+ end
44
+
45
+
46
+ private
47
+
48
+ def acroform_number_of_pages
49
+ acroform_specifications.first.keys.length
50
+ end
51
+
52
+ def acroform_field_info(form_fields, field_ref, page)
53
+
54
+ field_dict = deref(field_ref)
55
+
56
+ field = {}
57
+
58
+ field_type = deref(field_dict[:FT])
59
+
60
+ field[:name] = deref(field_dict[:T]).to_sym
61
+ field[:type] = field_type == :Tx ? :text : :image
62
+ field[:rect] = deref(field_dict[:Rect])
63
+ field[:x] = field[:rect][0]
64
+ field[:y] = field[:rect][3]
65
+ field[:width] = field[:rect][2] - field[:rect][0]
66
+ field[:height] = field[:rect][3] - field[:rect][1]
67
+ field[:page_number] = page
68
+
69
+ if field[:type] == :text
70
+ field[:description] = deref(field_dict[:TU])
71
+ field[:default_value] = deref(field_dict[:DV])
72
+ field[:value] = deref(field_dict[:V])
73
+ field[:size] = deref(field_dict[:DA]).split(" ")[1].to_i
74
+ field[:style] = deref(field_dict[:DA].split(" ")[0])
75
+ field[:align] = case deref(field_dict[:Q])
76
+ when 0 then :left
77
+ when 2 then :center
78
+ when 4 then :right
79
+ end
80
+ field[:max_length] = deref(field_dict[:MaxLen])
81
+ field[:multi_line] = deref(field_dict[:Ff]).to_i > 0 ? :true : :false
82
+ field[:border_width] = deref(field_dict[:BS]).fetch(:W, nil)
83
+ field[:border_style] = deref(field_dict[:BS]).fetch(:S, nil)
84
+ field[:border_color] = deref(field_dict[:MK]).fetch(:BC, nil)
85
+ field[:background_color] = deref(field_dict[:MK]).fetch(:BG, nil)
86
+ end
87
+ field
88
+ end
89
+
90
+ def acroform_specifications
91
+
92
+ specifications = {}
93
+ references = []
94
+
95
+ state.pages.each_with_index do |page, i|
96
+ form_fields = deref(page.dictionary.data[:Annots])
97
+
98
+ page_number = "page_#{i+1}".to_sym
99
+ specifications[page_number] = {}
100
+ specifications[page_number][:fields] = []
101
+
102
+ form_fields.map do |field_ref|
103
+ field_dict = deref(field_ref)
104
+ next unless deref(field_dict[:Type]) == :Annot and deref(field_dict[:Subtype]) == :Widget
105
+ next unless (deref(field_dict[:FT]) == :Tx || deref(field_dict[:FT]) == :Btn)
106
+
107
+ field = acroform_field_info(form_fields, field_ref, i+1)
108
+ specifications[page_number][:fields] << field
109
+
110
+ reference = {}
111
+ reference[:field] = field_ref
112
+ reference[:acroform_fields] = form_fields
113
+ references << reference
114
+ end
115
+ end
116
+ [specifications, references]
117
+ end
118
+
119
+ end
120
+ end
121
+
122
+ require 'prawn/document'
123
+ Prawn::Document.send(:include, Prawn::Forms)
124
+
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "prawn-fillform/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "prawn-fillform"
7
+ s.version = Prawn::Fillform::VERSION
8
+ s.authors = ["Maurice Hadamczyk"]
9
+ s.email = ["moessimple@googlemail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{fill text and images through acroform fields}
12
+
13
+ s.rubyforge_project = "prawn-fillform"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ # specify any dependencies here; for example:
21
+ # s.add_development_dependency "rspec"
22
+ # s.add_runtime_dependency "rest-client"
23
+ end
24
+
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prawn-fillform
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Maurice Hadamczyk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-09-26 00:00:00.000000000Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ - moessimple@googlemail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - Rakefile
24
+ - lib/prawn-fillform.rb
25
+ - lib/prawn-fillform/version.rb
26
+ - prawn-fillform.gemspec
27
+ homepage: ''
28
+ licenses: []
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ requirements: []
46
+ rubyforge_project: prawn-fillform
47
+ rubygems_version: 1.8.10
48
+ signing_key:
49
+ specification_version: 3
50
+ summary: fill text and images through acroform fields
51
+ test_files: []