pdf-forms 1.1.1 → 1.4.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
- SHA1:
3
- metadata.gz: 7082ff050aa4f56aa3bfede64817eda71a98d5f7
4
- data.tar.gz: 54078c07d1d53e29858c56daf19ae3ce446ebcd3
2
+ SHA256:
3
+ metadata.gz: 6f7ed295a6b7127c97a398655de313fcce3950b3ea32b62b31ecafb72993315e
4
+ data.tar.gz: 432d25045b4cc7383aa2500cc66da6f7ca058be803965be1fab3dd202be1eff8
5
5
  SHA512:
6
- metadata.gz: ac501b1ee613c1f21a526bceb24331535eafb017e71d8935cb6a175f29d0123d64ad5b4aa4a5ba5770315e4b00e10dd806946695bbdadc306af0083bb620afe6
7
- data.tar.gz: 0d366788a6163b62f6c5e1b9e9d7a27ffc46898efb93067acc5f5b1fee675452395e5ad0671b6218344cd318dbf3a2b428fcb4479c9fb2377e9db5c2030bc261
6
+ metadata.gz: 439a63e18b1ee43256ca559f6fcd7a34faa9ac2db6ccb807ec91c2378c16bc5db840bb772d6ba59690cd856cb82e98bc814cdade458800dcb6744c3ace4aaaee
7
+ data.tar.gz: e6e7e593801b25ad4912e8d3eaf647d27f6ca1aad08ca8a170cee702c9d5781a3353f3fcc7af56f9f94c28a8bcb9b98c6320d9452f05e3c5fea17d86bbd51904
data/README.md CHANGED
@@ -15,6 +15,31 @@ http://www.pdflabs.com/tools/pdftk-server/ and install it, or run
15
15
  After that, add `pdf-forms` to your Gemfile or manually install the gem. Nothing
16
16
  unusual here.
17
17
 
18
+ ### Using the Java port of PDFTK
19
+
20
+ The PDFTK package was dropped from most (all?) current versions of major Linux distributions.
21
+ As contributed in [this issue](https://github.com/jkraemer/pdf-forms/issues/75#issuecomment-698436643), you can use the [Java version of PDFTK](https://gitlab.com/pdftk-java/pdftk)
22
+ with this gem, as well. Just create a small shell script:
23
+
24
+ ~~~shell
25
+ #!/bin/sh
26
+ MYSELF=`which "$0" 2>/dev/null`
27
+ [ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
28
+ java=java
29
+ if test -n "$JAVA_HOME"; then
30
+ java="$JAVA_HOME/bin/java"
31
+ fi
32
+ exec "$java" $java_args -jar $MYSELF "$@"
33
+ exit 1
34
+ ~~~
35
+
36
+ Next, concatenate the wrapper script and the Jar file, and you end up with an executable that
37
+ can be used with pdf-forms:
38
+
39
+ ~~~
40
+ cat stub.sh pdftk-all.jar > pdftk.run && chmod +x pdftk.run
41
+ ~~~
42
+
18
43
 
19
44
  ## Usage
20
45
 
@@ -40,6 +65,9 @@ require 'pdf_forms'
40
65
  # add :data_format => 'XFdf' option to generate XFDF instead of FDF when
41
66
  # filling a form (XFDF is supposed to have better support for non-western
42
67
  # encodings)
68
+ # add :data_format => 'FdfHex' option to generate FDF with values passed in
69
+ # UTF16 hexadecimal format (Hexadecimal format has also proven more reliable
70
+ # for passing latin accented characters to pdftk)
43
71
  # add :utf8_fields => true in order to get UTF8 encoded field metadata (this
44
72
  # will use dump_data_fields_utf8 instead of dump_data_fields in the call to
45
73
  # pdftk)
@@ -51,7 +79,8 @@ pdftk.get_field_names 'path/to/form.pdf'
51
79
  # take form.pdf, set the 'foo' field to 'bar' and save the document to myform.pdf
52
80
  pdftk.fill_form '/path/to/form.pdf', 'myform.pdf', :foo => 'bar'
53
81
 
54
- # optionally, add the :flatten option to prevent editing of a filled out form
82
+ # optionally, add the :flatten option to prevent editing of a filled out form.
83
+ # Other supported options are :drop_xfa and :drop_xmp.
55
84
  pdftk.fill_form '/path/to/form.pdf', 'myform.pdf', {:foo => 'bar'}, :flatten => true
56
85
 
57
86
  # to enable PDF encryption, pass encrypt: true. By default, a random 'owner
@@ -66,6 +95,21 @@ Any options shown above can also be set when initializing the PdfForms
66
95
  instance. In this case, options given to `fill_form` will override the global
67
96
  options.
68
97
 
98
+ ### Field names with HTML entities
99
+
100
+ In case your form's field names contain HTML entities (like
101
+ `Straße Hausnummer`), make sure you unescape those before using them, i.e.
102
+ `CGI.unescapeHTML(name)`. Thanks to @phoet for figuring this out in #65.
103
+
104
+ ### Non-ASCII Characters (UTF8 etc) are not displayed in the filled out PDF
105
+
106
+ First, check if the field value has been stored properly in the output PDF using `pdftk output.pdf dump_data_fields_utf8`.
107
+
108
+ If it has been stored but is not rendered, your input PDF lacks the proper font for your kind of characters. Re-create it and embed any necessary fonts.
109
+ If the value has not been stored, there is a problem with filling out the form, either on your side, of with this gem.
110
+
111
+ Also see [UTF-8 chars are not displayed in the filled PDF](https://github.com/jkraemer/pdf-forms/issues/53)
112
+
69
113
  ### Prior Art
70
114
 
71
115
  The FDF generation part is a straight port of Steffen Schwigon's PDF::FDF::Simple perl module. Didn't port the FDF parsing, though ;-)
data/lib/pdf_forms/fdf.rb CHANGED
@@ -28,6 +28,7 @@ module PdfForms
28
28
  end
29
29
  end
30
30
 
31
+ # pp 559 https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/pdf_reference_archives/PDFReference.pdf
31
32
  def header
32
33
  header = "%FDF-1.2\n\n1 0 obj\n<<\n/FDF << /Fields 2 0 R"
33
34
 
@@ -39,13 +40,16 @@ module PdfForms
39
40
  header << "/ID[" << options[:id].join << "]" if options[:id]
40
41
 
41
42
  header << ">>\n>>\nendobj\n2 0 obj\n["
42
- return header
43
+ header
43
44
  end
44
45
 
46
+ # pp 561 https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/pdf_reference_archives/PDFReference.pdf
45
47
  def field(key, value)
46
- "<</T(#{key})/V" +
47
- (Array === value ? "[#{value.map{ |v|"(#{quote(v)})" }.join}]" : "(#{quote(value)})") +
48
- ">>\n"
48
+ field = "<<"
49
+ field << "/T" + "(#{key})"
50
+ field << "/V" + (Array === value ? "[#{value.map{ |v|"(#{quote(v)})" }.join}]" : "(#{quote(value)})")
51
+ field << ">>\n"
52
+ field
49
53
  end
50
54
 
51
55
  def quote(value)
@@ -0,0 +1,39 @@
1
+ # coding: UTF-8
2
+
3
+ module PdfForms
4
+ # Map keys and values to Adobe's FDF format.
5
+ #
6
+ # This is a variation of the original Fdf data format, values are encoded in UTF16 hexadesimal
7
+ # notation to improve compatibility with non ascii charsets.
8
+ #
9
+ # Information about hexadesimal FDF values was found here:
10
+ #
11
+ # http://stackoverflow.com/questions/6047970/weird-characters-when-filling-pdf-with-pdftk
12
+ #
13
+ class FdfHex < Fdf
14
+ private
15
+
16
+ def field(key, value)
17
+ "<</T(#{key})/V" +
18
+ (Array === value ? encode_many(value) : encode_value_as_hex(value)) +
19
+ ">>\n"
20
+ end
21
+
22
+ def encode_many(values)
23
+ "[#{values.map { |v| encode_value_as_hex(v) }.join}]"
24
+ end
25
+
26
+ def encode_value_as_hex(value)
27
+ value = value.to_s
28
+ utf_16 = value.encode('UTF-16BE', :invalid => :replace, :undef => :replace)
29
+ hex = utf_16.unpack('H*').first
30
+ hex.force_encoding 'ASCII-8BIT' # jruby
31
+ '<FEFF' + hex.upcase + '>'
32
+ end
33
+
34
+ # Fdf implementation encodes to ISO-8859-15 which we do not want here.
35
+ def encode_data(fdf)
36
+ fdf
37
+ end
38
+ end
39
+ end
@@ -23,6 +23,9 @@ module PdfForms
23
23
  #
24
24
  # The pdftk binary may also be explecitly specified:
25
25
  # PdftkWrapper.new('/usr/bin/pdftk', :flatten => true, :encrypt => true, :encrypt_options => 'allow Printing')
26
+ #
27
+ # Besides the options shown above, the drop_xfa or drop_xmp options are
28
+ # also supported.
26
29
  def initialize(*args)
27
30
  pdftk, options = normalize_args *args
28
31
  @pdftk = Cliver.detect! pdftk
@@ -82,7 +85,7 @@ module PdfForms
82
85
  # returns the commands output, check general execution success with
83
86
  # $?.success?
84
87
  def call_pdftk(*args)
85
- SafeShell.execute pdftk, *(args.flatten)
88
+ SafeShell.execute! pdftk, *(args.flatten)
86
89
  end
87
90
 
88
91
  # concatenate documents, can optionally specify page ranges
@@ -133,11 +136,20 @@ module PdfForms
133
136
  local[attrib] || options[attrib]
134
137
  end
135
138
 
139
+ ALLOWED_OPTIONS = %i(
140
+ drop_xmp
141
+ drop_xfa
142
+ flatten
143
+ need_appearances
144
+ ).freeze
145
+
136
146
  def append_options(args, local_options = {})
137
147
  return args if options.empty? && local_options.empty?
138
148
  args = args.dup
139
- if option_or_global(:flatten, local_options)
140
- args << 'flatten'
149
+ ALLOWED_OPTIONS.each do |option|
150
+ if option_or_global(option, local_options)
151
+ args << option.to_s
152
+ end
141
153
  end
142
154
  if option_or_global(:encrypt, local_options)
143
155
  encrypt_pass = option_or_global(:encrypt_password, local_options)
@@ -160,11 +172,11 @@ module PdfForms
160
172
  when String
161
173
  pdftk = first_arg
162
174
  options = args.shift || {}
163
- raise InvalidArgumentError.new("expected hash, got #{options.class.name}") unless Hash === options
175
+ raise ArgumentError.new("expected hash, got #{options.class.name}") unless Hash === options
164
176
  when Hash
165
177
  options = first_arg
166
178
  else
167
- raise InvalidArgumentError.new("expected string or hash, got #{first_arg.class.name}")
179
+ raise ArgumentError.new("expected string or hash, got #{first_arg.class.name}")
168
180
  end
169
181
  end
170
182
  [pdftk, options]
@@ -1,5 +1,5 @@
1
1
  # coding: UTF-8
2
2
 
3
3
  module PdfForms
4
- VERSION = '1.1.1'
4
+ VERSION = '1.4.0'
5
5
  end
data/lib/pdf_forms.rb CHANGED
@@ -5,6 +5,7 @@ require 'pdf_forms/normalize_path'
5
5
  require 'pdf_forms/data_format'
6
6
  require 'pdf_forms/fdf'
7
7
  require 'pdf_forms/xfdf'
8
+ require 'pdf_forms/fdf_hex'
8
9
  require 'pdf_forms/pdf'
9
10
  require 'pdf_forms/pdftk_wrapper'
10
11
 
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.1.1
4
+ version: 1.4.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: 2016-08-30 00:00:00.000000000 Z
11
+ date: 2021-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cliver
@@ -50,30 +50,31 @@ dependencies:
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '1.7'
53
+ version: '2.1'
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
- version: '1.7'
60
+ version: '2.1'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: rake
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: '10.0'
67
+ version: '13.0'
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: '10.0'
74
+ version: '13.0'
75
75
  description: A Ruby frontend to the pdftk binary, including FDF and XFDF creation.
76
- Just pass your template and a hash of data to fill in.
76
+ Also works with the PDFTK Java port. Just pass your template and a hash of data
77
+ to fill in.
77
78
  email:
78
79
  - jk@jkraemer.net
79
80
  executables: []
@@ -86,6 +87,7 @@ files:
86
87
  - lib/pdf_forms.rb
87
88
  - lib/pdf_forms/data_format.rb
88
89
  - lib/pdf_forms/fdf.rb
90
+ - lib/pdf_forms/fdf_hex.rb
89
91
  - lib/pdf_forms/field.rb
90
92
  - lib/pdf_forms/normalize_path.rb
91
93
  - lib/pdf_forms/pdf.rb
@@ -111,8 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
113
  - !ruby/object:Gem::Version
112
114
  version: 1.3.6
113
115
  requirements: []
114
- rubyforge_project: pdf-forms
115
- rubygems_version: 2.5.1
116
+ rubygems_version: 3.0.3
116
117
  signing_key:
117
118
  specification_version: 4
118
119
  summary: Fill out PDF forms with pdftk (http://www.accesspdf.com/pdftk/).