pdf-forms 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dafa2a5c9130fc6e580251237c45d7c423f4e178
4
- data.tar.gz: ade160fa891ee6d433f0d8baedb404eee2750d44
3
+ metadata.gz: 00aacbca71befeb06614093686dcf6e77de19a7a
4
+ data.tar.gz: 65ff4a6bc3c2e9ae17b79ec2b2796667ca1cfba6
5
5
  SHA512:
6
- metadata.gz: de0909cb12fc43557003d02b6fc972ee2fad1916da68b318d52df36b3fa5bb3aabd7055d6c0e9744d6f0f92c4c74a731cc87fc4e5a36de96773a27c8958620ed
7
- data.tar.gz: 8f0eadfcdd96afa187303644909c5db91de9c18771731aebd2a89cac98fa63ac13289b62c7e0677428879ad484a39098c1fd5ebb63f816b0295e7227b2e4a2bb
6
+ metadata.gz: 3ceacae28cd5ac5389144a712d1eab0ea26e1ab3bc7e13d4cc0502d193e8fe96f3f3cc48d1e9fbad8d6219c2e81160c7d8d5a9e9ec9ca4445c4523f6923926f9
7
+ data.tar.gz: 921095f8f6fb83ea26fc5af61b2c8e84f6ab57316c14433dc12c1af9a1fc2e38c3af982e6ede02a48955a05d3c057969deb8690241b9d7665028db41c3b572d3
data/lib/pdf_forms.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # coding: UTF-8
2
2
 
3
3
  require 'pdf_forms/version'
4
- require 'pdf_forms/safe_path'
4
+ require 'pdf_forms/normalize_path'
5
5
  require 'pdf_forms/data_format'
6
6
  require 'pdf_forms/fdf'
7
7
  require 'pdf_forms/xfdf'
@@ -38,7 +38,8 @@ module PdfForms
38
38
  (File.open(path, 'wb') << to_fdf).close
39
39
  end
40
40
 
41
- protected
41
+ private
42
+
42
43
  def encode_data(data)
43
44
  data
44
45
  end
data/lib/pdf_forms/fdf.rb CHANGED
@@ -12,7 +12,7 @@ module PdfForms
12
12
  super
13
13
  end
14
14
 
15
- protected
15
+ private
16
16
 
17
17
  def encode_data(fdf)
18
18
  # I have yet to see a version of pdftk which can handle UTF8 input,
@@ -49,10 +49,8 @@ module PdfForms
49
49
  end
50
50
 
51
51
  def quote(value)
52
- value.to_s.strip.
53
- gsub( /\\/, '\\' ).
54
- gsub( /\(/, '\(' ).
55
- gsub( /\)/, '\)' ).
52
+ value.to_s.
53
+ gsub( /(\\|\(|\))/ ) { '\\' + $1 }.
56
54
  gsub( /\n/, '\r' )
57
55
  end
58
56
 
@@ -0,0 +1,14 @@
1
+ # coding: UTF-8
2
+
3
+ module PdfForms
4
+
5
+ module NormalizePath
6
+
7
+ def normalize_path(path)
8
+ path = path.to_path if path.respond_to? :to_path
9
+ path.to_str
10
+ end
11
+
12
+ end
13
+
14
+ end
data/lib/pdf_forms/pdf.rb CHANGED
@@ -4,14 +4,13 @@ require 'pdf_forms/field'
4
4
 
5
5
  module PdfForms
6
6
  class Pdf
7
-
8
- include SafePath
7
+ include NormalizePath
9
8
 
10
9
  attr_reader :path, :options
11
10
 
12
11
  def initialize(path, pdftk, options = {})
13
12
  @options = options
14
- @path = file_path(path)
13
+ @path = normalize_path(path)
15
14
  raise IOError unless File.readable?(@path)
16
15
  @pdftk = pdftk
17
16
  end
@@ -29,11 +28,11 @@ module PdfForms
29
28
  fields.detect{ |f| f.name == name }
30
29
  end
31
30
 
32
- protected
31
+ private
33
32
 
34
33
  def read_fields
35
34
  dump_method = options[:utf8_fields] ? 'dump_data_fields_utf8' : 'dump_data_fields'
36
- field_output = @pdftk.call_pdftk quote_path(path), dump_method
35
+ field_output = @pdftk.call_pdftk path, dump_method
37
36
  @fields = field_output.split(/^---\n/).map do |field_text|
38
37
  Field.new field_text if field_text =~ /FieldName:/
39
38
  end.compact
@@ -1,6 +1,9 @@
1
1
  # coding: UTF-8
2
2
 
3
3
  require 'tempfile'
4
+ require 'cliver'
5
+ require 'safe_shell'
6
+
4
7
  module PdfForms
5
8
  class PdftkError < StandardError
6
9
  end
@@ -8,28 +11,38 @@ module PdfForms
8
11
  # Wraps calls to PdfTk
9
12
  class PdftkWrapper
10
13
 
11
- include SafePath
14
+ include NormalizePath
12
15
 
13
16
  attr_reader :pdftk, :options
14
17
 
18
+ PDFTK = 'pdftk'
19
+
20
+ # Initializes a new wrapper instance. Pdftk will be autodetected from PATH:
21
+ # PdftkWrapper.new(:flatten => true, :encrypt => true, :encrypt_options => 'allow Printing')
22
+ #
23
+ # The pdftk binary may also be explecitly specified:
15
24
  # PdftkWrapper.new('/usr/bin/pdftk', :flatten => true, :encrypt => true, :encrypt_options => 'allow Printing')
16
- def initialize(pdftk_path, options = {})
17
- @pdftk = file_path(pdftk_path)
18
- raise "pdftk executable #{@pdftk} not found" unless call_pdftk('-h') =~ /pdftk\s+\d/i
25
+ def initialize(*args)
26
+ pdftk, options = normalize_args *args
27
+ @pdftk = Cliver.detect! pdftk
28
+ raise "pdftk executable #{@pdftk} not found" unless call_pdftk('-h') && $?.success?
19
29
  @options = options
20
30
  end
21
31
 
22
32
  # pdftk.fill_form '/path/to/form.pdf', '/path/to/destination.pdf', :field1 => 'value 1'
23
33
  def fill_form(template, destination, data = {}, fill_options = {})
24
- q_template = safe_path(template)
25
- q_destination = safe_path(destination)
34
+ q_template = normalize_path(template)
35
+ q_destination = normalize_path(destination)
26
36
  fdf = data_format(data)
27
37
  tmp = Tempfile.new('pdf_forms-fdf')
28
38
  tmp.close
29
39
  fdf.save_to tmp.path
30
40
  fill_options = {:tmp_path => tmp.path}.merge(fill_options)
31
- command = pdftk_command q_template, 'fill_form', safe_path(tmp.path), 'output', q_destination, add_options(fill_options)
32
- output = %x{#{command}}
41
+
42
+ args = [ q_template, 'fill_form', normalize_path(tmp.path), 'output', q_destination ]
43
+ append_options args, fill_options
44
+ result = call_pdftk *args
45
+
33
46
  unless File.readable?(destination) && File.size(destination) > 0
34
47
  fdf_path = nil
35
48
  begin
@@ -38,7 +51,7 @@ module PdfForms
38
51
  rescue Exception
39
52
  fdf_path = "#{$!}\n#{$!.backtrace.join("\n")}"
40
53
  end
41
- raise PdftkError.new("failed to fill form with command\n#{command}\ncommand output was:\n#{output}\nfailing form data has been saved to #{fdf_path}")
54
+ raise PdftkError.new("failed to fill form with command\n#{pdftk} #{args.flatten.compact.join ' '}\ncommand output was:\n#{result}\nfailing form data has been saved to #{fdf_path}")
42
55
  end
43
56
  ensure
44
57
  tmp.unlink if tmp
@@ -66,47 +79,77 @@ module PdfForms
66
79
  read(template).fields.map{|f| f.name}
67
80
  end
68
81
 
82
+ # returns the commands output, check general execution success with
83
+ # $?.success?
69
84
  def call_pdftk(*args)
70
- %x{#{pdftk_command args}}
85
+ SafeShell.execute pdftk, *(args.flatten)
71
86
  end
72
87
 
73
88
  # concatenate documents
74
89
  #
75
90
  # args: in_file1, in_file2, ... , in_file_n, output
76
91
  def cat(*args)
77
- arguments = args.flatten.compact.map{|path| safe_path(path)}
92
+ arguments = args.flatten.compact.map{|path| normalize_path(path)}
78
93
  output = arguments.pop
79
- call_pdftk(*([arguments, 'output', output].flatten))
94
+ call_pdftk arguments, 'output', output
80
95
  end
81
96
 
82
- protected
97
+ # stamp one pdf with another
98
+ #
99
+ # args: primary_file, stamp_file, output
100
+ def stamp(primary_file, stamp_file, output)
101
+ call_pdftk primary_file, 'stamp', stamp_file, 'output', output
102
+ end
103
+
104
+ # applies each page of the stamp PDF to the corresponding page of the input PDF
105
+ # args: primary_file, stamp_file, output
106
+ def multistamp(primary_file, stamp_file, output)
107
+ call_pdftk primary_file, 'multistamp', stamp_file, 'output', output
108
+ end
109
+
110
+ private
83
111
 
84
112
  def data_format(data)
85
113
  data_format = options[:data_format] || 'Fdf'
86
114
  PdfForms.const_get(data_format).new(data)
87
115
  end
88
116
 
89
- def pdftk_command(*args)
90
- quote_path(pdftk) + " #{args.flatten.compact.join ' '} 2>&1"
91
- end
92
-
93
117
  def option_or_global(attrib, local = {})
94
118
  local[attrib] || options[attrib]
95
119
  end
96
120
 
97
- def add_options(local_options = {})
121
+ def append_options(args, local_options = {})
98
122
  return if options.empty? && local_options.empty?
99
- opt_args = []
100
123
  if option_or_global(:flatten, local_options)
101
- opt_args << 'flatten'
124
+ args << 'flatten'
102
125
  end
103
126
  if option_or_global(:encrypt, local_options)
104
127
  encrypt_pass = option_or_global(:encrypt_password, local_options)
105
128
  encrypt_pass ||= option_or_global(:tmp_path, local_options)
106
129
  encrypt_options = option_or_global(:encrypt_options, local_options)
107
- opt_args.concat ['encrypt_128bit', 'owner_pw', encrypt_pass, encrypt_options]
130
+ args += ['encrypt_128bit', 'owner_pw', encrypt_pass, encrypt_options].flatten.compact
131
+ end
132
+ args
133
+ end
134
+
135
+ def normalize_args(*args)
136
+ args = args.dup
137
+ pdftk = PDFTK
138
+ options = {}
139
+ if first_arg = args.shift
140
+ case first_arg
141
+ when String
142
+ pdftk = first_arg
143
+ options = args.shift || {}
144
+ raise InvalidArgumentError.new("expected hash, got #{options.class.name}") unless Hash === options
145
+ when Hash
146
+ options = first_arg
147
+ else
148
+ raise InvalidArgumentError.new("expected string or hash, got #{first_arg.class.name}")
149
+ end
108
150
  end
109
- opt_args
151
+ [pdftk, options]
110
152
  end
153
+
111
154
  end
112
155
  end
@@ -1,5 +1,5 @@
1
1
  # coding: UTF-8
2
2
 
3
3
  module PdfForms
4
- VERSION = '0.6.0'
4
+ VERSION = '0.6.1'
5
5
  end
@@ -7,7 +7,7 @@ module PdfForms
7
7
  super
8
8
  end
9
9
 
10
- protected
10
+ private
11
11
 
12
12
  def encode_data(pdf_data)
13
13
  pdf_data
metadata CHANGED
@@ -1,15 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdf-forms
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
- - Jens Krämer
7
+ - Jens Krämer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-14 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2015-08-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cliver
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.2
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.2
27
+ - !ruby/object:Gem::Dependency
28
+ name: safe_shell
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
13
69
  description: A Ruby frontend to the pdftk binary, including FDF and XFDF creation.
14
70
  Just pass your template and a hash of data to fill in.
15
71
  email:
@@ -25,9 +81,9 @@ files:
25
81
  - lib/pdf_forms/data_format.rb
26
82
  - lib/pdf_forms/fdf.rb
27
83
  - lib/pdf_forms/field.rb
84
+ - lib/pdf_forms/normalize_path.rb
28
85
  - lib/pdf_forms/pdf.rb
29
86
  - lib/pdf_forms/pdftk_wrapper.rb
30
- - lib/pdf_forms/safe_path.rb
31
87
  - lib/pdf_forms/version.rb
32
88
  - lib/pdf_forms/xfdf.rb
33
89
  homepage: http://github.com/jkraemer/pdf-forms
@@ -40,17 +96,17 @@ require_paths:
40
96
  - lib
41
97
  required_ruby_version: !ruby/object:Gem::Requirement
42
98
  requirements:
43
- - - '>='
99
+ - - ">="
44
100
  - !ruby/object:Gem::Version
45
101
  version: '0'
46
102
  required_rubygems_version: !ruby/object:Gem::Requirement
47
103
  requirements:
48
- - - '>='
104
+ - - ">="
49
105
  - !ruby/object:Gem::Version
50
106
  version: 1.3.6
51
107
  requirements: []
52
108
  rubyforge_project: pdf-forms
53
- rubygems_version: 2.2.2
109
+ rubygems_version: 2.4.5
54
110
  signing_key:
55
111
  specification_version: 4
56
112
  summary: Fill out PDF forms with pdftk (http://www.accesspdf.com/pdftk/).
@@ -1,24 +0,0 @@
1
- # coding: UTF-8
2
-
3
- module PdfForms
4
-
5
- module SafePath
6
-
7
- def normalize_path(path)
8
- fpath = file_path(path)
9
- quote_path(fpath)
10
- end
11
-
12
- alias :safe_path :normalize_path
13
-
14
- def quote_path(path)
15
- %Q("#{path}")
16
- end
17
-
18
- def file_path(path)
19
- path = path.to_path if path.respond_to? :to_path
20
- path.to_str
21
- end
22
- end
23
-
24
- end