prawn-fillform 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/prawn-fillform/version.rb +1 -1
- data/lib/prawn-fillform.rb +127 -76
- metadata +2 -2
data/lib/prawn-fillform.rb
CHANGED
@@ -3,115 +3,166 @@ require 'prawn-fillform/version'
|
|
3
3
|
module Prawn
|
4
4
|
module Fillform
|
5
5
|
|
6
|
-
|
6
|
+
class Field
|
7
|
+
include Prawn::Document::Internals
|
7
8
|
|
8
|
-
|
9
|
+
def initialize(dictionary)
|
10
|
+
@dictionary = dictionary
|
11
|
+
end
|
9
12
|
|
13
|
+
def description
|
14
|
+
deref(@dictionary[:TU])
|
15
|
+
end
|
10
16
|
|
11
|
-
|
12
|
-
|
13
|
-
fields.each do |field|
|
14
|
-
x = field[:x]
|
15
|
-
y = field[:y]
|
16
|
-
value = data[page_number][field.fetch(:name)] rescue nil
|
17
|
-
go_to_page(field[:page_number])
|
18
|
-
|
19
|
-
if field[:type] == :image
|
20
|
-
image value, :at => [x, y], :position => :center, :vposition => :center, :fit => [field[:width], field[:height]] if value
|
21
|
-
else
|
22
|
-
text_box value, :at => [x, y], :align => field[:align],
|
23
|
-
:width => field[:width], :height => field[:height],
|
24
|
-
:valign => :center, :align => :left, :single_line => !field[:multi_line], :size => field[:size] if value
|
25
|
-
end
|
26
|
-
end
|
17
|
+
def rect
|
18
|
+
deref(@dictionary[:Rect])
|
27
19
|
end
|
28
20
|
|
29
|
-
|
30
|
-
|
31
|
-
|
21
|
+
def name
|
22
|
+
deref(@dictionary[:T]).to_sym
|
23
|
+
end
|
24
|
+
|
25
|
+
def x
|
26
|
+
rect[0]
|
32
27
|
end
|
33
|
-
end
|
34
28
|
|
29
|
+
def y
|
30
|
+
rect[3]
|
31
|
+
end
|
35
32
|
|
36
|
-
|
37
|
-
|
38
|
-
acroform_specifications.first.fetch(page).fetch(:fields)
|
33
|
+
def width
|
34
|
+
rect[2] - rect[0]
|
39
35
|
end
|
40
36
|
|
41
|
-
def
|
42
|
-
|
37
|
+
def height
|
38
|
+
rect[3] - rect[1]
|
43
39
|
end
|
44
40
|
|
45
|
-
def
|
41
|
+
def value
|
42
|
+
deref(@dictionary[:V])
|
43
|
+
end
|
46
44
|
|
47
|
-
|
45
|
+
def default_value
|
46
|
+
deref(@dictionary[:DV])
|
47
|
+
end
|
48
48
|
|
49
|
-
|
49
|
+
def flags
|
50
|
+
deref(@dictionary[:Ff])
|
51
|
+
end
|
50
52
|
|
51
|
-
|
53
|
+
end
|
52
54
|
|
53
|
-
|
54
|
-
field[:type] = field_type == :Tx ? :text : :image
|
55
|
-
field[:rect] = deref(field_dict[:Rect])
|
56
|
-
field[:x] = field[:rect][0]
|
57
|
-
field[:y] = field[:rect][3]
|
58
|
-
field[:width] = field[:rect][2] - field[:rect][0]
|
59
|
-
field[:height] = field[:rect][3] - field[:rect][1]
|
60
|
-
field[:page_number] = page
|
55
|
+
class Text < Field
|
61
56
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
when 2 then :center
|
71
|
-
when 4 then :right
|
72
|
-
end
|
73
|
-
field[:max_length] = deref(field_dict[:MaxLen])
|
74
|
-
field[:multi_line] = deref(field_dict[:Ff]).to_i > 0 ? :true : :false
|
75
|
-
field[:border_width] = deref(field_dict[:BS]).fetch(:W, nil) if deref(field_dict[:BS])
|
76
|
-
field[:border_style] = deref(field_dict[:BS]).fetch(:S, nil) if deref(field_dict[:BS])
|
77
|
-
field[:border_color] = deref(field_dict[:MK]).fetch(:BC, nil) if deref(field_dict[:MK])
|
78
|
-
field[:background_color] = deref(field_dict[:MK]).fetch(:BG, nil) if deref(field_dict[:MK])
|
57
|
+
def align
|
58
|
+
case deref(@dictionary[:Q])
|
59
|
+
when 0
|
60
|
+
:left
|
61
|
+
when 1
|
62
|
+
:center
|
63
|
+
when 2
|
64
|
+
:right
|
79
65
|
end
|
66
|
+
end
|
80
67
|
|
81
|
-
|
68
|
+
def max_length
|
69
|
+
deref(@dictionary[:MaxLen])
|
70
|
+
end
|
82
71
|
|
72
|
+
def type
|
73
|
+
:text
|
83
74
|
end
|
75
|
+
end
|
84
76
|
|
85
|
-
|
77
|
+
class Button < Field
|
78
|
+
def type
|
79
|
+
:button
|
80
|
+
end
|
81
|
+
end
|
86
82
|
|
87
|
-
|
88
|
-
|
83
|
+
class References
|
84
|
+
include Prawn::Document::Internals
|
85
|
+
def initialize(state)
|
86
|
+
@state = state
|
87
|
+
@refs = []
|
88
|
+
collect!
|
89
|
+
end
|
89
90
|
|
90
|
-
|
91
|
-
|
91
|
+
def delete!
|
92
|
+
@refs.each do |ref|
|
93
|
+
ref[:annots].delete(ref[:ref])
|
94
|
+
end
|
95
|
+
end
|
92
96
|
|
93
|
-
|
94
|
-
|
95
|
-
|
97
|
+
protected
|
98
|
+
def collect!
|
99
|
+
@state.pages.each_with_index do |page, i|
|
100
|
+
@annots = deref(page.dictionary.data[:Annots])
|
101
|
+
@annots.map do |ref|
|
102
|
+
reference = {}
|
103
|
+
reference[:ref] = ref
|
104
|
+
reference[:annots] = @annots
|
105
|
+
@refs << reference
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
96
110
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
111
|
+
def acroform_fields
|
112
|
+
acroform = {}
|
113
|
+
state.pages.each_with_index do |page, i|
|
114
|
+
annots = deref(page.dictionary.data[:Annots])
|
115
|
+
page_number = "page_#{i+1}".to_sym
|
116
|
+
acroform[page_number] = []
|
117
|
+
acroform[page_number] = []
|
118
|
+
annots.map do |ref|
|
119
|
+
dictionary = deref(ref)
|
120
|
+
|
121
|
+
next unless deref(dictionary[:Type]) == :Annot and deref(dictionary[:Subtype]) == :Widget
|
122
|
+
next unless (deref(dictionary[:FT]) == :Tx || deref(dictionary[:FT]) == :Btn)
|
123
|
+
|
124
|
+
type = deref(dictionary[:FT]).to_sym
|
125
|
+
case type
|
126
|
+
when :Tx
|
127
|
+
acroform[page_number] << Text.new(dictionary)
|
128
|
+
when :Btn
|
129
|
+
acroform[page_number] << Button.new(dictionary)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
acroform
|
134
|
+
end
|
101
135
|
|
102
|
-
|
103
|
-
specifications[page_number][:fields] << field
|
136
|
+
def fill_form_with(data={})
|
104
137
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
138
|
+
acroform_fields.each do |page, fields|
|
139
|
+
fields.each do |field|
|
140
|
+
number = page.to_s.split("_").last.to_i
|
141
|
+
go_to_page(number)
|
142
|
+
value = data[page][field.name] rescue nil
|
143
|
+
|
144
|
+
if value
|
145
|
+
if field.instance_of? Prawn::Fillform::Text
|
146
|
+
text_box value, :at => [field.x, field.y],
|
147
|
+
:align => field.align,
|
148
|
+
:width => field.width,
|
149
|
+
:height => field.height,
|
150
|
+
:valign => :center
|
151
|
+
elsif field.instance_of? Prawn::Fillform::Button
|
152
|
+
image value, :at => [field.x, field.y],
|
153
|
+
:position => :center,
|
154
|
+
:vposition => :center,
|
155
|
+
:fit => [field.width, field.height]
|
156
|
+
end
|
109
157
|
end
|
110
158
|
end
|
111
|
-
[specifications, references]
|
112
159
|
end
|
113
160
|
|
161
|
+
references = References.new(state)
|
162
|
+
references.delete!
|
163
|
+
|
114
164
|
end
|
165
|
+
end
|
115
166
|
end
|
116
167
|
|
117
168
|
require 'prawn/document'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: prawn-fillform
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-28 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
14
|
description:
|
15
15
|
email:
|