pdf-toolkit 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,84 @@
1
+ require 'test/unit'
2
+ require 'pdf/toolkit'
3
+ require 'tempfile'
4
+ # require 'active_record'
5
+ # require 'active_record/validations'
6
+ # require 'active_record/callbacks'
7
+ # require 'active_record/timestamp'
8
+
9
+ class PDF::Toolkit
10
+ end
11
+
12
+ class MyPDF < PDF::Toolkit
13
+ loot_active_record
14
+ self.record_timestamps = true
15
+ validates_presence_of :author
16
+ before_save do |object|
17
+ object.producer = "PDF::Toolkit Test"
18
+ end
19
+ self.default_permissions = %w(Printing ModifyAnnotations)
20
+ end
21
+
22
+ class PDFToolkitActiveRecordTest < Test::Unit::TestCase
23
+ DATA = File.open(File.join(File.dirname(__FILE__),"pdfs","blank.pdf")).read
24
+
25
+ def setup
26
+ @tempfiles = []
27
+ @pdf = new_pdf
28
+ @pdftk = MyPDF.new(@pdf)
29
+ end
30
+
31
+ def teardown
32
+ (@tempfiles || []).each do |tempfile|
33
+ tempfile.close
34
+ tempfile.unlink
35
+ end
36
+ @tempfiles = nil
37
+ end
38
+
39
+ def new_pdf
40
+ tempfile = Tempfile.open("pdftktest")
41
+ tempfile.write(DATA)
42
+ tempfile.close
43
+ @tempfiles << tempfile if @tempfiles
44
+ return tempfile
45
+ end
46
+
47
+ def new_tempfile
48
+ tempfile = Tempfile.open("pdftktest")
49
+ tempfile.close
50
+ @tempfiles << tempfile if @tempfiles
51
+ return tempfile.path
52
+ end
53
+
54
+ def test_invalid
55
+ PDF::Toolkit.loot_active_record # Ensure a double loot is harmless
56
+ assert_nil @pdftk.author
57
+ assert_equal false, @pdftk.valid?
58
+ assert_nil @pdftk.save_as(new_tempfile)
59
+ end
60
+
61
+ def test_save
62
+ @pdftk.author = "John Smith"
63
+ assert_equal true, @pdftk.valid?
64
+ assert @pdftk.save
65
+ end
66
+
67
+ def test_callback
68
+ @pdftk.author = "Andy Hunt"
69
+ assert @pdftk.save
70
+ @pdftk.reload
71
+ assert_match /PDF::Toolkit/, @pdftk.producer
72
+ end
73
+
74
+ def test_timestamp
75
+ before = Time.now
76
+ @pdftk.author = "Dave Thomas"
77
+ assert @pdftk.save
78
+ @pdftk.reload
79
+ after = Time.now
80
+ assert @pdftk.updated_at >= before-1 && @pdftk.updated_at <= after,
81
+ "Timestamp (#{@pdftk.updated_at}) out of range"
82
+ end
83
+
84
+ end
@@ -0,0 +1,84 @@
1
+ require 'test/unit'
2
+ require 'pdf/toolkit'
3
+ require 'tempfile'
4
+ # require 'active_record'
5
+ require 'active_record/validations'
6
+ # require 'active_record/callbacks'
7
+ # require 'active_record/timestamp'
8
+
9
+ class PDF::Toolkit
10
+ end
11
+
12
+ class MyPDF < PDF::Toolkit
13
+ loot_active_record
14
+ self.record_timestamps = true
15
+ validates_presence_of :author
16
+ before_save do |object|
17
+ object.producer = "PDF::Toolkit Test"
18
+ end
19
+ self.default_permissions = %w(Printing ModifyAnnotations)
20
+ end
21
+
22
+ class PDFToolkitActiveRecordTest < Test::Unit::TestCase
23
+ DATA = File.open(File.join(File.dirname(__FILE__),"pdfs","blank.pdf")).read
24
+
25
+ def setup
26
+ @tempfiles = []
27
+ @pdf = new_pdf
28
+ @pdftk = MyPDF.new(@pdf)
29
+ end
30
+
31
+ def teardown
32
+ (@tempfiles || []).each do |tempfile|
33
+ tempfile.close
34
+ tempfile.unlink
35
+ end
36
+ @tempfiles = nil
37
+ end
38
+
39
+ def new_pdf
40
+ tempfile = Tempfile.open("pdftktest")
41
+ tempfile.write(DATA)
42
+ tempfile.close
43
+ @tempfiles << tempfile if @tempfiles
44
+ return tempfile
45
+ end
46
+
47
+ def new_tempfile
48
+ tempfile = Tempfile.open("pdftktest")
49
+ tempfile.close
50
+ @tempfiles << tempfile if @tempfiles
51
+ return tempfile.path
52
+ end
53
+
54
+ def test_invalid
55
+ PDF::Toolkit.loot_active_record # Ensure a double loot is harmless
56
+ assert_nil @pdftk.author
57
+ assert_equal false, @pdftk.valid?
58
+ assert_nil @pdftk.save_as(new_tempfile)
59
+ end
60
+
61
+ def test_save
62
+ @pdftk.author = "John Smith"
63
+ assert_equal true, @pdftk.valid?
64
+ assert @pdftk.save
65
+ end
66
+
67
+ def test_callback
68
+ @pdftk.author = "Andy Hunt"
69
+ assert @pdftk.save
70
+ @pdftk.reload
71
+ assert_match /PDF::Toolkit/, @pdftk.producer
72
+ end
73
+
74
+ def test_timestamp
75
+ before = Time.now
76
+ @pdftk.author = "Dave Thomas"
77
+ assert @pdftk.save
78
+ @pdftk.reload
79
+ after = Time.now
80
+ assert @pdftk.updated_at >= before-1 && @pdftk.updated_at <= after,
81
+ "Timestamp (#{@pdftk.updated_at}) out of range"
82
+ end
83
+
84
+ end
@@ -0,0 +1,158 @@
1
+ require 'test/unit'
2
+ require 'pdf/toolkit'
3
+ require 'tempfile'
4
+
5
+ class MyPdfTool < PDF::Toolkit
6
+ def before_save
7
+ self.updated_at = Time.at(1111111111)
8
+ end
9
+ self.default_permissions = %w(Printing ModifyAnnotations)
10
+ end
11
+
12
+ class PDFToolkitTest < Test::Unit::TestCase
13
+ DATA = File.open(File.join(File.dirname(__FILE__),"pdfs","blank.pdf")).read
14
+
15
+ def setup
16
+ @tempfiles = []
17
+ @pdf = new_pdf
18
+ @pdftk = PDF::Toolkit.new(@pdf)
19
+ end
20
+
21
+ def teardown
22
+ (@tempfiles || []).each do |tempfile|
23
+ tempfile.close
24
+ tempfile.unlink
25
+ end
26
+ @tempfiles = nil
27
+ end
28
+
29
+ def new_pdf
30
+ tempfile = Tempfile.open("pdftktest")
31
+ tempfile.write(DATA)
32
+ tempfile.close
33
+ @tempfiles << tempfile if @tempfiles
34
+ return tempfile
35
+ end
36
+
37
+ def new_pdftool
38
+ PDF::Toolkit.open(new_pdf.path)
39
+ end
40
+
41
+ def new_tempfile
42
+ tempfile = Tempfile.open("pdftktest")
43
+ tempfile.close
44
+ @tempfiles << tempfile if @tempfiles
45
+ return tempfile.path
46
+ end
47
+
48
+ def assert_reload(object = nil)
49
+ object ||= @pdftk
50
+ assert_nothing_raised { object.save!; object.reload }
51
+ end
52
+
53
+ def test_reload
54
+ pdftk2 = PDF::Toolkit.open(@pdftk.path)
55
+ pdftk2.keywords = "Ruby"
56
+ pdftk2.save
57
+ @pdftk.reload
58
+ assert_equal pdftk2.keywords, @pdftk.keywords
59
+ end
60
+
61
+ def test_keys
62
+ @pdftk["FakeKey"] = "Test"
63
+ assert_reload
64
+ assert_not_nil @pdftk["FakeKey"]
65
+ assert_equal @pdftk["FakeKey"], @pdftk[:fake_key]
66
+ assert_nil @pdftk["fake_key"]
67
+ end
68
+
69
+ def test_save_as
70
+ @pdftk.author = "John Doe"
71
+ assert @pdftk.save
72
+ @pdftk.author = "Jane Doe"
73
+ saved = @pdftk.save_as(new_tempfile)
74
+ assert_not_nil saved
75
+ @pdftk.reload
76
+ saved.reload
77
+ assert_equal "John Doe", @pdftk.author
78
+ assert_equal "Jane Doe", saved.author
79
+ end
80
+
81
+ def test_non_info
82
+ assert_equal :"1.4", @pdftk.version
83
+ assert_equal 1, @pdftk.page_count
84
+ assert_match /<PDF::Toolkit/, @pdftk.to_s
85
+ end
86
+
87
+ def test_date
88
+ time = Time.at(1234567890)
89
+ @pdftk.updated_at = time
90
+ @pdftk.created_at = time.utc
91
+ assert_reload
92
+ assert_equal time, @pdftk.updated_at
93
+ assert_equal time, @pdftk["ModDate"]
94
+ assert_equal time, @pdftk.created_at
95
+ end
96
+
97
+ def test_delete
98
+ assert_not_nil @pdftk.creator
99
+ @pdftk.delete(:creator)
100
+ assert_reload
101
+ assert_nil @pdftk.creator
102
+ end
103
+
104
+ def test_encrypt
105
+ @pdftk.owner_password = "chunky bacon"
106
+ @pdftk.author = "Why the Lucky Stiff"
107
+ assert @pdftk.save
108
+ pdftk = nil
109
+ assert_raise(PDF::Toolkit::ExecutionError){ PDF::Toolkit.open(@pdftk.path) }
110
+ assert_nothing_raised { pdftk = PDF::Toolkit.open(@pdftk.path,"chunky bacon") }
111
+ assert_not_nil pdftk.author
112
+ end
113
+
114
+ def test_autoloading
115
+ pdftk = PDF::Toolkit.new(@pdftk)
116
+ assert pdftk.has_key?(:creator), "Could not find creator"
117
+ end
118
+
119
+ def test_inheritable_attributes
120
+ old = PDF::Toolkit.default_permissions
121
+ PDF::Toolkit.default_permissions = ["AllFeatures"]
122
+ new_class = Class.new(PDF::Toolkit)
123
+ new_class.default_permissions = ["DegradedPrinting"]
124
+ assert_equal ["AllFeatures"], PDF::Toolkit.default_permissions
125
+ PDF::Toolkit.default_permissions = ["ModifyContents"]
126
+ new_class = Class.new(PDF::Toolkit)
127
+ assert_equal ["ModifyContents"], new_class.default_permissions
128
+ ensure
129
+ PDF::Toolkit.default_permissions = old
130
+ end
131
+
132
+ def test_inheritance
133
+ mypdf = MyPdfTool.open(@pdftk)
134
+ assert_nothing_raised { mypdf.save!; mypdf.reload }
135
+ assert_equal Time.at(1111111111), mypdf.updated_at
136
+ end
137
+
138
+ def test_to_text
139
+ text = @pdftk.to_text {|io|io.read}
140
+ assert_match /^\f\n?$/, text
141
+ end
142
+
143
+ def test_enumerable
144
+ assert_equal 4, @pdftk.keys.size
145
+ @pdftk.merge!(:author => "Why", 'Keywords' => "chunky backon")
146
+ assert_equal 6, @pdftk.keys.size
147
+ assert_equal true, @pdftk.has_key?('Author')
148
+ @pdftk.delete_if {|k,v| k.to_s =~ /Date/}
149
+ assert_equal 4, @pdftk.keys.size
150
+ assert_equal true, @pdftk.has_key?(:Creator)
151
+ @pdftk.delete(:creator)
152
+ assert_equal false, @pdftk.has_key?(:Creator)
153
+ @pdftk.delete(:Author)
154
+ assert_equal false, @pdftk.has_key?('Author')
155
+ assert_equal 2, @pdftk.keys.size
156
+ end
157
+
158
+ end
Binary file
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: pdf-toolkit
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.5.0
7
+ date: 2006-10-29 00:00:00 -05:00
8
+ summary: A wrapper around pdftk to allow PDF metadata manipulation
9
+ require_paths:
10
+ - lib
11
+ email: ruby@tpope.info
12
+ homepage: http://pdf-toolkit.rubyforge.org
13
+ rubyforge_project: pdf-toolkit
14
+ description: PDF::Toolkit provides a simple interface for querying and unpdation PDF metadata like the document Author and Title.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Tim Pope
31
+ files:
32
+ - Rakefile
33
+ - README
34
+ - setup.rb
35
+ - lib/pdf/toolkit.rb
36
+ - test/pdfs
37
+ - test/pdf_toolkit_test.rb
38
+ - test/pdf_toolkit_active_record_test.rb~
39
+ - test/pdf_toolkit_active_record_test.rb
40
+ - test/pdfs/blank.pdf
41
+ test_files: []
42
+
43
+ rdoc_options: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ requirements: []
52
+
53
+ dependencies:
54
+ - !ruby/object:Gem::Dependency
55
+ name: activesupport
56
+ version_requirement:
57
+ version_requirements: !ruby/object:Gem::Version::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.0.0
62
+ version: