pdf-forms 1.0.0 → 1.1.0

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: 74a388e3ee6551d601f4d713863ff18d8d71e7bc
4
- data.tar.gz: 28c603be2710e14695777208fa1b2219422c67ca
3
+ metadata.gz: f82a5f40a898d0b44980998389d2d65fccdf609c
4
+ data.tar.gz: 785b8dddce02826364296a277d070e9a9e513ba6
5
5
  SHA512:
6
- metadata.gz: e7ba4e9bca7cf23c37e431a646c766486a748a9836fb7d3e432ea7ecf814523d1932276f81159c02af298562fdf2653933c2a3c6be16a8d54496e9469d29238b
7
- data.tar.gz: b2e6c9002bcc6999c7b33ccf47eb82c22d3699bb709f6085f2b15b23dd0d4e94ad18e34eee14851f4cf0af5ddd031e779445bb6e1c57d207fd055ea12e64df1f
6
+ metadata.gz: 7d88e13704cb3190b22b64cf1b0cde1f94ab16b52e7ca6f95719f95a545a2859ede57509a8a0f93318ff4bd83994f97cb3ce537a327f34e9312725ca373b8643
7
+ data.tar.gz: 273c23ad22d6321829f45c2e0a1b2a568ea8faecbaf95359569b1b9011b14e6d082809052731ca9cf0adfc22439edc3a661973ca8575e91a71a9e7ac7e2774d6
data/README.md CHANGED
@@ -11,7 +11,6 @@ Fill out PDF forms with [pdftk](http://www.pdflabs.com/tools/pdftk-server/).
11
11
  You'll need a working `pdftk` binary. Either get a binary package from
12
12
  http://www.pdflabs.com/tools/pdftk-server/ and install it, or run
13
13
  `apt-get install pdftk` if you're on Debian or similar.
14
- [Homebrew Cask](http://caskroom.io) also has a pftk formula.
15
14
 
16
15
  After that, add `pdf-forms` to your Gemfile or manually install the gem. Nothing
17
16
  unusual here.
@@ -54,13 +53,24 @@ pdftk.fill_form '/path/to/form.pdf', 'myform.pdf', :foo => 'bar'
54
53
 
55
54
  # optionally, add the :flatten option to prevent editing of a filled out form
56
55
  pdftk.fill_form '/path/to/form.pdf', 'myform.pdf', {:foo => 'bar'}, :flatten => true
56
+
57
+ # to enable PDF encryption, pass encrypt: true. By default, a random 'owner
58
+ # password' will be used, but you can also set one with the :encrypt_pw option.
59
+ pdftk.fill_form '/path/to/form.pdf', 'myform.pdf', {foo: 'bar'}, encrypt: true, encrypt_options: 'allow printing'
60
+
61
+ # you can also protect the PDF even from opening by specifying an additional user_pw option:
62
+ pdftk.fill_form '/path/to/form.pdf', 'myform.pdf', {foo: 'bar'}, encrypt: true, encrypt_options: 'user_pw secret'
57
63
  ```
58
64
 
65
+ Any options shown above can also be set when initializing the PdfForms
66
+ instance. In this case, options given to `fill_form` will override the global
67
+ options.
68
+
59
69
  ### Prior Art
60
70
 
61
71
  The FDF generation part is a straight port of Steffen Schwigon's PDF::FDF::Simple perl module. Didn't port the FDF parsing, though ;-)
62
72
 
63
73
  ## License
64
74
 
65
- Created by [Jens Kraemer](http://jkraemer.net/) and licensed under the MIT Liense.
75
+ Created by [Jens Kraemer](http://jkraemer.net/) and licensed under the MIT License.
66
76
 
@@ -9,35 +9,45 @@ module PdfForms
9
9
  # FieldStateOption: Ja
10
10
  # FieldStateOption: Off
11
11
  #
12
- # Represenation of a PDF Form Field
12
+ # Representation of a PDF Form Field
13
13
  def initialize(field_description)
14
+ last_value = nil
14
15
  field_description.each_line do |line|
15
- case line
16
- when /FieldStateOption:\s*(.*?)\s*$/
17
- (@options ||= []) << $1
18
- else
19
- line.strip!
20
- key, value = line.split(": ")
21
- key.gsub!(/Field/, "")
22
- key = key.split(/(?=[A-Z])/).map(&:downcase).join('_').split(":")[0]
23
-
24
- instance_variable_set("@#{key}", value)
25
-
26
- # dynamically add in fields that we didn't anticipate in ATTRS
27
- unless self.respond_to?(key.to_sym)
28
- proc = Proc.new { instance_variable_get("@#{key}".to_sym) }
29
- self.class.send(:define_method, key.to_sym, proc)
16
+ line.chomp!
17
+
18
+ if line =~ /^Field([A-Za-z]+):\s+(.*)/
19
+ _, key, value = *$~
20
+
21
+ if key == 'StateOption'
22
+ (@options ||= []) << value
23
+
24
+ else
25
+ value.chomp!
26
+ last_value = value
27
+ key = key.split(/(?=[A-Z])/).map(&:downcase).join('_')
28
+ instance_variable_set("@#{key}", value)
29
+
30
+ # dynamically add in fields that we didn't anticipate in ATTRS
31
+ unless self.respond_to?(key.to_sym)
32
+ proc = Proc.new { instance_variable_get("@#{key}".to_sym) }
33
+ self.class.send(:define_method, key.to_sym, proc)
34
+ end
30
35
  end
36
+
37
+ else
38
+ # pdftk returns a line that doesn't start with "Field"
39
+ # It happens when a text field has multiple lines
40
+ last_value << "\n" << line
31
41
  end
32
42
  end
33
43
  end
34
-
44
+
35
45
  def to_hash
36
46
  hash = {}
37
47
  ATTRS.each do |attribute|
38
48
  hash[attribute] = self.send(attribute)
39
49
  end
40
-
50
+
41
51
  hash
42
52
  end
43
53
 
@@ -5,8 +5,7 @@ module PdfForms
5
5
  module NormalizePath
6
6
 
7
7
  def normalize_path(path)
8
- path = path.to_path if path.respond_to? :to_path
9
- path.to_str
8
+ Pathname(path).to_path
10
9
  end
11
10
 
12
11
  end
@@ -3,6 +3,7 @@
3
3
  require 'tempfile'
4
4
  require 'cliver'
5
5
  require 'safe_shell'
6
+ require 'securerandom'
6
7
 
7
8
  module PdfForms
8
9
  class PdftkError < StandardError
@@ -40,8 +41,7 @@ module PdfForms
40
41
  fill_options = {:tmp_path => tmp.path}.merge(fill_options)
41
42
 
42
43
  args = [ q_template, 'fill_form', normalize_path(tmp.path), 'output', q_destination ]
43
- append_options args, fill_options
44
- result = call_pdftk *args
44
+ result = call_pdftk(*(append_options(args, fill_options)))
45
45
 
46
46
  unless File.readable?(destination) && File.size(destination) > 0
47
47
  fdf_path = nil
@@ -85,13 +85,28 @@ module PdfForms
85
85
  SafeShell.execute pdftk, *(args.flatten)
86
86
  end
87
87
 
88
- # concatenate documents
88
+ # concatenate documents, can optionally specify page ranges
89
89
  #
90
- # args: in_file1, in_file2, ... , in_file_n, output
90
+ # args: in_file1, {in_file2 => ["1-2", "4-10"]}, ... , in_file_n, output
91
91
  def cat(*args)
92
- arguments = args.flatten.compact.map{|path| normalize_path(path)}
93
- output = arguments.pop
94
- call_pdftk arguments, 'output', output
92
+ in_files = []
93
+ page_ranges = []
94
+ file_handle = "A"
95
+ output = normalize_path args.pop
96
+
97
+ args.flatten.compact.each do |in_file|
98
+ if in_file.is_a? Hash
99
+ path = in_file.keys.first
100
+ page_ranges.push *in_file.values.first.map {|range| "#{file_handle}#{range}"}
101
+ else
102
+ path = in_file
103
+ page_ranges.push "#{file_handle}"
104
+ end
105
+ in_files.push "#{file_handle}=#{normalize_path(path)}"
106
+ file_handle.next!
107
+ end
108
+
109
+ call_pdftk in_files, 'cat', page_ranges, 'output', output
95
110
  end
96
111
 
97
112
  # stamp one pdf with another
@@ -119,16 +134,20 @@ module PdfForms
119
134
  end
120
135
 
121
136
  def append_options(args, local_options = {})
122
- return if options.empty? && local_options.empty?
137
+ return args if options.empty? && local_options.empty?
138
+ args = args.dup
123
139
  if option_or_global(:flatten, local_options)
124
140
  args << 'flatten'
125
141
  end
126
142
  if option_or_global(:encrypt, local_options)
127
143
  encrypt_pass = option_or_global(:encrypt_password, local_options)
128
- encrypt_pass ||= option_or_global(:tmp_path, local_options)
144
+ encrypt_pass ||= SecureRandom.urlsafe_base64
129
145
  encrypt_options = option_or_global(:encrypt_options, local_options)
130
- args += ['encrypt_128bit', 'owner_pw', encrypt_pass, encrypt_options].flatten.compact
146
+ encrypt_options = encrypt_options.split if String === encrypt_options
147
+ args << ['encrypt_128bit', 'owner_pw', encrypt_pass, encrypt_options]
131
148
  end
149
+ args.flatten!
150
+ args.compact!
132
151
  args
133
152
  end
134
153
 
@@ -1,5 +1,5 @@
1
1
  # coding: UTF-8
2
2
 
3
3
  module PdfForms
4
- VERSION = '1.0.0'
4
+ VERSION = '1.1.0'
5
5
  end
@@ -1,5 +1,7 @@
1
1
  # coding: UTF-8
2
2
 
3
+ require 'rexml/document'
4
+
3
5
  module PdfForms
4
6
  # Map keys and values to Adobe's XFDF format.
5
7
  class XFdf < DataFormat
@@ -14,7 +16,6 @@ module PdfForms
14
16
  end
15
17
 
16
18
  def quote(value)
17
- require 'rexml/document'
18
19
  REXML::Text.new(value.to_s).to_s
19
20
  end
20
21
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pdf-forms
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Krämer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-30 00:00:00.000000000 Z
11
+ date: 2016-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cliver
@@ -48,14 +48,14 @@ dependencies:
48
48
  name: bundler
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - ">="
51
+ - - "~>"
52
52
  - !ruby/object:Gem::Version
53
53
  version: '1.7'
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
- - - ">="
58
+ - - "~>"
59
59
  - !ruby/object:Gem::Version
60
60
  version: '1.7'
61
61
  - !ruby/object:Gem::Dependency
@@ -112,9 +112,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  version: 1.3.6
113
113
  requirements: []
114
114
  rubyforge_project: pdf-forms
115
- rubygems_version: 2.4.5.1
115
+ rubygems_version: 2.5.1
116
116
  signing_key:
117
117
  specification_version: 4
118
118
  summary: Fill out PDF forms with pdftk (http://www.accesspdf.com/pdftk/).
119
119
  test_files: []
120
- has_rdoc: