pdf-forms 0.4.0 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pdf_forms.rb +2 -0
- data/lib/pdf_forms/pdf.rb +6 -3
- data/lib/pdf_forms/pdftk_wrapper.rb +13 -5
- data/lib/pdf_forms/safe_path.rb +22 -0
- data/lib/pdf_forms/version.rb +1 -1
- metadata +26 -4
data/lib/pdf_forms.rb
CHANGED
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
|
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
|
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
|
-
|
48
|
-
input =
|
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
|
-
|
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
|
data/lib/pdf_forms/version.rb
CHANGED
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.
|
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-
|
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
|