nguyen 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +9 -5
- data/lib/nguyen/pdftk_wrapper.rb +16 -4
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -14,7 +14,7 @@ Nguyên is a fork of Jens Krämer's pdf-forms with addition of filling forms wit
|
|
14
14
|
# use to_fdf if you just want the FDF data, without writing it to a file
|
15
15
|
puts fdf.to_fdf
|
16
16
|
# write fdf file
|
17
|
-
fdf.save_to
|
17
|
+
fdf.save_to('path/to/file.fdf')
|
18
18
|
|
19
19
|
|
20
20
|
=== XFDF creation
|
@@ -23,7 +23,7 @@ Nguyên is a fork of Jens Krämer's pdf-forms with addition of filling forms wit
|
|
23
23
|
# use to_xfdf if you just want the XFDF data, without writing it to a file
|
24
24
|
puts xfdf.to_xfdf
|
25
25
|
# write xfdf file
|
26
|
-
xfdf.save_to
|
26
|
+
xfdf.save_to('path/to/file.xfdf')
|
27
27
|
|
28
28
|
=== Query form fields and fill out PDF forms with pdftk
|
29
29
|
|
@@ -31,17 +31,17 @@ Nguyên is a fork of Jens Krämer's pdf-forms with addition of filling forms wit
|
|
31
31
|
pdftk = Nguyen.new('/usr/local/bin/pdftk')
|
32
32
|
|
33
33
|
# find out the field names that are present in form.pdf
|
34
|
-
pdftk.get_field_names
|
34
|
+
pdftk.get_field_names('path/to/form.pdf')
|
35
35
|
|
36
36
|
# take form.pdf, set the 'foo' field to 'bar' and save the document to myform.pdf
|
37
37
|
|
38
38
|
# with fdf
|
39
39
|
fdf = Nguyen::Fdf(:foo => 'bar')
|
40
|
-
pdftk.fill_form
|
40
|
+
pdftk.fill_form('/path/to/form.pdf', 'myform.pdf', fdf)
|
41
41
|
|
42
42
|
# with xfdf
|
43
43
|
xfdf = Nguyen::Xfdf(:foo => 'bar')
|
44
|
-
pdftk.fill_form
|
44
|
+
pdftk.fill_form('/path/to/form.pdf', 'myform.pdf', xfdf)
|
45
45
|
|
46
46
|
== INSTALL:
|
47
47
|
|
@@ -51,6 +51,10 @@ Nguyên is a fork of Jens Krämer's pdf-forms with addition of filling forms wit
|
|
51
51
|
|
52
52
|
$ git clone http://github.com/joneslee85/nguyen.git
|
53
53
|
|
54
|
+
== Contribution:
|
55
|
+
|
56
|
+
I greatly welcome contributions, please feel free to fork and submit pull request.
|
57
|
+
|
54
58
|
== LICENSE
|
55
59
|
|
56
60
|
see LICENSE
|
data/lib/nguyen/pdftk_wrapper.rb
CHANGED
@@ -8,7 +8,7 @@ module Nguyen
|
|
8
8
|
|
9
9
|
attr_reader :pdftk, :options
|
10
10
|
|
11
|
-
# PdftkWrapper.new('/usr/bin/pdftk', :encrypt => true, :encrypt_options => 'allow Printing')
|
11
|
+
# PdftkWrapper.new('/usr/bin/pdftk', :flatten => true, :encrypt => true, :encrypt_options => 'allow Printing')
|
12
12
|
def initialize(pdftk_path, options = {})
|
13
13
|
@pdftk = pdftk_path
|
14
14
|
@options = options
|
@@ -19,7 +19,7 @@ module Nguyen
|
|
19
19
|
tmp = Tempfile.new('pdf_forms-fdf')
|
20
20
|
tmp.close
|
21
21
|
form_data_format.save_to tmp.path
|
22
|
-
command = pdftk_command %Q("#{template}"), 'fill_form', tmp.path, 'output', destination,
|
22
|
+
command = pdftk_command %Q("#{template}"), 'fill_form', %Q("#{tmp.path}"), 'output', destination, add_options(tmp.path)
|
23
23
|
output = %x{#{command}}
|
24
24
|
unless File.readable?(destination) && File.size(destination) > 0
|
25
25
|
raise PdftkError.new("failed to fill form with command\n#{command}\ncommand output was:\n#{output}")
|
@@ -42,16 +42,28 @@ module Nguyen
|
|
42
42
|
%x{#{pdftk_command args}}
|
43
43
|
end
|
44
44
|
|
45
|
+
def cat(*files,output)
|
46
|
+
files = files[0] if files[0].class == Array
|
47
|
+
input = files.map{|f| %Q("#{f}")}
|
48
|
+
call_pdftk(*input,'output',output)
|
49
|
+
end
|
50
|
+
|
45
51
|
protected
|
46
52
|
|
47
53
|
def pdftk_command(*args)
|
48
54
|
"#{pdftk} #{args.flatten.compact.join ' '} 2>&1"
|
49
55
|
end
|
50
56
|
|
51
|
-
def
|
57
|
+
def add_options(pwd)
|
58
|
+
return if options.empty?
|
59
|
+
opt_args = []
|
60
|
+
if options[:flatten]
|
61
|
+
opt_args << 'flatten'
|
62
|
+
end
|
52
63
|
if options[:encrypt]
|
53
|
-
['encrypt_128bit', 'owner_pw', pwd, options[:encrypt_options]]
|
64
|
+
opt_args.concat ['encrypt_128bit', 'owner_pw', pwd, options[:encrypt_options]]
|
54
65
|
end
|
66
|
+
opt_args
|
55
67
|
end
|
56
68
|
|
57
69
|
end
|