pdf-forms 0.4.0 → 0.4.2

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/lib/pdf_forms.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  require 'pdf_forms/version'
2
+ require 'pdf_forms/safe_path'
2
3
  require 'pdf_forms/fdf'
3
4
  require 'pdf_forms/pdf'
4
5
  require 'pdf_forms/pdftk_wrapper'
5
6
 
7
+
6
8
  module PdfForms
7
9
  # shorthand for PdfForms::PdftkWrapper.new(...)
8
10
  def self.new(*args)
data/lib/pdf_forms/pdf.rb CHANGED
@@ -1,10 +1,13 @@
1
1
  module PdfForms
2
2
  class Pdf
3
+
4
+ include SafePath
5
+
3
6
  attr_reader :path
4
7
 
5
8
  def initialize(path, pdftk)
6
- @path = path
7
- raise IOError unless File.readable?(path)
9
+ @path = file_path(path)
10
+ raise IOError unless File.readable?(@path)
8
11
  @pdftk = pdftk
9
12
  end
10
13
 
@@ -15,7 +18,7 @@ module PdfForms
15
18
  protected
16
19
 
17
20
  def read_fields
18
- field_output = @pdftk.call_pdftk %Q("#{path}"), 'dump_data_fields'
21
+ field_output = @pdftk.call_pdftk quote_path(path), 'dump_data_fields'
19
22
  @fields = field_output.split(/^---\n/).map do |field_text|
20
23
  if field_text =~ /^FieldName: (\w+)$/
21
24
  $1
@@ -6,21 +6,25 @@ module PdfForms
6
6
  # Wraps calls to PdfTk
7
7
  class PdftkWrapper
8
8
 
9
+ include SafePath
10
+
9
11
  attr_reader :pdftk, :options
10
12
 
11
13
  # PdftkWrapper.new('/usr/bin/pdftk', :flatten => true, :encrypt => true, :encrypt_options => 'allow Printing')
12
14
  def initialize(pdftk_path, options = {})
13
- @pdftk = pdftk_path
15
+ @pdftk = file_path(pdftk_path)
14
16
  @options = options
15
17
  end
16
18
 
17
19
  # pdftk.fill_form '/path/to/form.pdf', '/path/to/destination.pdf', :field1 => 'value 1'
18
20
  def fill_form(template, destination, data = {})
21
+ q_template = safe_path(template)
22
+ q_destination = safe_path(destination)
19
23
  fdf = Fdf.new(data)
20
24
  tmp = Tempfile.new('pdf_forms-fdf')
21
25
  tmp.close
22
26
  fdf.save_to tmp.path
23
- command = pdftk_command %Q("#{template}"), 'fill_form', %Q("#{tmp.path}"), 'output', %Q("#{destination}"), add_options(tmp.path)
27
+ command = pdftk_command q_template, 'fill_form', safe_path(tmp.path), 'output', q_destination, add_options(tmp.path)
24
28
  output = %x{#{command}}
25
29
  unless File.readable?(destination) && File.size(destination) > 0
26
30
  raise PdftkError.new("failed to fill form with command\n#{command}\ncommand output was:\n#{output}")
@@ -44,15 +48,19 @@ module PdfForms
44
48
  end
45
49
 
46
50
  def cat(*files,output)
47
- files = files[0] if files[0].class == Array
48
- input = files.map{|f| %Q("#{f}")}
51
+ input_array, output_file = Array(files.flatten.compact), output
52
+ input = input_array.map{|path| safe_path(path)}
53
+ output = safe_path(output_file)
49
54
  call_pdftk(*input,'output',output)
50
55
  end
51
56
 
52
57
  protected
53
58
 
59
+
60
+
61
+
54
62
  def pdftk_command(*args)
55
- "#{pdftk} #{args.flatten.compact.join ' '} 2>&1"
63
+ quote_path(pdftk) + " #{args.flatten.compact.join ' '} 2>&1"
56
64
  end
57
65
 
58
66
  def add_options(pwd)
@@ -0,0 +1,22 @@
1
+ module PdfForms
2
+
3
+ module SafePath
4
+
5
+ def normalize_path(path)
6
+ fpath = file_path(path)
7
+ quote_path(fpath)
8
+ end
9
+
10
+ alias :safe_path :normalize_path
11
+
12
+ def quote_path(path)
13
+ %Q("#{path}")
14
+ end
15
+
16
+ def file_path(path)
17
+ path = path.to_path if path.respond_to? :to_path
18
+ path.to_str
19
+ end
20
+ end
21
+
22
+ end
@@ -1,3 +1,3 @@
1
1
  module PdfForms
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.2'
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: pdf-forms
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.0
5
+ version: 0.4.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Jens Kr\xC3\xA4mer"
@@ -10,9 +10,30 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-07-04 00:00:00 Z
14
- dependencies: []
15
-
13
+ date: 2012-08-02 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: pry
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: "0.0"
24
+ type: :development
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: pry-nav
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">"
33
+ - !ruby/object:Gem::Version
34
+ version: "0.0"
35
+ type: :development
36
+ version_requirements: *id002
16
37
  description: Fill out PDF forms with pdftk (http://www.accesspdf.com/pdftk/).
17
38
  email:
18
39
  - jk@jkraemer.net
@@ -26,6 +47,7 @@ files:
26
47
  - lib/pdf_forms/fdf.rb
27
48
  - lib/pdf_forms/pdf.rb
28
49
  - lib/pdf_forms/pdftk_wrapper.rb
50
+ - lib/pdf_forms/safe_path.rb
29
51
  - lib/pdf_forms/version.rb
30
52
  - lib/pdf_forms.rb
31
53
  - LICENSE