prawn-fillform 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Prawn/Fillform: Fill Text and Images through Acroform Fields
2
+
3
+ ## Install
4
+
5
+ ```bash
6
+ $ gem install prawn-fillform
7
+ ```
8
+
9
+ ## Usage
10
+ Create a PDF form with Scribus, Adobe Products or something else. I have only tested this with Scribus.
11
+ Currently only text fields and buttons are supported. Buttons are replaced by images.
12
+
13
+
14
+ ```ruby
15
+ require 'prawn-fillform'
16
+
17
+ data = {}
18
+ data[:page_1] = {}
19
+ data[:page_1][:firstname] = "Max"
20
+ data[:page_1][:lastname] = "Mustermann"
21
+ data[:page_1][:photo] = "test.jpg"
22
+
23
+ # Create a PDF file with predefined data Fields
24
+ Prawn::Document.generate "output.pdf", :template => "template.pdf" do |pdf|
25
+ pdf.fill_form_with(data)
26
+ end
27
+ ```
28
+
29
+ Take a look in `examples` folder
30
+
@@ -2,121 +2,116 @@ require 'prawn-fillform/version'
2
2
 
3
3
  module Prawn
4
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
5
 
16
- def fill_form_with(data={})
6
+ def fill_form_with(data={})
17
7
 
18
- 1.upto(acroform_number_of_pages).each do |page|
19
- fields = form_fields(page)
8
+ 1.upto(acroform_number_of_pages).each do |page|
20
9
 
21
- page_number = "page_#{page}".to_sym
22
10
 
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])
11
+ page_number = "page_#{page}".to_sym
12
+ fields = acroform_fields_for(page_number)
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])
28
18
 
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
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
35
26
  end
36
27
  end
37
- end
38
28
 
39
- # delete acroform references
40
- acroform_specifications.last.each do |reference|
41
- reference[:acroform_fields].delete(reference[:field])
29
+ # delete acroform references
30
+ acroform_specifications.last.each do |reference|
31
+ reference[:acroform_fields].delete(reference[:field])
32
+ end
42
33
  end
43
- end
44
34
 
45
35
 
46
- private
36
+ private
37
+ def acroform_fields_for(page)
38
+ acroform_specifications.first.fetch(page).fetch(:fields)
39
+ end
47
40
 
48
- def acroform_number_of_pages
49
- acroform_specifications.first.keys.length
50
- end
41
+ def acroform_number_of_pages
42
+ acroform_specifications.first.keys.length
43
+ end
51
44
 
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
45
+ def acroform_field_info(form_fields, field_ref, page)
46
+
47
+ field_dict = deref(field_ref)
48
+
49
+ field = {}
50
+
51
+ field_type = deref(field_dict[:FT])
52
+
53
+ field[:name] = deref(field_dict[:T]).to_sym
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
61
+
62
+ if field[:type] == :text
63
+ field[:description] = deref(field_dict[:TU])
64
+ field[:default_value] = deref(field_dict[:DV])
65
+ field[:value] = deref(field_dict[:V])
66
+ field[:size] = deref(field_dict[:DA]).split(" ")[1].to_i
67
+ field[:style] = deref(field_dict[:DA].split(" ")[0])
68
+ field[:align] = case deref(field_dict[:Q])
69
+ when 0 then :left
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])
79
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)
80
+
81
+ field
82
+
86
83
  end
87
- field
88
- end
89
84
 
90
- def acroform_specifications
85
+ def acroform_specifications
91
86
 
92
- specifications = {}
93
- references = []
87
+ specifications = {}
88
+ references = []
94
89
 
95
- state.pages.each_with_index do |page, i|
96
- form_fields = deref(page.dictionary.data[:Annots])
90
+ state.pages.each_with_index do |page, i|
91
+ form_fields = deref(page.dictionary.data[:Annots])
97
92
 
98
- page_number = "page_#{i+1}".to_sym
99
- specifications[page_number] = {}
100
- specifications[page_number][:fields] = []
93
+ page_number = "page_#{i+1}".to_sym
94
+ specifications[page_number] = {}
95
+ specifications[page_number][:fields] = []
101
96
 
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)
97
+ form_fields.map do |field_ref|
98
+ field_dict = deref(field_ref)
99
+ next unless deref(field_dict[:Type]) == :Annot and deref(field_dict[:Subtype]) == :Widget
100
+ next unless (deref(field_dict[:FT]) == :Tx || deref(field_dict[:FT]) == :Btn)
106
101
 
107
- field = acroform_field_info(form_fields, field_ref, i+1)
108
- specifications[page_number][:fields] << field
102
+ field = acroform_field_info(form_fields, field_ref, i+1)
103
+ specifications[page_number][:fields] << field
109
104
 
110
- reference = {}
111
- reference[:field] = field_ref
112
- reference[:acroform_fields] = form_fields
113
- references << reference
105
+ reference = {}
106
+ reference[:field] = field_ref
107
+ reference[:acroform_fields] = form_fields
108
+ references << reference
109
+ end
114
110
  end
111
+ [specifications, references]
115
112
  end
116
- [specifications, references]
117
- end
118
113
 
119
- end
114
+ end
120
115
  end
121
116
 
122
117
  require 'prawn/document'
@@ -1,6 +1,6 @@
1
1
  module Prawn
2
2
  module Fillform
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
6
6
 
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.2
4
+ version: 0.0.3
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-26 00:00:00.000000000Z
12
+ date: 2011-09-27 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description:
15
15
  email:
@@ -20,6 +20,7 @@ extra_rdoc_files: []
20
20
  files:
21
21
  - .gitignore
22
22
  - Gemfile
23
+ - README.md
23
24
  - Rakefile
24
25
  - data/template.pdf
25
26
  - data/test.jpg