nguyen 1.0.2 → 2.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 87249f3df0276b081523efa08c46dd8feb43db2f
4
- data.tar.gz: d872b66be7d38fbd3f20f3a053b5cb6b260dda36
2
+ SHA256:
3
+ metadata.gz: f506bb08df03fd015f7b9abac64a8788717daf5c3f3c7eaacc58f9c96590c7ef
4
+ data.tar.gz: 7f425f695f794dbddef3e7550e212e845b9cbb85cd6c5e121c9f066e3b1beb53
5
5
  SHA512:
6
- metadata.gz: 0d1e6f777d4603591fc638e70af122cf1913c0aa7df534833062d653ba869bf8696b30a96e25658bc00f202dc5e241adae4acc76728629d117e2259db8bc78b2
7
- data.tar.gz: bef8d65f125f49319afb248023a9be1eda31a44f5679f101d2e22d619309ffe172376a37ed2d3ad64ca323c90a639b96a74702b9e3bc8f8ab3457de6252a0df6
6
+ metadata.gz: 0ed2625129d36a1175e10316e771591815b6f1ed71dd85c9d6a07c38ec237cc80dad4fd6df20c2fb6de4a165d067434bafb095ab1d93d70bc3c7a58fe3da0f07
7
+ data.tar.gz: 4ceb1deadd047804d440e31230eeb49eaca8ddd6be5cf2c4295b6dbedb6001ea7aa9c9e63cc7c4336baa20a7ccb7c7dd803cff847f5ef60128910b5236ec9135
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  (The MIT License)
2
2
 
3
- Copyright (c) 2014 Trung Lê
3
+ Copyright (c) 2014 - 2026 Trung Lê
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining
6
6
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,13 +1,16 @@
1
- # Nguyên the PDF Field Merger
1
+ # Nguyễn the PDF Field Merger
2
2
 
3
3
  A very lightweight library that fill PDF forms using XFDF/FDF with pdftk
4
4
 
5
5
  You could download pdftk at http://www.accesspdf.com/pdftk/
6
6
 
7
- Nguyên is a fork of Jens Krämer's pdf-forms with addition of filling forms with XFDF feature.
7
+ Nguyễn is a fork of Jens Krämer's pdf-forms with addition of filling forms with XFDF feature.
8
8
 
9
- [![Build Status](https://secure.travis-ci.org/ruby-journal/nguyen.png)](http://travis-ci.org/ruby-journal/nguyen)
10
- [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/ruby-journal/nguyen)
9
+ [![CI](https://github.com/ruby-journal/nguyen/actions/workflows/ci.yml/badge.svg)](https://github.com/ruby-journal/nguyen/actions/workflows/ci.yml)
10
+ [![Code Climate](https://codeclimate.com/github/ruby-journal/nguyen/badges/gpa.svg)](https://codeclimate.com/github/ruby-journal/nguyen)
11
+ [![From Vietnam with <3](https://raw.githubusercontent.com/webuild-community/badge/master/svg/love.svg)](https://webuild.community)
12
+ [![Made in Vietnam](https://raw.githubusercontent.com/webuild-community/badge/master/svg/made.svg)](https://webuild.community)
13
+ [![Built with WeBuild](https://raw.githubusercontent.com/webuild-community/badge/master/svg/WeBuild.svg)](https://webuild.community)
11
14
 
12
15
  ## EXAMPLE:
13
16
 
@@ -49,6 +52,27 @@ pdftk.fill_form('/path/to/form.pdf', 'myform.pdf', fdf)
49
52
  # with xfdf
50
53
  xfdf = Nguyen::Xfdf.new(foo: 'bar')
51
54
  pdftk.fill_form('/path/to/form.pdf', 'myform.pdf', xfdf)
55
+
56
+ # will work with xfdf or fdf xml string directly as well
57
+ xfdf_string = <<XML
58
+ <?xml version="1.0" encoding="UTF-8"?>
59
+ <xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve"><fields><field name="foo"><value>bar</value></field></fields></xfdf>
60
+ XML
61
+ # or xfdf_string = Nguyen::Xfdf.new(foo: 'bar').to_xfdf
62
+ pdftk.fill_form('/path/to/form', 'myform.pdf', xfdf_string)
63
+ ```
64
+
65
+ ### Stamp PDFs
66
+
67
+ ```ruby
68
+ pdftk = Nguyen::PdftkWrapper.new('/usr/local/bin/pdftk')
69
+
70
+ # overlay stamp.pdf (e.g. a watermark or signature) on top of every page of source.pdf
71
+ pdftk.stamp('source.pdf', 'stamp.pdf', 'stamped.pdf')
72
+
73
+ # overlay stamp.pdf page by page: page 1 of stamp.pdf onto page 1 of source.pdf, etc.
74
+ # the last page of stamp.pdf is repeated if source.pdf has more pages
75
+ pdftk.multistamp('source.pdf', 'stamp.pdf', 'stamped.pdf')
52
76
  ```
53
77
 
54
78
  ## INSTALL:
data/lib/nguyen/fdf.rb CHANGED
@@ -5,9 +5,9 @@ module Nguyen
5
5
  # Straight port of Perl's PDF::FDF::Simple by Steffen Schwigon.
6
6
  # Parsing FDF files is not supported (yet).
7
7
  class Fdf
8
-
8
+
9
9
  attr_reader :options
10
-
10
+
11
11
  def initialize(data = {}, options = {})
12
12
  @data = data
13
13
  @options = {
@@ -20,7 +20,7 @@ module Nguyen
20
20
  # generate FDF content
21
21
  def to_fdf
22
22
  fdf = header
23
-
23
+
24
24
  @data.each do |key, value|
25
25
  if Hash === value
26
26
  value.each do |sub_key, sub_value|
@@ -34,34 +34,42 @@ module Nguyen
34
34
  fdf << footer
35
35
  return fdf
36
36
  end
37
-
37
+
38
38
  # write fdf content to path
39
39
  def save_to(path)
40
- (File.open(path, 'w') << to_fdf).close
40
+ File.write(path, to_fdf)
41
41
  end
42
-
42
+
43
43
  protected
44
-
44
+
45
45
  def header
46
- header = "%FDF-1.2\n\n1 0 obj\n<<\n/FDF << /Fields 2 0 R"
47
-
46
+ header = +"%FDF-1.2\n\n1 0 obj\n<<\n/FDF << /Fields 2 0 R"
47
+
48
48
  # /F
49
49
  header << "/F (#{options[:file]})" if options[:file]
50
50
  # /UF
51
51
  header << "/UF (#{options[:ufile]})" if options[:ufile]
52
52
  # /ID
53
53
  header << "/ID[" << options[:id].join << "]" if options[:id]
54
-
54
+
55
55
  header << ">>\n>>\nendobj\n2 0 obj\n["
56
56
  return header
57
57
  end
58
58
 
59
59
  def field(key, value)
60
- "<</T(#{key})/V" +
61
- (Array === value ? "[#{value.map{ |v|"(#{quote(v)})" }.join}]" : "(#{quote(value)})") +
62
- ">>\n"
60
+ value_string = case value
61
+ when TrueClass
62
+ '/1'
63
+ when FalseClass
64
+ '/0'
65
+ when Array
66
+ '[' + value.map { |v| "(#{quote(v)})" }.join + ']'
67
+ else
68
+ "(#{quote(value)})"
69
+ end
70
+ "<<\n/T(#{key})\n/V #{value_string}\n>>\n"
63
71
  end
64
-
72
+
65
73
  def quote(value)
66
74
  value.to_s.strip.
67
75
  gsub( /\\/, '\\' ).
@@ -69,7 +77,7 @@ module Nguyen
69
77
  gsub( /\)/, '\)' ).
70
78
  gsub( /\n/, '\r' )
71
79
  end
72
-
80
+
73
81
  FOOTER =<<-EOFOOTER
74
82
  ]
75
83
  endobj
@@ -80,10 +88,10 @@ trailer
80
88
  >>
81
89
  %%EOF
82
90
  EOFOOTER
83
-
91
+
84
92
  def footer
85
93
  FOOTER
86
94
  end
87
-
95
+
88
96
  end
89
- end
97
+ end
data/lib/nguyen/pdf.rb CHANGED
@@ -14,13 +14,13 @@ module Nguyen
14
14
 
15
15
  protected
16
16
 
17
- def read_fields
18
- field_output = @pdftk.call_pdftk %Q("#{path}"), 'dump_data_fields'
19
- @fields = field_output.split(/^---\n/).map do |field_text|
20
- if field_text =~ /^FieldName: (\w+)$/
21
- $1
22
- end
23
- end.compact.uniq
24
- end
17
+ def read_fields
18
+ field_output = @pdftk.call_pdftk path, 'dump_data_fields_utf8'
19
+ @fields = field_output.split("\n").map do |field_text|
20
+ if field_text =~ /^FieldName: (.+)$/
21
+ $1
22
+ end
23
+ end.compact.uniq
24
+ end
25
25
  end
26
26
  end
@@ -14,11 +14,11 @@ module Nguyen
14
14
  @options = options
15
15
  end
16
16
 
17
- # pdftk.fill_form '/path/to/form.pdf', '/path/to/destination.pdf', xfdf_or_fdf_object
18
- def fill_form(template, destination, form_data_format)
17
+ # pdftk.fill_form '/path/to/form.pdf', '/path/to/destination.pdf', xfdf_or_fdf_object or xfdf_or_fdf_string
18
+ def fill_form(template, destination, form_data)
19
19
  tmp = Tempfile.new('pdf_forms-fdf')
20
20
  tmp.close
21
- form_data_format.save_to tmp.path
21
+ form_data.respond_to?(:save_to) ? form_data.save_to(tmp.path) : File.write(tmp.path, form_data)
22
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
@@ -28,6 +28,18 @@ module Nguyen
28
28
  tmp.unlink if tmp
29
29
  end
30
30
 
31
+ # pdftk.stamp '/path/to/source.pdf', '/path/to/stamp.pdf', '/path/to/destination.pdf'
32
+ # overlays the stamp PDF on top of every page of the source PDF
33
+ def stamp(template, stamp_file, destination)
34
+ stamp_operation 'stamp', template, stamp_file, destination
35
+ end
36
+
37
+ # pdftk.multistamp '/path/to/source.pdf', '/path/to/stamp.pdf', '/path/to/destination.pdf'
38
+ # overlays the stamp PDF onto the source PDF page by page
39
+ def multistamp(template, stamp_file, destination)
40
+ stamp_operation 'multistamp', template, stamp_file, destination
41
+ end
42
+
31
43
  # pdftk.read '/path/to/form.pdf'
32
44
  # returns an instance of Nguyen::Pdf representing the given template
33
45
  def read(path)
@@ -50,6 +62,14 @@ module Nguyen
50
62
 
51
63
  protected
52
64
 
65
+ def stamp_operation(operation, template, stamp_file, destination)
66
+ command = pdftk_command %Q("#{template}"), operation, %Q("#{stamp_file}"), 'output', %Q("#{destination}")
67
+ output = %x{#{command}}
68
+ unless File.readable?(destination) && File.size(destination) > 0
69
+ raise PdftkError.new("failed to #{operation} with command\n#{command}\ncommand output was:\n#{output}")
70
+ end
71
+ end
72
+
53
73
  def pdftk_command(*args)
54
74
  "#{pdftk} #{args.flatten.compact.join ' '} 2>&1"
55
75
  end
@@ -1,3 +1,3 @@
1
1
  module Nguyen
2
- VERSION = '1.0.2'
3
- end
2
+ VERSION = '2.0.0'
3
+ end
data/lib/nguyen/xfdf.rb CHANGED
@@ -37,8 +37,8 @@ module Nguyen
37
37
 
38
38
  # write fdf content to path
39
39
  def save_to(path)
40
- (File.open(path, 'w') << to_xfdf).close
40
+ File.write(path, to_xfdf)
41
41
  end
42
42
 
43
43
  end
44
- end
44
+ end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nguyen
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Trung Lê
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2014-04-24 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: nokogiri
@@ -16,15 +15,29 @@ dependencies:
16
15
  requirements:
17
16
  - - "~>"
18
17
  - !ruby/object:Gem::Version
19
- version: '1.5'
18
+ version: '1.16'
20
19
  type: :runtime
21
20
  prerelease: false
22
21
  version_requirements: !ruby/object:Gem::Requirement
23
22
  requirements:
24
23
  - - "~>"
25
24
  - !ruby/object:Gem::Version
26
- version: '1.5'
27
- description: Forms for Nguyên is Ruby library that could merge PDF fields by XFDF/FDF
25
+ version: '1.16'
26
+ - !ruby/object:Gem::Dependency
27
+ name: minitest
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ description: Forms for Nguyễn is Ruby library that could merge PDF fields by XFDF/FDF
28
41
  via pdftk.
29
42
  email: trung.le@ruby-journal.com
30
43
  executables: []
@@ -44,7 +57,6 @@ homepage: http://github.com/ruby-journal/nguyen
44
57
  licenses:
45
58
  - MIT
46
59
  metadata: {}
47
- post_install_message:
48
60
  rdoc_options: []
49
61
  require_paths:
50
62
  - lib
@@ -52,17 +64,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
52
64
  requirements:
53
65
  - - ">="
54
66
  - !ruby/object:Gem::Version
55
- version: 1.9.3
67
+ version: 3.3.0
56
68
  required_rubygems_version: !ruby/object:Gem::Requirement
57
69
  requirements:
58
70
  - - ">="
59
71
  - !ruby/object:Gem::Version
60
- version: 1.8.25
72
+ version: '0'
61
73
  requirements:
62
74
  - pdtk 1.44.1 or newer
63
- rubyforge_project:
64
- rubygems_version: 2.2.2
65
- signing_key:
75
+ rubygems_version: 4.0.10
66
76
  specification_version: 4
67
77
  summary: Fill out PDF forms by XFDF/FDF via pdftk.
68
78
  test_files: []