nguyen 0.0.1

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2012 Trung Lê
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,56 @@
1
+ = Nguyên the PDF Field Merger
2
+
3
+ A very lightweight library that fill PDF forms using XFDF/FDF with pdftk
4
+
5
+ You could download pdftk at http://www.accesspdf.com/pdftk/
6
+
7
+ Nguyên is a fork of Jens Krämer's pdf-forms with addition of filling forms with XFDF feature.
8
+
9
+ == EXAMPLE:
10
+
11
+ === FDF creation
12
+
13
+ fdf = Nguyen::Fdf.new(:key => 'value', :other_key => 'other value')
14
+ # use to_fdf if you just want the FDF data, without writing it to a file
15
+ puts fdf.to_fdf
16
+ # write fdf file
17
+ fdf.save_to 'path/to/file.fdf'
18
+
19
+
20
+ === XFDF creation
21
+
22
+ xfdf = Nguyen::Xfdf.new(:key => 'value', :other_key => 'other value')
23
+ # use to_xfdf if you just want the XFDF data, without writing it to a file
24
+ puts xfdf.to_xfdf
25
+ # write xfdf file
26
+ xfdf.save_to 'path/to/file.xfdf'
27
+
28
+ === Query form fields and fill out PDF forms with pdftk
29
+
30
+ # adjust the pdftk path to suit your pdftk installation
31
+ pdftk = Nguyen.new('/usr/local/bin/pdftk')
32
+
33
+ # find out the field names that are present in form.pdf
34
+ pdftk.get_field_names 'path/to/form.pdf'
35
+
36
+ # take form.pdf, set the 'foo' field to 'bar' and save the document to myform.pdf
37
+
38
+ # with fdf
39
+ fdf = Nguyen::Fdf(:foo => 'bar')
40
+ pdftk.fill_form '/path/to/form.pdf', 'myform.pdf', fdf
41
+
42
+ # with xfdf
43
+ xfdf = Nguyen::Xfdf(:foo => 'bar')
44
+ pdftk.fill_form '/path/to/form.pdf', 'myform.pdf', xfdf
45
+
46
+ == INSTALL:
47
+
48
+ $ gem install nguyen
49
+
50
+ == Source Code:
51
+
52
+ $ git clone http://github.com/joneslee85/nguyen.git
53
+
54
+ == LICENSE
55
+
56
+ see LICENSE
@@ -0,0 +1,11 @@
1
+ require 'nguyen/fdf'
2
+ require 'nguyen/xfdf'
3
+ require 'nguyen/pdf'
4
+ require 'nguyen/pdftk_wrapper'
5
+
6
+ module Nguyen
7
+ # shorthand for Nguyen::PdftkWrapper.new(...)
8
+ def self.new(*args)
9
+ PdftkWrapper.new(*args)
10
+ end
11
+ end
@@ -0,0 +1,89 @@
1
+ module Nguyen
2
+
3
+ # Map keys and values to Adobe's FDF format.
4
+ #
5
+ # Straight port of Perl's PDF::FDF::Simple by Steffen Schwigon.
6
+ # Parsing FDF files is not supported (yet).
7
+ class Fdf
8
+
9
+ attr_reader :options
10
+
11
+ def initialize(data = {}, options = {})
12
+ @data = data
13
+ @options = {
14
+ :file => nil,
15
+ :ufile => nil,
16
+ :id => nil
17
+ }.merge(options)
18
+ end
19
+
20
+ # generate FDF content
21
+ def to_fdf
22
+ fdf = header
23
+
24
+ @data.each do |key, value|
25
+ if Hash === value
26
+ value.each do |sub_key, sub_value|
27
+ fdf << field("#{key}_#{sub_key}", sub_value)
28
+ end
29
+ else
30
+ fdf << field(key, value)
31
+ end
32
+ end
33
+
34
+ fdf << footer
35
+ return fdf
36
+ end
37
+
38
+ # write fdf content to path
39
+ def save_to(path)
40
+ (File.open(path, 'w') << to_fdf).close
41
+ end
42
+
43
+ protected
44
+
45
+ def header
46
+ header = "%FDF-1.2\n\n1 0 obj\n<<\n/FDF << /Fields 2 0 R"
47
+
48
+ # /F
49
+ header << "/F (#{options[:file]})" if options[:file]
50
+ # /UF
51
+ header << "/UF (#{options[:ufile]})" if options[:ufile]
52
+ # /ID
53
+ header << "/ID[" << options[:id].join << "]" if options[:id]
54
+
55
+ header << ">>\n>>\nendobj\n2 0 obj\n["
56
+ return header
57
+ end
58
+
59
+ def field(key, value)
60
+ "<</T(#{key})/V" +
61
+ (Array === value ? "[#{value.map{ |v|"(#{quote(v)})" }.join}]" : "(#{quote(value)})") +
62
+ ">>\n"
63
+ end
64
+
65
+ def quote(value)
66
+ value.to_s.strip.
67
+ gsub( /\\/, '\\' ).
68
+ gsub( /\(/, '\(' ).
69
+ gsub( /\)/, '\)' ).
70
+ gsub( /\n/, '\r' )
71
+ end
72
+
73
+ FOOTER =<<-EOFOOTER
74
+ ]
75
+ endobj
76
+ trailer
77
+ <<
78
+ /Root 1 0 R
79
+
80
+ >>
81
+ %%EOF
82
+ EOFOOTER
83
+
84
+ def footer
85
+ FOOTER
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,26 @@
1
+ module Nguyen
2
+ class Pdf
3
+ attr_reader :path
4
+
5
+ def initialize(path, pdftk)
6
+ @path = path
7
+ raise IOError unless File.readable?(path)
8
+ @pdftk = pdftk
9
+ end
10
+
11
+ def fields
12
+ @fields ||= read_fields
13
+ end
14
+
15
+ protected
16
+
17
+ def read_fields
18
+ field_output = @pdftk.call_pdftk %Q("#{path}"), 'dump_data_fields'
19
+ @fields = field_output.split(/^---\n/).map do |field_text|
20
+ if field_text =~ /^FieldName: (\w+)$/
21
+ $1
22
+ end
23
+ end.compact.uniq
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,58 @@
1
+ require 'tempfile'
2
+ module Nguyen
3
+ class PdftkError < StandardError
4
+ end
5
+
6
+ # Wraps calls to PdfTk
7
+ class PdftkWrapper
8
+
9
+ attr_reader :pdftk, :options
10
+
11
+ # PdftkWrapper.new('/usr/bin/pdftk', :encrypt => true, :encrypt_options => 'allow Printing')
12
+ def initialize(pdftk_path, options = {})
13
+ @pdftk = pdftk_path
14
+ @options = options
15
+ end
16
+
17
+ # pdftk.fill_form '/path/to/form.pdf', '/path/to/destination.pdf', xfdf_or_fdf_object
18
+ def fill_form(template, destination, form_data_format)
19
+ tmp = Tempfile.new('pdf_forms-fdf')
20
+ tmp.close
21
+ form_data_format.save_to tmp.path
22
+ command = pdftk_command %Q("#{template}"), 'fill_form', tmp.path, 'output', destination, 'flatten', encrypt_options(tmp.path)
23
+ output = %x{#{command}}
24
+ unless File.readable?(destination) && File.size(destination) > 0
25
+ raise PdftkError.new("failed to fill form with command\n#{command}\ncommand output was:\n#{output}")
26
+ end
27
+ ensure
28
+ tmp.unlink if tmp
29
+ end
30
+
31
+ # pdftk.read '/path/to/form.pdf'
32
+ # returns an instance of PdfForms::Pdf representing the given template
33
+ def read(path)
34
+ Pdf.new path, self
35
+ end
36
+
37
+ def get_field_names(template)
38
+ read(template).fields
39
+ end
40
+
41
+ def call_pdftk(*args)
42
+ %x{#{pdftk_command args}}
43
+ end
44
+
45
+ protected
46
+
47
+ def pdftk_command(*args)
48
+ "#{pdftk} #{args.flatten.compact.join ' '} 2>&1"
49
+ end
50
+
51
+ def encrypt_options(pwd)
52
+ if options[:encrypt]
53
+ ['encrypt_128bit', 'owner_pw', pwd, options[:encrypt_options]]
54
+ end
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,44 @@
1
+ require 'nokogiri'
2
+
3
+ module Nguyen
4
+ class Xfdf
5
+
6
+ attr_reader :options
7
+
8
+ def initialize(fields = {}, options = {})
9
+ @fields = fields
10
+ @options = {
11
+ :file => nil,
12
+ :id => nil
13
+ }.merge(options)
14
+ end
15
+
16
+ # generate XFDF content
17
+ def to_xfdf
18
+ builder = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |xml|
19
+ xml.xfdf('xmlns' => 'http://ns.adobe.com/xfdf/', 'xml:space' => 'preserve') {
20
+ xml.f(:href => options[:file]) if options[:file]
21
+ xml.ids(:original => options[:id], :modified => options[:id]) if options[:id]
22
+ xml.fields {
23
+ @fields.each do |field, value|
24
+ xml.field(:name => field) {
25
+ if value.is_a? Array
26
+ value.each { |item| xml.value(item.to_s) }
27
+ else
28
+ xml.value(value.to_s)
29
+ end
30
+ }
31
+ end
32
+ }
33
+ }
34
+ end
35
+ builder.to_xml
36
+ end
37
+
38
+ # write fdf content to path
39
+ def save_to(path)
40
+ (File.open(path, 'w') << to_xfdf).close
41
+ end
42
+
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nguyen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Trung Lê
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.5
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.5
30
+ description: Forms for Nguyên is Ruby library that could merge PDF fields by XFDF/FDF
31
+ via pdftk.
32
+ email: joneslee85@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/nguyen/fdf.rb
38
+ - lib/nguyen/pdf.rb
39
+ - lib/nguyen/pdftk_wrapper.rb
40
+ - lib/nguyen/xfdf.rb
41
+ - lib/nguyen.rb
42
+ - LICENSE
43
+ - README.rdoc
44
+ homepage: http://github.com/joneslee85/nguyen
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: 1.8.7
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.3.6
62
+ requirements:
63
+ - pdtk 1.44.1 or newer
64
+ rubyforge_project:
65
+ rubygems_version: 1.8.24
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Fill out PDF forms by XFDF/FDF via pdftk.
69
+ test_files: []