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 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 veneer over the
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
@@ -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
@@ -1,17 +1,17 @@
1
1
  require 'java'
2
2
  require File.dirname(__FILE__) + '/../../vendor/iText-4.2.0'
3
3
 
4
- include_class "com.lowagie.text.pdf.AcroFields"
5
- include_class "com.lowagie.text.pdf.PdfArray"
6
- include_class "com.lowagie.text.pdf.PdfDictionary"
7
- include_class "com.lowagie.text.pdf.PdfName"
8
- include_class "com.lowagie.text.pdf.PdfObject"
9
- include_class "com.lowagie.text.pdf.PdfReader"
10
- include_class "com.lowagie.text.pdf.PdfStamper"
11
- include_class "com.lowagie.text.pdf.PdfStream"
12
- include_class "com.lowagie.text.pdf.PdfWriter"
13
- include_class "com.lowagie.text.pdf.XfaForm"
14
- include_class "com.lowagie.text.pdf.XfdfReader"
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
- doc = Nokogiri::XML::Document.wrap(@xfa.getDomDocument)
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_ do |v|
45
- v.text_ value
46
- end
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
@@ -1,3 +1,3 @@
1
1
  module PDFRavager
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
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
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.3
5
+ version: 0.0.4
6
6
  platform: java
7
7
  authors:
8
8
  - Abe Voelker