pdf-forms 0.5.7 → 0.5.8
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/README.md +66 -0
- data/lib/pdf_forms/field.rb +8 -5
- data/lib/pdf_forms/pdf.rb +8 -3
- data/lib/pdf_forms/pdftk_wrapper.rb +11 -1
- data/lib/pdf_forms/version.rb +1 -1
- metadata +12 -12
- data/README.rdoc +0 -58
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 239cd68e0051c755b0197679386bbf0ce23f77da
|
4
|
+
data.tar.gz: 0fcd9c4ff9c7ae8f9adbe3d0ad7976cfcaa63b0b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d05df9821b1df134640b448ca9888b7e5926cef2fc7ad9193d26cc3d8579dd1c396e61c905ef7d326fdae4c3f70a0a52e7951d4ca54986469a80484fe3b2911d
|
7
|
+
data.tar.gz: e2a3c1880c92aecbf6a4532dc5bd437c5a20cf3a7cda5c31dab4c75b815d18cb56160bb1df0d7c3a1015889283d51a10d2176aa300b0941d248e360e5f7eba1e
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# pdf-forms [](https://travis-ci.org/jkraemer/pdf-forms)
|
2
|
+
|
3
|
+
http://github.com/jkraemer/pdf-forms/
|
4
|
+
|
5
|
+
## Description
|
6
|
+
|
7
|
+
Fill out PDF forms with [pdftk](http://www.pdflabs.com/tools/pdftk-server/).
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
You'll need a working `pdftk` binary. Either get a binary package from
|
12
|
+
http://www.pdflabs.com/tools/pdftk-server/ and install it, or run
|
13
|
+
`apt-get install pdftk` if you're on Debian or similar.
|
14
|
+
[Homebrew Cask](http://caskroom.io) also has a pftk formula.
|
15
|
+
|
16
|
+
After that, add `pdf-forms` to your Gemfile or manually install the gem. Nothing
|
17
|
+
unusual here.
|
18
|
+
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
### FDF/XFdf creation
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
require 'pdf_forms'
|
26
|
+
fdf # PdfForms::Fdf.new :key #> 'value', :other_key #> 'other value'
|
27
|
+
# use to_pdf_data if you just want the fdf data, without writing it to a file
|
28
|
+
puts fdf.to_pdf_data
|
29
|
+
# write fdf file
|
30
|
+
fdf.save_to 'path/to/file.fdf'
|
31
|
+
```
|
32
|
+
|
33
|
+
To generate XFDF instead of FDF instantiate `PdfForms::XFdf` instead of `PdfForms::Fdf`
|
34
|
+
|
35
|
+
### Query form fields and fill out PDF forms with pdftk
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
require 'pdf_forms'
|
39
|
+
|
40
|
+
# adjust the pdftk path to suit your pdftk installation
|
41
|
+
# add :data_format #> 'XFdf' option to generate XFDF instead of FDF when
|
42
|
+
# filling a form (XFDF is supposed to have better support for non-western
|
43
|
+
# encodings)
|
44
|
+
# add :utf8_fields #> true in order to get UTF8 encoded field metadata (this
|
45
|
+
# will use dump_data_fields_utf8 instead of dump_data_fields in the call to
|
46
|
+
# pdftk)
|
47
|
+
pdftk # PdfForms.new('/usr/local/bin/pdftk')
|
48
|
+
|
49
|
+
# find out the field names that are present in form.pdf
|
50
|
+
pdftk.get_field_names 'path/to/form.pdf'
|
51
|
+
|
52
|
+
# take form.pdf, set the 'foo' field to 'bar' and save the document to myform.pdf
|
53
|
+
pdftk.fill_form '/path/to/form.pdf', 'myform.pdf', :foo #> 'bar'
|
54
|
+
|
55
|
+
# optionally, add the :flatten option to prevent editing of a filled out form
|
56
|
+
pdftk.fill_form '/path/to/form.pdf', 'myform.pdf', {:foo #> 'bar'}, :flatten #> true
|
57
|
+
```
|
58
|
+
|
59
|
+
### Prior Art
|
60
|
+
|
61
|
+
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
|
+
|
63
|
+
## License
|
64
|
+
|
65
|
+
Created by [Jens Kraemer](http://jkraemer.net/) and licensed under the MIT Liense.
|
66
|
+
|
data/lib/pdf_forms/field.rb
CHANGED
@@ -17,12 +17,15 @@ module PdfForms
|
|
17
17
|
when /FieldStateOption:\s*(.*?)\s*$/
|
18
18
|
(@options ||= []) << $1
|
19
19
|
else
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
20
|
+
if match = line.match(/^\s*(?<key>[^:]+):\s*(?<value>.*)$/)
|
21
|
+
key = match[:key].to_s.strip
|
22
|
+
value = match[:value].to_s
|
23
|
+
var_name = key.gsub(/Field/, '').downcase
|
24
|
+
unless self.respond_to?(var_name)
|
25
|
+
self.class.send(:define_method, var_name.to_sym, Proc.new{ instance_variable_get("@#{var_name}".to_sym) } ) # in case new or unknown fields crop up...
|
26
|
+
end
|
27
|
+
instance_variable_set("@#{key.gsub(/Field/, '').downcase}".to_sym, value)
|
24
28
|
end
|
25
|
-
instance_variable_set("@#{key.gsub(/Field/, '').downcase}".to_sym, value)
|
26
29
|
end
|
27
30
|
end
|
28
31
|
end
|
data/lib/pdf_forms/pdf.rb
CHANGED
@@ -7,15 +7,19 @@ module PdfForms
|
|
7
7
|
|
8
8
|
include SafePath
|
9
9
|
|
10
|
-
attr_reader :path
|
10
|
+
attr_reader :path, :options
|
11
11
|
|
12
|
-
def initialize(path, pdftk)
|
12
|
+
def initialize(path, pdftk, options = {})
|
13
|
+
@options = options
|
13
14
|
@path = file_path(path)
|
14
15
|
raise IOError unless File.readable?(@path)
|
15
16
|
@pdftk = pdftk
|
16
17
|
end
|
17
18
|
|
18
19
|
# list of field objects for all defined fields
|
20
|
+
#
|
21
|
+
# Initialize the object with utf8_fields: true to get utf8 encoded field
|
22
|
+
# names.
|
19
23
|
def fields
|
20
24
|
@fields ||= read_fields
|
21
25
|
end
|
@@ -28,7 +32,8 @@ module PdfForms
|
|
28
32
|
protected
|
29
33
|
|
30
34
|
def read_fields
|
31
|
-
|
35
|
+
dump_method = options[:utf8_fields] ? 'dump_data_fields_utf8' : 'dump_data_fields'
|
36
|
+
field_output = @pdftk.call_pdftk quote_path(path), dump_method
|
32
37
|
@fields = field_output.split(/^---\n/).map do |field_text|
|
33
38
|
Field.new field_text if field_text =~ /FieldName:/
|
34
39
|
end.compact
|
@@ -27,6 +27,7 @@ module PdfForms
|
|
27
27
|
tmp = Tempfile.new('pdf_forms-fdf')
|
28
28
|
tmp.close
|
29
29
|
fdf.save_to tmp.path
|
30
|
+
fill_options = {:tmp_path => tmp.path}.merge(fill_options)
|
30
31
|
command = pdftk_command q_template, 'fill_form', safe_path(tmp.path), 'output', q_destination, add_options(fill_options)
|
31
32
|
output = %x{#{command}}
|
32
33
|
unless File.readable?(destination) && File.size(destination) > 0
|
@@ -46,13 +47,21 @@ module PdfForms
|
|
46
47
|
# pdftk.read '/path/to/form.pdf'
|
47
48
|
# returns an instance of PdfForms::Pdf representing the given template
|
48
49
|
def read(path)
|
49
|
-
Pdf.new path, self
|
50
|
+
Pdf.new path, self, options
|
50
51
|
end
|
51
52
|
|
53
|
+
# Get field metadata for template
|
54
|
+
#
|
55
|
+
# Initialize the object with utf8_fields: true to get utf8 encoded field
|
56
|
+
# metadata.
|
52
57
|
def get_fields(template)
|
53
58
|
read(template).fields
|
54
59
|
end
|
55
60
|
|
61
|
+
# get field names for template
|
62
|
+
#
|
63
|
+
# Initialize the object with utf8_fields: true to get utf8 encoded field
|
64
|
+
# names.
|
56
65
|
def get_field_names(template)
|
57
66
|
read(template).fields.map{|f| f.name}
|
58
67
|
end
|
@@ -93,6 +102,7 @@ module PdfForms
|
|
93
102
|
end
|
94
103
|
if option_or_global(:encrypt, local_options)
|
95
104
|
encrypt_pass = option_or_global(:encrypt_password, local_options)
|
105
|
+
encrypt_pass ||= option_or_global(:tmp_path, local_options)
|
96
106
|
encrypt_options = option_or_global(:encrypt_options, local_options)
|
97
107
|
opt_args.concat ['encrypt_128bit', 'owner_pw', encrypt_pass, encrypt_options]
|
98
108
|
end
|
data/lib/pdf_forms/version.rb
CHANGED
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: 0.5.
|
4
|
+
version: 0.5.8
|
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: 2014-
|
11
|
+
date: 2014-08-07 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A Ruby frontend to the pdftk binary, including FDF and XFDF creation.
|
14
14
|
Just pass your template and a hash of data to fill in.
|
@@ -18,18 +18,18 @@ executables: []
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
+
- LICENSE
|
22
|
+
- README.md
|
21
23
|
- lib/pdf-forms.rb
|
22
24
|
- lib/pdf_forms.rb
|
23
|
-
- lib/pdf_forms/safe_path.rb
|
24
|
-
- lib/pdf_forms/field.rb
|
25
|
-
- lib/pdf_forms/xfdf.rb
|
26
|
-
- lib/pdf_forms/pdf.rb
|
27
25
|
- lib/pdf_forms/data_format.rb
|
28
|
-
- lib/pdf_forms/version.rb
|
29
26
|
- lib/pdf_forms/fdf.rb
|
27
|
+
- lib/pdf_forms/field.rb
|
28
|
+
- lib/pdf_forms/pdf.rb
|
30
29
|
- lib/pdf_forms/pdftk_wrapper.rb
|
31
|
-
-
|
32
|
-
-
|
30
|
+
- lib/pdf_forms/safe_path.rb
|
31
|
+
- lib/pdf_forms/version.rb
|
32
|
+
- lib/pdf_forms/xfdf.rb
|
33
33
|
homepage: http://github.com/jkraemer/pdf-forms
|
34
34
|
licenses:
|
35
35
|
- MIT
|
@@ -40,17 +40,17 @@ require_paths:
|
|
40
40
|
- lib
|
41
41
|
required_ruby_version: !ruby/object:Gem::Requirement
|
42
42
|
requirements:
|
43
|
-
- -
|
43
|
+
- - ">="
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
46
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
47
|
requirements:
|
48
|
-
- -
|
48
|
+
- - ">="
|
49
49
|
- !ruby/object:Gem::Version
|
50
50
|
version: 1.3.6
|
51
51
|
requirements: []
|
52
52
|
rubyforge_project: pdf-forms
|
53
|
-
rubygems_version: 2.
|
53
|
+
rubygems_version: 2.2.2
|
54
54
|
signing_key:
|
55
55
|
specification_version: 4
|
56
56
|
summary: Fill out PDF forms with pdftk (http://www.accesspdf.com/pdftk/).
|
data/README.rdoc
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
= pdf-forms
|
2
|
-
http://github.com/jkraemer/pdf-forms/
|
3
|
-
by Jens Kraemer, jk@jkraemer.net
|
4
|
-
|
5
|
-
== DESCRIPTION:
|
6
|
-
|
7
|
-
Fill out PDF forms with pdftk (http://www.accesspdf.com/pdftk/).
|
8
|
-
|
9
|
-
== EXAMPLE:
|
10
|
-
|
11
|
-
=== FDF/XFdf creation
|
12
|
-
|
13
|
-
require 'pdf_forms'
|
14
|
-
fdf = PdfForms::Fdf.new :key => 'value', :other_key => 'other value'
|
15
|
-
# use to_pdf_data if you just want the fdf data, without writing it to a file
|
16
|
-
puts fdf.to_pdf_data
|
17
|
-
# write fdf file
|
18
|
-
fdf.save_to 'path/to/file.fdf'
|
19
|
-
|
20
|
-
# To generate XFDF instead of FDF instantiate PdfForms::XFdf instead of PdfForms::Fdf
|
21
|
-
|
22
|
-
=== Query form fields and fill out PDF forms with pdftk
|
23
|
-
|
24
|
-
First get pdftk from http://www.accesspdf.com/pdftk/ and install it.
|
25
|
-
|
26
|
-
require 'pdf_forms'
|
27
|
-
|
28
|
-
# adjust the pdftk path to suit your pdftk installation
|
29
|
-
# add :data_format => 'XFdf' option to generate XFDF instead of FDF when filling a form
|
30
|
-
pdftk = PdfForms.new('/usr/local/bin/pdftk')
|
31
|
-
|
32
|
-
# find out the field names that are present in form.pdf
|
33
|
-
pdftk.get_field_names 'path/to/form.pdf'
|
34
|
-
|
35
|
-
# take form.pdf, set the 'foo' field to 'bar' and save the document to myform.pdf
|
36
|
-
pdftk.fill_form '/path/to/form.pdf', 'myform.pdf', :foo => 'bar'
|
37
|
-
|
38
|
-
# optionally, add the :flatten option to prevent editing of a filled out form
|
39
|
-
pdftk.fill_form '/path/to/form.pdf', 'myform.pdf', {:foo => 'bar'}, :flatten => true
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
== INSTALL:
|
45
|
-
|
46
|
-
$ gem install pdf-forms
|
47
|
-
|
48
|
-
== CODE:
|
49
|
-
|
50
|
-
$ git clone http://github.com/jkraemer/pdf-forms.git
|
51
|
-
|
52
|
-
== Prior Art
|
53
|
-
|
54
|
-
The FDF generation part is a straight port of Steffen Schwigon's PDF::FDF::Simple perl module. Didn't port the FDF parsing, though ;-)
|
55
|
-
|
56
|
-
== LICENSE:
|
57
|
-
|
58
|
-
see LICENSE
|