pdf_ravager 0.0.3-java → 0.0.4-java
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 +8 -3
- data/lib/pdf_ravager/pdf.rb +8 -1
- data/lib/pdf_ravager/ravager.rb +38 -16
- data/lib/pdf_ravager/version.rb +1 -1
- data/spec/pdf_spec.rb +5 -0
- metadata +1 -1
data/README.md
CHANGED
@@ -4,7 +4,7 @@ Provides a simple DSL for easily filling out AcroForms PDF or XFA documents.
|
|
4
4
|
|
5
5
|
## Description
|
6
6
|
|
7
|
-
This library uses a combination of a simple DSL and a minimal
|
7
|
+
This library uses a combination of a simple DSL and a minimal façade over the
|
8
8
|
last free version of the iText library to aid in filling out AcroForms PDF or
|
9
9
|
XFA documents.
|
10
10
|
|
@@ -17,6 +17,7 @@ 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
21
|
radio_group 'sex' do
|
21
22
|
fill 'male', :if => data[:gender] == 'm'
|
22
23
|
fill 'female', :if => data[:gender] == 'f'
|
@@ -43,9 +44,13 @@ info.ravage '/tmp/info.pdf', :out_file => '/tmp/info_filled.pdf'
|
|
43
44
|
|
44
45
|
## Usage
|
45
46
|
|
46
|
-
To find the names of the fields, use a tool such as Adobe LiveCycle.
|
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.
|
47
50
|
|
48
51
|
## Copyright
|
49
52
|
|
50
53
|
Copyright (c) 2012 Abe Voelker. Released under the terms of the
|
51
|
-
MIT license. See LICENSE for details.
|
54
|
+
MIT license. See LICENSE for details.
|
55
|
+
|
56
|
+
[1]: http://partners.adobe.com/public/developer/xml/index_arch.html
|
data/lib/pdf_ravager/pdf.rb
CHANGED
@@ -17,6 +17,13 @@ module PDFRavager
|
|
17
17
|
@fields << {:name => name, :value => value, :type => :text}
|
18
18
|
end
|
19
19
|
|
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}
|
25
|
+
end
|
26
|
+
|
20
27
|
def check(name, opts={})
|
21
28
|
return if opts.has_key?(:when) && !opts[:when]
|
22
29
|
return if opts.has_key?(:if) && !opts[:if]
|
@@ -64,7 +71,7 @@ module PDFRavager
|
|
64
71
|
else
|
65
72
|
f[:value]
|
66
73
|
end
|
67
|
-
pdf.set_field_value(f[:name], value)
|
74
|
+
pdf.set_field_value(f[:name], value, f[:type])
|
68
75
|
end
|
69
76
|
end
|
70
77
|
end
|
data/lib/pdf_ravager/ravager.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
require 'java'
|
2
2
|
require File.dirname(__FILE__) + '/../../vendor/iText-4.2.0'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
4
|
+
java_import "com.lowagie.text.pdf.AcroFields"
|
5
|
+
java_import "com.lowagie.text.pdf.PdfArray"
|
6
|
+
java_import "com.lowagie.text.pdf.PdfDictionary"
|
7
|
+
java_import "com.lowagie.text.pdf.PdfName"
|
8
|
+
java_import "com.lowagie.text.pdf.PdfObject"
|
9
|
+
java_import "com.lowagie.text.pdf.PdfReader"
|
10
|
+
java_import "com.lowagie.text.pdf.PdfStamper"
|
11
|
+
java_import "com.lowagie.text.pdf.PdfStream"
|
12
|
+
java_import "com.lowagie.text.pdf.PdfWriter"
|
13
|
+
java_import "com.lowagie.text.pdf.XfaForm"
|
14
|
+
java_import "com.lowagie.text.pdf.XfdfReader"
|
15
15
|
|
16
16
|
module PDFRavager
|
17
17
|
class Ravager
|
@@ -31,19 +31,24 @@ module PDFRavager
|
|
31
31
|
out
|
32
32
|
end
|
33
33
|
|
34
|
-
def set_field_value(name, value)
|
34
|
+
def set_field_value(name, value, type=nil)
|
35
|
+
return set_html_field(name, value) if type == :html
|
35
36
|
# First use AcroForms method
|
36
37
|
begin
|
37
38
|
@afields.setField(XfaForm::Xml2Som::getShortName(SOM.escape(name)), value)
|
38
39
|
rescue java.lang.NullPointerException
|
39
40
|
# If the AcroForms method doesn't work, we'll set the XDP
|
40
|
-
|
41
|
+
# Note: the double-load is to work around a Nokogiri bug I found:
|
42
|
+
# https://github.com/sparklemotion/nokogiri/issues/781
|
43
|
+
doc = Nokogiri::XML(Nokogiri::XML::Document.wrap(@xfa.getDomDocument).to_xml)
|
41
44
|
doc.xpath("//*[local-name()='field'][@name='#{name}']").each do |node|
|
42
45
|
# Create an XML node in the XDP basically like this: "<value><text>#{value}</text></value>"
|
43
46
|
Nokogiri::XML::Builder.with(node) do |xml|
|
44
|
-
xml.value_
|
45
|
-
|
46
|
-
|
47
|
+
xml.value_ {
|
48
|
+
xml.text_ {
|
49
|
+
xml.text value
|
50
|
+
}
|
51
|
+
}
|
47
52
|
end
|
48
53
|
end
|
49
54
|
@xfa.setDomDocument(doc.to_java)
|
@@ -76,6 +81,23 @@ module PDFRavager
|
|
76
81
|
@som_template = @xfa.getTemplateSom
|
77
82
|
end
|
78
83
|
|
84
|
+
def set_html_field(name, value)
|
85
|
+
doc = Nokogiri::XML(Nokogiri::XML::Document.wrap(@xfa.getDomDocument).to_xml)
|
86
|
+
doc.xpath("//*[local-name()='field'][@name='#{name}']").each do |node|
|
87
|
+
Nokogiri::XML::Builder.with(node) do |xml|
|
88
|
+
xml.value_ do
|
89
|
+
xml.exData('contentType' => 'text/html') do
|
90
|
+
xml.body_('xmlns' => "http://www.w3.org/1999/xhtml", 'xmlns:xfa' => "http://www.xfa.org/schema/xfa-data/1.0/") do
|
91
|
+
xml << value # Note: this value is not sanitized/escaped!
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
@xfa.setDomDocument(doc.to_java)
|
98
|
+
@xfa.setChanged(true)
|
99
|
+
end
|
100
|
+
|
79
101
|
end
|
80
102
|
|
81
103
|
class SOM
|
data/lib/pdf_ravager/version.rb
CHANGED
data/spec/pdf_spec.rb
CHANGED
@@ -11,6 +11,7 @@ class TestPDF < MiniTest::Unit::TestCase
|
|
11
11
|
text 'text_if_false', 'foo', :if => false
|
12
12
|
text 'text_unless_true', 'foo', :unless => true
|
13
13
|
text 'text_unless_false', 'foo', :unless => false
|
14
|
+
html 'html', '<b>foo</b>'
|
14
15
|
check 'checkbox'
|
15
16
|
radio_group 'radio_group_always_filled' do
|
16
17
|
fill 'foo'
|
@@ -79,6 +80,10 @@ class TestPDF < MiniTest::Unit::TestCase
|
|
79
80
|
assert_includes @pdf.fields, {:name => 'text_unless_false', :value => 'foo', :type => :text}
|
80
81
|
end
|
81
82
|
|
83
|
+
def test_that_html_is_set
|
84
|
+
assert_includes @pdf.fields, {:name => 'html', :value => '<b>foo</b>', :type => :html}
|
85
|
+
end
|
86
|
+
|
82
87
|
def test_that_checkbox_is_set
|
83
88
|
assert_includes @pdf.fields, {:name => 'checkbox', :value => true, :type => :checkbox}
|
84
89
|
end
|