pdf-forms 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b38b51cb4c9b3164c52041260accc25b36cb1bdb
4
+ data.tar.gz: 590aeedcdc7d0481d6102b5d82468a46c80912c5
5
+ SHA512:
6
+ metadata.gz: ca665b990d61e8a9f7510d6b6580620ececf6b4d4ad2994a095be3dc719a92a2356a9314501dcab392ee0c0aab951239d2585048a94257a669cd5a9db4f51d07
7
+ data.tar.gz: 5bcb0c643f3f8d20acea518730ba7a69977284a14910fc40006c9d026f0d7f99be500ed89dd91bfb279184b39e6cebc4cd1f9a1280ea1c16faefda03212c5f79
data/README.rdoc CHANGED
@@ -1,4 +1,4 @@
1
- = pdf-forms
1
+ = pdf-forms
2
2
  http://github.com/jkraemer/pdf-forms/
3
3
  by Jens Kraemer, jk@jkraemer.net
4
4
 
@@ -8,31 +8,36 @@ Fill out PDF forms with pdftk (http://www.accesspdf.com/pdftk/).
8
8
 
9
9
  == EXAMPLE:
10
10
 
11
- === FDF creation
11
+ === FDF/XFdf creation
12
12
 
13
13
  require 'pdf_forms'
14
14
  fdf = PdfForms::Fdf.new :key => 'value', :other_key => 'other value'
15
- # use to_fdf if you just want the fdf data, without writing it to a file
16
- puts fdf.to_fdf
15
+ # use to_pdf_data if you just want the fdf data, without writing it to a file
16
+ puts fdf.to_pdf_data
17
17
  # write fdf file
18
18
  fdf.save_to 'path/to/file.fdf'
19
19
 
20
+ # To generate XFDF instead of FDF instantiate PdfForms::XFdf instead of PdfForms::Fdf
21
+
20
22
  === Query form fields and fill out PDF forms with pdftk
21
23
 
22
24
  First get pdftk from http://www.accesspdf.com/pdftk/ and install it.
23
25
 
24
26
  require 'pdf_forms'
25
-
27
+
26
28
  # adjust the pdftk path to suit your pdftk installation
29
+ # add :data_format => 'XFdf' option to generate XFDF instead of FDF when filling a form
27
30
  pdftk = PdfForms.new('/usr/local/bin/pdftk')
28
-
31
+
32
+ add :data_format => 'XFdf' option to generate XFDF instead of FDF when filling a form
33
+
29
34
  # find out the field names that are present in form.pdf
30
35
  pdftk.get_field_names 'path/to/form.pdf'
31
-
36
+
32
37
  # take form.pdf, set the 'foo' field to 'bar' and save the document to myform.pdf
33
38
  pdftk.fill_form '/path/to/form.pdf', 'myform.pdf', :foo => 'bar'
34
-
35
-
39
+
40
+
36
41
 
37
42
  == INSTALL:
38
43
 
@@ -48,4 +53,4 @@ The FDF generation part is a straight port of Steffen Schwigon's PDF::FDF::Simpl
48
53
 
49
54
  == LICENSE:
50
55
 
51
- see LICENSE
56
+ see LICENSE
data/lib/pdf_forms.rb CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
  require 'pdf_forms/version'
4
4
  require 'pdf_forms/safe_path'
5
+ require 'pdf_forms/data_format'
5
6
  require 'pdf_forms/fdf'
7
+ require 'pdf_forms/xfdf'
6
8
  require 'pdf_forms/pdf'
7
9
  require 'pdf_forms/pdftk_wrapper'
8
10
 
@@ -0,0 +1,46 @@
1
+ # coding UTF-8
2
+
3
+ module PdfForms
4
+ class DataFormat
5
+ attr_reader :options
6
+
7
+ def initialize(data = {}, options = {})
8
+ @data = data
9
+ @options = {
10
+ :file => nil,
11
+ :ufile => nil,
12
+ :id => nil
13
+ }.merge(options)
14
+ end
15
+
16
+ # generate PDF content in this data format
17
+ def to_pdf_data
18
+ pdf_data = header
19
+
20
+ @data.each do |key, value|
21
+ if Hash === value
22
+ value.each do |sub_key, sub_value|
23
+ pdf_data << field("#{key}_#{sub_key}", sub_value)
24
+ end
25
+ else
26
+ pdf_data << field(key, value)
27
+ end
28
+ end
29
+
30
+ pdf_data << footer
31
+ return encode_data(pdf_data)
32
+ end
33
+
34
+ alias_method :to_fdf, :to_pdf_data
35
+
36
+ # write fdf content to path
37
+ def save_to(path)
38
+ (File.open(path, 'wb') << to_fdf).close
39
+ end
40
+
41
+ protected
42
+ def encode_data(data)
43
+ data
44
+ end
45
+ end
46
+ end
data/lib/pdf_forms/fdf.rb CHANGED
@@ -6,34 +6,15 @@ module PdfForms
6
6
  #
7
7
  # Straight port of Perl's PDF::FDF::Simple by Steffen Schwigon.
8
8
  # Parsing FDF files is not supported (yet).
9
- class Fdf
10
-
11
- attr_reader :options
9
+ class Fdf < DataFormat
12
10
 
13
11
  def initialize(data = {}, options = {})
14
- @data = data
15
- @options = {
16
- :file => nil,
17
- :ufile => nil,
18
- :id => nil
19
- }.merge(options)
12
+ super
20
13
  end
21
14
 
22
- # generate FDF content
23
- def to_fdf
24
- fdf = header
25
-
26
- @data.each do |key, value|
27
- if Hash === value
28
- value.each do |sub_key, sub_value|
29
- fdf << field("#{key}_#{sub_key}", sub_value)
30
- end
31
- else
32
- fdf << field(key, value)
33
- end
34
- end
15
+ protected
35
16
 
36
- fdf << footer
17
+ def encode_data(fdf)
37
18
  # I have yet to see a version of pdftk which can handle UTF8 input,
38
19
  # so we convert to ISO-8859-15 here, replacing unknown / invalid chars
39
20
  # with the default replacement which is '?'.
@@ -43,18 +24,10 @@ module PdfForms
43
24
  else
44
25
  # pre 1.9
45
26
  require 'iconv'
46
- fdf = Iconv.conv('ISO-8859-15//IGNORE', 'utf-8', fdf)
27
+ Iconv.conv('ISO-8859-15//IGNORE', 'utf-8', fdf)
47
28
  end
48
- return fdf
49
29
  end
50
30
 
51
- # write fdf content to path
52
- def save_to(path)
53
- (File.open(path, 'wb') << to_fdf).close
54
- end
55
-
56
- protected
57
-
58
31
  def header
59
32
  header = "%FDF-1.2\n\n1 0 obj\n<<\n/FDF << /Fields 2 0 R"
60
33
 
@@ -23,7 +23,7 @@ module PdfForms
23
23
  def fill_form(template, destination, data = {})
24
24
  q_template = safe_path(template)
25
25
  q_destination = safe_path(destination)
26
- fdf = Fdf.new(data)
26
+ fdf = data_format(data)
27
27
  tmp = Tempfile.new('pdf_forms-fdf')
28
28
  tmp.close
29
29
  fdf.save_to tmp.path
@@ -72,6 +72,11 @@ module PdfForms
72
72
 
73
73
  protected
74
74
 
75
+ def data_format(data)
76
+ data_format = options[:data_format] || 'Fdf'
77
+ PdfForms.const_get(data_format).new(data)
78
+ end
79
+
75
80
  def pdftk_command(*args)
76
81
  quote_path(pdftk) + " #{args.flatten.compact.join ' '} 2>&1"
77
82
  end
@@ -87,8 +92,5 @@ module PdfForms
87
92
  end
88
93
  opt_args
89
94
  end
90
-
91
-
92
-
93
95
  end
94
96
  end
@@ -1,5 +1,5 @@
1
1
  # coding: UTF-8
2
2
 
3
3
  module PdfForms
4
- VERSION = '0.5.5'
4
+ VERSION = '0.5.6'
5
5
  end
@@ -0,0 +1,36 @@
1
+ # coding: UTF-8
2
+
3
+ module PdfForms
4
+ # Map keys and values to Adobe's XFDF format.
5
+ class XFdf < DataFormat
6
+ def initialize(data = {}, options = {})
7
+ super
8
+ end
9
+
10
+ protected
11
+
12
+ def encode_data(pdf_data)
13
+ pdf_data
14
+ end
15
+
16
+ def quote(value)
17
+ require 'rexml/document'
18
+ REXML::Text.new(value.to_s).to_s
19
+ end
20
+
21
+ def header
22
+ '<?xml version="1.0" encoding="UTF-8"?>
23
+ <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
24
+ <fields>
25
+ '
26
+ end
27
+
28
+ def field(key, value)
29
+ "<field name=\"#{key}\"><value>#{Array(value).map{ |v| quote(v) }.join(" ")}</value></field>"
30
+ end
31
+
32
+ def footer
33
+ "</fields></xfdf>"
34
+ end
35
+ end
36
+ end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdf-forms
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.5
5
- prerelease:
4
+ version: 0.5.6
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jens Krämer
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-05 00:00:00.000000000 Z
11
+ date: 2014-02-28 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Fill out PDF forms with pdftk (http://www.accesspdf.com/pdftk/).
15
14
  email:
@@ -19,37 +18,39 @@ extensions: []
19
18
  extra_rdoc_files: []
20
19
  files:
21
20
  - lib/pdf-forms.rb
22
- - lib/pdf_forms/fdf.rb
21
+ - lib/pdf_forms.rb
22
+ - lib/pdf_forms/safe_path.rb
23
23
  - lib/pdf_forms/field.rb
24
+ - lib/pdf_forms/xfdf.rb
24
25
  - lib/pdf_forms/pdf.rb
25
- - lib/pdf_forms/pdftk_wrapper.rb
26
- - lib/pdf_forms/safe_path.rb
26
+ - lib/pdf_forms/data_format.rb
27
27
  - lib/pdf_forms/version.rb
28
- - lib/pdf_forms.rb
28
+ - lib/pdf_forms/fdf.rb
29
+ - lib/pdf_forms/pdftk_wrapper.rb
29
30
  - LICENSE
30
31
  - README.rdoc
31
32
  homepage: http://github.com/jkraemer/pdf-forms
32
33
  licenses: []
34
+ metadata: {}
33
35
  post_install_message:
34
36
  rdoc_options: []
35
37
  require_paths:
36
38
  - lib
37
39
  required_ruby_version: !ruby/object:Gem::Requirement
38
- none: false
39
40
  requirements:
40
- - - ! '>='
41
+ - - '>='
41
42
  - !ruby/object:Gem::Version
42
43
  version: '0'
43
44
  required_rubygems_version: !ruby/object:Gem::Requirement
44
- none: false
45
45
  requirements:
46
- - - ! '>='
46
+ - - '>='
47
47
  - !ruby/object:Gem::Version
48
48
  version: 1.3.6
49
49
  requirements: []
50
50
  rubyforge_project: pdf-forms
51
- rubygems_version: 1.8.23
51
+ rubygems_version: 2.0.3
52
52
  signing_key:
53
- specification_version: 3
53
+ specification_version: 4
54
54
  summary: Fill out PDF forms with pdftk (http://www.accesspdf.com/pdftk/).
55
55
  test_files: []
56
+ has_rdoc: