nguyen 2.0.0 → 2.1.0
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.
- checksums.yaml +4 -4
- data/lib/nguyen/pdftk_wrapper.rb +26 -14
- data/lib/nguyen/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 74a1c2e3015f9f5338b2e3140a85f26bc7190d6884f5fcfcd22b76bf0e5ed23a
|
|
4
|
+
data.tar.gz: 9704aa598ac801cd59171aa081ca3c3d9b76d2dc3d9d0001ea33e9dceba9e217
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 22d0fe163418da9d6023d757d1a2a48ccd5dd976ee186d6c68e5a5965d8b49ddba1638e324e286dfeee536bd310e5ec506b62270a55fe33472119c15b6bde85f
|
|
7
|
+
data.tar.gz: 8b8328c35cd0a98be573d9dd50fb6ddb79e27b9275df57be34b682016ca7c85ecc7e88e5e966f36a99e62d2de4e107018e1927eec23b13aaf83a494c39b63b8e
|
data/lib/nguyen/pdftk_wrapper.rb
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
require 'tempfile'
|
|
2
|
+
require 'open3'
|
|
3
|
+
require 'shellwords'
|
|
2
4
|
module Nguyen
|
|
3
5
|
class PdftkError < StandardError
|
|
4
6
|
end
|
|
@@ -19,10 +21,10 @@ module Nguyen
|
|
|
19
21
|
tmp = Tempfile.new('pdf_forms-fdf')
|
|
20
22
|
tmp.close
|
|
21
23
|
form_data.respond_to?(:save_to) ? form_data.save_to(tmp.path) : File.write(tmp.path, form_data)
|
|
22
|
-
|
|
23
|
-
output =
|
|
24
|
+
args = [template, 'fill_form', tmp.path, 'output', destination, add_options(tmp.path)]
|
|
25
|
+
output = call_pdftk(*args)
|
|
24
26
|
unless File.readable?(destination) && File.size(destination) > 0
|
|
25
|
-
raise PdftkError.new("failed to fill form with command\n#{
|
|
27
|
+
raise PdftkError.new("failed to fill form with command\n#{describe_command(args)}\ncommand output was:\n#{output}")
|
|
26
28
|
end
|
|
27
29
|
ensure
|
|
28
30
|
tmp.unlink if tmp
|
|
@@ -50,28 +52,37 @@ module Nguyen
|
|
|
50
52
|
read(template).fields
|
|
51
53
|
end
|
|
52
54
|
|
|
55
|
+
# invokes pdftk with the given arguments and returns its output (stdout
|
|
56
|
+
# and stderr combined); arguments are passed to the process verbatim,
|
|
57
|
+
# without any shell involved, so paths need no quoting or escaping
|
|
53
58
|
def call_pdftk(*args)
|
|
54
|
-
|
|
59
|
+
output, _status = Open3.capture2e(pdftk, *normalize_args(args))
|
|
60
|
+
output
|
|
55
61
|
end
|
|
56
62
|
|
|
57
|
-
def cat(*files,output)
|
|
58
|
-
files = files
|
|
59
|
-
input = files.
|
|
60
|
-
call_pdftk(*input,'output',output)
|
|
63
|
+
def cat(*files, output)
|
|
64
|
+
files = files.flatten
|
|
65
|
+
input = files.flat_map { |f| f.to_s.include?('*') ? Dir.glob(f) : [f] }
|
|
66
|
+
call_pdftk(*input, 'output', output)
|
|
61
67
|
end
|
|
62
68
|
|
|
63
69
|
protected
|
|
64
70
|
|
|
65
71
|
def stamp_operation(operation, template, stamp_file, destination)
|
|
66
|
-
|
|
67
|
-
output =
|
|
72
|
+
args = [template, operation, stamp_file, 'output', destination]
|
|
73
|
+
output = call_pdftk(*args)
|
|
68
74
|
unless File.readable?(destination) && File.size(destination) > 0
|
|
69
|
-
raise PdftkError.new("failed to #{operation} with command\n#{
|
|
75
|
+
raise PdftkError.new("failed to #{operation} with command\n#{describe_command(args)}\ncommand output was:\n#{output}")
|
|
70
76
|
end
|
|
71
77
|
end
|
|
72
78
|
|
|
73
|
-
def
|
|
74
|
-
|
|
79
|
+
def normalize_args(args)
|
|
80
|
+
args.flatten.compact.map(&:to_s)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# human readable rendition of a pdftk invocation, for error messages
|
|
84
|
+
def describe_command(args)
|
|
85
|
+
Shellwords.join([pdftk, *normalize_args(args)])
|
|
75
86
|
end
|
|
76
87
|
|
|
77
88
|
def add_options(pwd)
|
|
@@ -81,7 +92,8 @@ module Nguyen
|
|
|
81
92
|
opt_args << 'flatten'
|
|
82
93
|
end
|
|
83
94
|
if options[:encrypt]
|
|
84
|
-
opt_args.concat ['encrypt_128bit', 'owner_pw', pwd
|
|
95
|
+
opt_args.concat ['encrypt_128bit', 'owner_pw', pwd]
|
|
96
|
+
opt_args.concat Shellwords.split(options[:encrypt_options]) if options[:encrypt_options]
|
|
85
97
|
end
|
|
86
98
|
opt_args
|
|
87
99
|
end
|
data/lib/nguyen/version.rb
CHANGED