pdf_ravager 0.0.4-java → 0.0.5-java

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -17,17 +17,13 @@ data = {:name => 'Bob', :gender => 'm', :relation => 'Uncle' }
17
17
 
18
18
  info = pdf do
19
19
  text 'name', data[:name]
20
- html 'name_stylized', "<b>#{data[:name]}</b>" # warning: HTML is not escaped
20
+ rich_text 'name_stylized', "<b>#{data[:name]}</b>" # warning: text is not HTML-escaped!
21
21
  radio_group 'sex' do
22
- fill 'male', :if => data[:gender] == 'm'
23
- fill 'female', :if => data[:gender] == 'f'
22
+ fill 'male' if data[:gender] == 'm'
23
+ fill 'female' if data[:gender] == 'f'
24
24
  end
25
25
  check 'related' if data[:relation]
26
26
  checkbox_group 'relation' do
27
- check 'parent', :if => ['Mom', 'Dad'].include?(data[:relation])
28
- check 'sibling', :if => ['Brother', 'Sister'].include?(data[:relation])
29
- check 'other', :unless => ['Brother', 'Sister', 'Mom', 'Dad'].include?(data[:relation])
30
- # OR
31
27
  case data[:relation]
32
28
  when 'Mom', 'Dad'
33
29
  check 'parent'
@@ -44,9 +40,36 @@ info.ravage '/tmp/info.pdf', :out_file => '/tmp/info_filled.pdf'
44
40
 
45
41
  ## Usage
46
42
 
47
- To find the names of the fields, use a tool such as Adobe LiveCycle. The
48
- `html` type is a subset of XHTML defined by [Adobe's XFA standard][1] - full
49
- HTML support isn't available.
43
+ ### Field Names
44
+ To query and modify a form's field names, use a tool such as Adobe
45
+ LiveCycle.
46
+
47
+ ### Rich Text
48
+ The `rich_text` type is specific to XFA forms. To understand how it
49
+ should be used, see the "Rich Text Reference" section of
50
+ [Adobe's XFA standard][1]. Rich Text is defined there as a subset of
51
+ XHTML and CSS which uses some custom restrictions and extensions by
52
+ Adobe. The minimum XHTML and CSS elements that a standards-compliant
53
+ XFA processor (e.g. Adobe Reader) must support are also listed there
54
+ and can be used as a guide.
55
+
56
+ ### Checkbox Groups
57
+ Because there is no such thing as a "checkbox group," the
58
+ `checkbox_group` syntax is simply syntactic sugar for calling
59
+ `check` with the group name and a `.` prepended to the name. For
60
+ example,
61
+
62
+ ```ruby
63
+ checkbox_group 'relation' do
64
+ check 'parent'
65
+ end
66
+ ```
67
+
68
+ is equivalent to
69
+
70
+ ```ruby
71
+ check 'relation.parent'
72
+ ```
50
73
 
51
74
  ## Copyright
52
75
 
@@ -11,23 +11,14 @@ module PDFRavager
11
11
  end
12
12
 
13
13
  def text(name, value, opts={})
14
- return if opts.has_key?(:when) && !opts[:when]
15
- return if opts.has_key?(:if) && !opts[:if]
16
- return if opts.has_key?(:unless) && opts[:unless]
17
14
  @fields << {:name => name, :value => value, :type => :text}
18
15
  end
19
16
 
20
- def html(name, value, opts={})
21
- return if opts.has_key?(:when) && !opts[:when]
22
- return if opts.has_key?(:if) && !opts[:if]
23
- return if opts.has_key?(:unless) && opts[:unless]
24
- @fields << {:name => name, :value => value, :type => :html}
17
+ def rich_text(name, value, opts={})
18
+ @fields << {:name => name, :value => value, :type => :rich_text}
25
19
  end
26
20
 
27
21
  def check(name, opts={})
28
- return if opts.has_key?(:when) && !opts[:when]
29
- return if opts.has_key?(:if) && !opts[:if]
30
- return if opts.has_key?(:unless) && opts[:unless]
31
22
  @fields << {:name => name, :value => true, :type => :checkbox}
32
23
  end
33
24
 
@@ -36,9 +27,6 @@ module PDFRavager
36
27
  # TODO: replace w/ singleton method?
37
28
  PDF.instance_eval do
38
29
  send(:define_method, :fill) do |name, opts={}|
39
- return if opts.has_key?(:when) && !opts[:when]
40
- return if opts.has_key?(:if) && !opts[:if]
41
- return if opts.has_key?(:unless) && opts[:unless]
42
30
  fields << {:name => gname, :value => name, :type => :radio}
43
31
  end
44
32
  blk.call
@@ -32,9 +32,9 @@ module PDFRavager
32
32
  end
33
33
 
34
34
  def set_field_value(name, value, type=nil)
35
- return set_html_field(name, value) if type == :html
36
- # First use AcroForms method
35
+ return set_rich_text_field(name, value) if type == :rich_text
37
36
  begin
37
+ # First try AcroForms method of setting value
38
38
  @afields.setField(XfaForm::Xml2Som::getShortName(SOM.escape(name)), value)
39
39
  rescue java.lang.NullPointerException
40
40
  # If the AcroForms method doesn't work, we'll set the XDP
@@ -42,7 +42,7 @@ module PDFRavager
42
42
  # https://github.com/sparklemotion/nokogiri/issues/781
43
43
  doc = Nokogiri::XML(Nokogiri::XML::Document.wrap(@xfa.getDomDocument).to_xml)
44
44
  doc.xpath("//*[local-name()='field'][@name='#{name}']").each do |node|
45
- # Create an XML node in the XDP basically like this: "<value><text>#{value}</text></value>"
45
+ # Create an XML node in the XDP like this: "<value><text>#{value}</text></value>"
46
46
  Nokogiri::XML::Builder.with(node) do |xml|
47
47
  xml.value_ {
48
48
  xml.text_ {
@@ -56,15 +56,6 @@ module PDFRavager
56
56
  end
57
57
  end
58
58
 
59
- def get_field_type(name)
60
- short_name = XfaForm::Xml2Som::getShortName(SOM.escape(name))
61
- begin
62
- @afields.getFieldType(short_name)
63
- rescue java.lang.NullPointerException
64
- nil
65
- end
66
- end
67
-
68
59
  def destroy
69
60
  @stamper.close
70
61
  end
@@ -81,7 +72,7 @@ module PDFRavager
81
72
  @som_template = @xfa.getTemplateSom
82
73
  end
83
74
 
84
- def set_html_field(name, value)
75
+ def set_rich_text_field(name, value)
85
76
  doc = Nokogiri::XML(Nokogiri::XML::Document.wrap(@xfa.getDomDocument).to_xml)
86
77
  doc.xpath("//*[local-name()='field'][@name='#{name}']").each do |node|
87
78
  Nokogiri::XML::Builder.with(node) do |xml|
@@ -1,3 +1,3 @@
1
1
  module PDFRavager
2
- VERSION = "0.0.4"
3
- end
2
+ VERSION = "0.0.5"
3
+ end
data/spec/pdf_spec.rb CHANGED
@@ -4,44 +4,14 @@ require 'pdf_ravager/pdf'
4
4
  class TestPDF < MiniTest::Unit::TestCase
5
5
  def setup
6
6
  @pdf = pdf 'foo.pdf' do
7
- text 'text_always', 'foo'
8
- text 'text_when_true', 'foo', :when => true
9
- text 'text_when_false', 'foo', :when => false
10
- text 'text_if_true', 'foo', :if => true
11
- text 'text_if_false', 'foo', :if => false
12
- text 'text_unless_true', 'foo', :unless => true
13
- text 'text_unless_false', 'foo', :unless => false
14
- html 'html', '<b>foo</b>'
15
- check 'checkbox'
16
- radio_group 'radio_group_always_filled' do
17
- fill 'foo'
18
- end
19
- radio_group 'radio_group_if' do
20
- fill 'true', :if => true
21
- fill 'false', :if => false
22
- end
23
- radio_group 'radio_group_when' do
24
- fill 'true', :when => true
25
- fill 'false', :when => false
26
- end
27
- radio_group 'radio_group_unless' do
28
- fill 'true', :unless => true
29
- fill 'false', :unless => false
30
- end
31
- checkbox_group 'checkbox_group_always_checked' do
7
+ text 'text', 'foo'
8
+ rich_text 'rich_text', '<b>foo</b>'
9
+ check 'checkbox'
10
+ checkbox_group 'checkbox_group' do
32
11
  check 'foo'
33
12
  end
34
- checkbox_group 'checkbox_group_if' do
35
- check 'true', :if => true
36
- check 'false', :if => false
37
- end
38
- checkbox_group 'checkbox_group_when' do
39
- check 'true', :when => true
40
- check 'false', :when => false
41
- end
42
- checkbox_group 'checkbox_group_unless' do
43
- check 'true', :unless => true
44
- check 'false', :unless => false
13
+ radio_group 'radio_group' do
14
+ fill 'foo'
45
15
  end
46
16
  end
47
17
 
@@ -53,95 +23,23 @@ class TestPDF < MiniTest::Unit::TestCase
53
23
  end
54
24
 
55
25
  def test_that_text_is_set
56
- assert_includes @pdf.fields, {:name => 'text_always', :value => 'foo', :type => :text}
57
- end
58
-
59
- def test_that_text_when_true_is_set
60
- assert_includes @pdf.fields, {:name => 'text_when_true', :value => 'foo', :type => :text}
61
- end
62
-
63
- def test_that_text_when_false_is_not_set
64
- refute_includes @pdf.fields, {:name => 'text_when_false', :value => 'foo', :type => :text}
65
- end
66
-
67
- def test_that_text_if_true_is_set
68
- assert_includes @pdf.fields, {:name => 'text_if_true', :value => 'foo', :type => :text}
26
+ assert_includes @pdf.fields, {:name => 'text', :value => 'foo', :type => :text}
69
27
  end
70
28
 
71
- def test_that_text_if_false_is_not_set
72
- refute_includes @pdf.fields, {:name => 'text_if_false', :value => 'foo', :type => :text}
73
- end
74
-
75
- def test_that_text_unless_true_is_not_set
76
- refute_includes @pdf.fields, {:name => 'text_unless_true', :value => 'foo', :type => :text}
77
- end
78
-
79
- def test_that_text_unless_false_is_set
80
- assert_includes @pdf.fields, {:name => 'text_unless_false', :value => 'foo', :type => :text}
81
- end
82
-
83
- def test_that_html_is_set
84
- assert_includes @pdf.fields, {:name => 'html', :value => '<b>foo</b>', :type => :html}
29
+ def test_that_rich_text_is_set
30
+ assert_includes @pdf.fields, {:name => 'rich_text', :value => '<b>foo</b>', :type => :rich_text}
85
31
  end
86
32
 
87
33
  def test_that_checkbox_is_set
88
34
  assert_includes @pdf.fields, {:name => 'checkbox', :value => true, :type => :checkbox}
89
35
  end
90
36
 
91
- def test_that_radio_group_always_filled_is_filled
92
- assert_includes @pdf.fields, {:name => 'radio_group_always_filled', :value => 'foo', :type => :radio}
93
- end
94
-
95
- def test_that_radio_group_when_true_is_set
96
- assert_includes @pdf.fields, {:name => 'radio_group_when', :value => 'true', :type => :radio}
97
- end
98
-
99
- def test_that_radio_group_when_false_is_not_set
100
- refute_includes @pdf.fields, {:name => 'radio_group_when', :value => 'false', :type => :radio}
101
- end
102
-
103
- def test_that_radio_group_if_true_is_set
104
- assert_includes @pdf.fields, {:name => 'radio_group_if', :value => 'true', :type => :radio}
105
- end
106
-
107
- def test_that_radio_group_if_false_is_not_set
108
- refute_includes @pdf.fields, {:name => 'radio_group_if', :value => 'false', :type => :radio}
109
- end
110
-
111
- def test_that_radio_group_unless_true_is_not_set
112
- refute_includes @pdf.fields, {:name => 'radio_group_unless', :value => 'true', :type => :radio}
113
- end
114
-
115
- def test_that_radio_group_unless_false_is_set
116
- assert_includes @pdf.fields, {:name => 'radio_group_unless', :value => 'false', :type => :radio}
117
- end
118
-
119
- def test_that_checkbox_group_always_checked_is_checked
120
- assert_includes @pdf.fields, {:name => 'checkbox_group_always_checked.foo', :value => true, :type => :checkbox}
121
- end
122
-
123
- def test_that_checkbox_group_when_true_is_set
124
- assert_includes @pdf.fields, {:name => 'checkbox_group_when.true', :value => true, :type => :checkbox}
125
- end
126
-
127
- def test_that_checkbox_group_when_false_is_not_set
128
- refute_includes @pdf.fields, {:name => 'checkbox_group_when.false', :value => true, :type => :checkbox}
129
- end
130
-
131
- def test_that_checkbox_group_if_true_is_set
132
- assert_includes @pdf.fields, {:name => 'checkbox_group_if.true', :value => true, :type => :checkbox}
133
- end
134
-
135
- def test_that_checkbox_group_if_false_is_not_set
136
- refute_includes @pdf.fields, {:name => 'checkbox_group_if.false', :value => true, :type => :checkbox}
137
- end
138
-
139
- def test_that_checkbox_group_unless_true_is_not_set
140
- refute_includes @pdf.fields, {:name => 'checkbox_group_unless.true', :value => true, :type => :checkbox}
37
+ def test_that_checkbox_group_is_set
38
+ assert_includes @pdf.fields, {:name => 'checkbox_group.foo', :value => true, :type => :checkbox}
141
39
  end
142
40
 
143
- def test_that_checkbox_group_unless_false_is_set
144
- assert_includes @pdf.fields, {:name => 'checkbox_group_unless.false', :value => true, :type => :checkbox}
41
+ def test_that_radio_group_is_set
42
+ assert_includes @pdf.fields, {:name => 'radio_group', :value => 'foo', :type => :radio}
145
43
  end
146
44
 
147
45
  def test_json_serialization
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: pdf_ravager
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: java
7
7
  authors:
8
8
  - Abe Voelker