prawn-fillform 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +30 -0
- data/lib/prawn-fillform.rb +85 -90
- data/lib/prawn-fillform/version.rb +1 -1
- metadata +3 -2
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
|
+
|
data/lib/prawn-fillform.rb
CHANGED
@@ -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
|
-
|
6
|
+
def fill_form_with(data={})
|
17
7
|
|
18
|
-
|
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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
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
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
36
|
+
private
|
37
|
+
def acroform_fields_for(page)
|
38
|
+
acroform_specifications.first.fetch(page).fetch(:fields)
|
39
|
+
end
|
47
40
|
|
48
|
-
|
49
|
-
|
50
|
-
|
41
|
+
def acroform_number_of_pages
|
42
|
+
acroform_specifications.first.keys.length
|
43
|
+
end
|
51
44
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
-
|
81
|
-
field
|
82
|
-
|
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
|
-
|
85
|
+
def acroform_specifications
|
91
86
|
|
92
|
-
|
93
|
-
|
87
|
+
specifications = {}
|
88
|
+
references = []
|
94
89
|
|
95
|
-
|
96
|
-
|
90
|
+
state.pages.each_with_index do |page, i|
|
91
|
+
form_fields = deref(page.dictionary.data[:Annots])
|
97
92
|
|
98
|
-
|
99
|
-
|
100
|
-
|
93
|
+
page_number = "page_#{i+1}".to_sym
|
94
|
+
specifications[page_number] = {}
|
95
|
+
specifications[page_number][:fields] = []
|
101
96
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
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
|
-
|
108
|
-
|
102
|
+
field = acroform_field_info(form_fields, field_ref, i+1)
|
103
|
+
specifications[page_number][:fields] << field
|
109
104
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
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
|
-
|
114
|
+
end
|
120
115
|
end
|
121
116
|
|
122
117
|
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.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-
|
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
|