nguyen 1.0.3 → 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
2
  SHA256:
3
- metadata.gz: acfaf8065a261f6cc2b2f5b95887a48d450686b6529a315664982c13ecc0c8d0
4
- data.tar.gz: 99729f86c46f23fc9846f1af8c727da593066dc9df39c4283eceba5b6bd2bb68
3
+ metadata.gz: f506bb08df03fd015f7b9abac64a8788717daf5c3f3c7eaacc58f9c96590c7ef
4
+ data.tar.gz: 7f425f695f794dbddef3e7550e212e845b9cbb85cd6c5e121c9f066e3b1beb53
5
5
  SHA512:
6
- metadata.gz: 5d65ce412084256c66f6061c32301f4d48772aafd28b438049877191bb93d72592cc13b9727c46d445be4d7cb8d41fb1db2163ba22c1d03d3739fd0a64907d8f
7
- data.tar.gz: b8ebe36c4cbff4e667b38dad208c305035bfc18623b68bace90ff8753f0a6aa7fb203fcec0332a959b155166c9a646d3cee455bb9601cf26b2abeee9d85f7d07
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)
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
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
 
@@ -59,6 +62,19 @@ XML
59
62
  pdftk.fill_form('/path/to/form', 'myform.pdf', xfdf_string)
60
63
  ```
61
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')
76
+ ```
77
+
62
78
  ## INSTALL:
63
79
 
64
80
  gem install nguyen
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
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
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
@@ -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.3'
2
+ VERSION = '2.0.0'
3
3
  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.3
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: 2020-06-11 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,14 +15,14 @@ dependencies:
16
15
  requirements:
17
16
  - - "~>"
18
17
  - !ruby/object:Gem::Version
19
- version: '1.10'
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.10'
25
+ version: '1.16'
27
26
  - !ruby/object:Gem::Dependency
28
27
  name: minitest
29
28
  requirement: !ruby/object:Gem::Requirement
@@ -38,7 +37,7 @@ dependencies:
38
37
  - - ">="
39
38
  - !ruby/object:Gem::Version
40
39
  version: '0'
41
- description: Forms for Nguyên is Ruby library that could merge PDF fields by XFDF/FDF
40
+ description: Forms for Nguyễn is Ruby library that could merge PDF fields by XFDF/FDF
42
41
  via pdftk.
43
42
  email: trung.le@ruby-journal.com
44
43
  executables: []
@@ -58,7 +57,6 @@ homepage: http://github.com/ruby-journal/nguyen
58
57
  licenses:
59
58
  - MIT
60
59
  metadata: {}
61
- post_install_message:
62
60
  rdoc_options: []
63
61
  require_paths:
64
62
  - lib
@@ -66,7 +64,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
66
64
  requirements:
67
65
  - - ">="
68
66
  - !ruby/object:Gem::Version
69
- version: 2.5.0
67
+ version: 3.3.0
70
68
  required_rubygems_version: !ruby/object:Gem::Requirement
71
69
  requirements:
72
70
  - - ">="
@@ -74,8 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
72
  version: '0'
75
73
  requirements:
76
74
  - pdtk 1.44.1 or newer
77
- rubygems_version: 3.0.3
78
- signing_key:
75
+ rubygems_version: 4.0.10
79
76
  specification_version: 4
80
77
  summary: Fill out PDF forms by XFDF/FDF via pdftk.
81
78
  test_files: []