pdfinfo 1.0.3 → 1.1.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 +4 -4
- data/lib/pdfinfo.rb +21 -13
- metadata +21 -28
- data/.gitignore +0 -24
- data/.rake_tasks~ +0 -5
- data/.rspec +0 -3
- data/.ruby-gemset +0 -1
- data/.ruby-version +0 -1
- data/Gemfile +0 -3
- data/LICENSE.txt +0 -22
- data/README.md +0 -92
- data/Rakefile +0 -8
- data/lib/pdfinfo/version.rb +0 -3
- data/lib/tasks/generate_pdf.rake +0 -56
- data/pdfinfo.gemspec +0 -24
- data/spec/fixtures/shell_responses/encrypted.txt +0 -15
- data/spec/fixtures/shell_responses/test.txt +0 -15
- data/spec/pdfinfo_spec.rb +0 -309
- data/spec/spec_helper.rb +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 23b45e11649bee36c2f385cb0306bfea02176fea
|
4
|
+
data.tar.gz: 787ac72eb44158aa770b988e27bb1b77056ce36b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11b841bf60de8835e2b9338781f07fa871f584f65c3849e1694bdc0fd46fcd626ec6a02160902f8516853b51f6cdb7930cb574f5b2119465f48d8fbfdaa8a75e
|
7
|
+
data.tar.gz: 21411e292115d76a446410ee38e6db9c4be9090bc972ed754d6e92aa18a113ceec1bf8d961c12f3f600a7ea4dac0a967eb5be8010420eb7a0b0f935e51bc9ecd
|
data/lib/pdfinfo.rb
CHANGED
@@ -4,6 +4,16 @@ require 'shellwords'
|
|
4
4
|
class Pdfinfo
|
5
5
|
DIMENSIONS_REGEXP = /([\d\.]+) x ([\d\.]+)/
|
6
6
|
|
7
|
+
class PdfinfoError < ::StandardError
|
8
|
+
end
|
9
|
+
|
10
|
+
class CommandNotFound < PdfinfoError
|
11
|
+
def initialize(command)
|
12
|
+
super("Command Not Found - '#{command}'")
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
7
17
|
attr_reader :title,
|
8
18
|
:subject,
|
9
19
|
:keywords,
|
@@ -20,14 +30,15 @@ class Pdfinfo
|
|
20
30
|
:pdf_version
|
21
31
|
|
22
32
|
def self.exec(file_path, opts = {})
|
33
|
+
raise CommandNotFound, 'pdfinfo' unless pdfinfo_command?
|
23
34
|
flags = []
|
24
35
|
flags.concat(['-enc', opts.fetch(:encoding, 'UTF-8')])
|
25
36
|
flags.concat(['-opw', opts[:owner_password]]) if opts[:owner_password]
|
26
37
|
flags.concat(['-upw', opts[:user_password]]) if opts[:user_password]
|
27
38
|
|
28
|
-
command = Shellwords.join([pdfinfo_command, *flags, file_path])
|
39
|
+
command = Shellwords.join([pdfinfo_command, *flags, file_path.to_s])
|
29
40
|
stdout, status = Open3.capture2(command)
|
30
|
-
stdout.encode('UTF-8', invalid: :replace, replace: '')
|
41
|
+
stdout.encode('UTF-8', invalid: :replace, replace: '', undef: :replace)
|
31
42
|
end
|
32
43
|
|
33
44
|
def self.pdfinfo_command
|
@@ -38,6 +49,10 @@ class Pdfinfo
|
|
38
49
|
@pdfinfo_command = cmd
|
39
50
|
end
|
40
51
|
|
52
|
+
def self.pdfinfo_command?
|
53
|
+
system("type #{pdfinfo_command} >/dev/null 2>&1")
|
54
|
+
end
|
55
|
+
|
41
56
|
def initialize(source_path, opts = {})
|
42
57
|
info_hash = parse_shell_response(Pdfinfo.exec(source_path, opts))
|
43
58
|
|
@@ -53,7 +68,7 @@ class Pdfinfo
|
|
53
68
|
@form = info_hash['Form']
|
54
69
|
@pdf_version = info_hash['PDF version']
|
55
70
|
|
56
|
-
@keywords = (info_hash['Keywords'] ||
|
71
|
+
@keywords = (info_hash['Keywords'] || '').split(/\s/)
|
57
72
|
@creation_date = presence(info_hash['CreationDate']) ? Time.parse(info_hash['CreationDate']) : nil
|
58
73
|
|
59
74
|
raw_usage_rights = Hash[info_hash['Encrypted'].scan(/(\w+):(\w+)/)]
|
@@ -77,16 +92,8 @@ class Pdfinfo
|
|
77
92
|
@encrypted
|
78
93
|
end
|
79
94
|
|
80
|
-
|
81
|
-
@usage_rights[
|
82
|
-
end
|
83
|
-
|
84
|
-
def copyable?
|
85
|
-
@usage_rights[:copy]
|
86
|
-
end
|
87
|
-
|
88
|
-
def changeable?
|
89
|
-
@usage_rights[:change]
|
95
|
+
%w(print copy change).each do |ur|
|
96
|
+
define_method("#{ur}able?") { @usage_rights[ur.to_sym] }
|
90
97
|
end
|
91
98
|
alias modifiable? changeable?
|
92
99
|
|
@@ -98,6 +105,7 @@ class Pdfinfo
|
|
98
105
|
def presence(val)
|
99
106
|
(val.nil? || val.empty?) ? nil : val
|
100
107
|
end
|
108
|
+
|
101
109
|
def parse_shell_response(response_str)
|
102
110
|
Hash[response_str.split(/\n/).map {|kv| kv.split(/:/, 2).map(&:strip) }]
|
103
111
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pdfinfo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Venegas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -66,31 +66,28 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: coveralls
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: Simple ruby wrapper around the pdfinfo executable
|
70
|
-
email:
|
71
|
-
- RVenegas2@Gmail.com
|
84
|
+
email: rvenegas2@gmail.com
|
72
85
|
executables: []
|
73
86
|
extensions: []
|
74
87
|
extra_rdoc_files: []
|
75
88
|
files:
|
76
|
-
- ".gitignore"
|
77
|
-
- ".rake_tasks~"
|
78
|
-
- ".rspec"
|
79
|
-
- ".ruby-gemset"
|
80
|
-
- ".ruby-version"
|
81
|
-
- Gemfile
|
82
|
-
- LICENSE.txt
|
83
|
-
- README.md
|
84
|
-
- Rakefile
|
85
89
|
- lib/pdfinfo.rb
|
86
|
-
|
87
|
-
- lib/tasks/generate_pdf.rake
|
88
|
-
- pdfinfo.gemspec
|
89
|
-
- spec/fixtures/shell_responses/encrypted.txt
|
90
|
-
- spec/fixtures/shell_responses/test.txt
|
91
|
-
- spec/pdfinfo_spec.rb
|
92
|
-
- spec/spec_helper.rb
|
93
|
-
homepage: ''
|
90
|
+
homepage: https://github.com/RyanV/pdfinfo
|
94
91
|
licenses:
|
95
92
|
- MIT
|
96
93
|
metadata: {}
|
@@ -102,7 +99,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
102
99
|
requirements:
|
103
100
|
- - ">="
|
104
101
|
- !ruby/object:Gem::Version
|
105
|
-
version:
|
102
|
+
version: 1.9.3
|
106
103
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
104
|
requirements:
|
108
105
|
- - ">="
|
@@ -110,12 +107,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
110
107
|
version: '0'
|
111
108
|
requirements: []
|
112
109
|
rubyforge_project:
|
113
|
-
rubygems_version: 2.
|
110
|
+
rubygems_version: 2.4.4
|
114
111
|
signing_key:
|
115
112
|
specification_version: 4
|
116
113
|
summary: Simple ruby wrapper around the pdfinfo executable
|
117
|
-
test_files:
|
118
|
-
- spec/fixtures/shell_responses/encrypted.txt
|
119
|
-
- spec/fixtures/shell_responses/test.txt
|
120
|
-
- spec/pdfinfo_spec.rb
|
121
|
-
- spec/spec_helper.rb
|
114
|
+
test_files: []
|
data/.gitignore
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
*.gem
|
2
|
-
*.rbc
|
3
|
-
.bundle
|
4
|
-
.config
|
5
|
-
.yardoc
|
6
|
-
Gemfile.lock
|
7
|
-
InstalledFiles
|
8
|
-
_yardoc
|
9
|
-
coverage
|
10
|
-
doc/
|
11
|
-
lib/bundler/man
|
12
|
-
pkg
|
13
|
-
rdoc
|
14
|
-
spec/reports
|
15
|
-
test/tmp
|
16
|
-
test/version_tmp
|
17
|
-
tmp
|
18
|
-
*.bundle
|
19
|
-
*.so
|
20
|
-
*.o
|
21
|
-
*.a
|
22
|
-
mkmf.log
|
23
|
-
.idea
|
24
|
-
spec/fixtures/pdfs/*
|
data/.rake_tasks~
DELETED
data/.rspec
DELETED
data/.ruby-gemset
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
pdfinfo
|
data/.ruby-version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
2.1.2
|
data/Gemfile
DELETED
data/LICENSE.txt
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Copyright (c) 2014 Ryan Venegas
|
2
|
-
|
3
|
-
MIT License
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
-
a copy of this software and associated documentation files (the
|
7
|
-
"Software"), to deal in the Software without restriction, including
|
8
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
-
permit persons to whom the Software is furnished to do so, subject to
|
11
|
-
the following conditions:
|
12
|
-
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
included in all copies or substantial portions of the Software.
|
15
|
-
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
DELETED
@@ -1,92 +0,0 @@
|
|
1
|
-
# Pdfinfo
|
2
|
-
|
3
|
-
Simple ruby wrapper around the pdfinfo command.
|
4
|
-
This gem was written and tested around xpdf version 3.04.
|
5
|
-
|
6
|
-
|
7
|
-
## Depdendecies
|
8
|
-
|
9
|
-
usage of this gem assumes that you have xpdf installed (which gives us access to the pdfinfo command). The fastest way to install xpdf:
|
10
|
-
|
11
|
-
$ brew install xpdf
|
12
|
-
|
13
|
-
## Installation
|
14
|
-
|
15
|
-
Add this line to your application's Gemfile:
|
16
|
-
|
17
|
-
gem 'pdfinfo', '~> 1.0.0'
|
18
|
-
|
19
|
-
And then execute:
|
20
|
-
|
21
|
-
$ bundle
|
22
|
-
|
23
|
-
Or install it yourself as:
|
24
|
-
|
25
|
-
$ gem install pdfinfo
|
26
|
-
|
27
|
-
## Usage
|
28
|
-
|
29
|
-
|
30
|
-
```ruby
|
31
|
-
pdfinfo = Pdfinfo.new("path/to/file.pdf")
|
32
|
-
|
33
|
-
pdfinfo.title #=> "Title" # or nil
|
34
|
-
pdfinfo.subject #=> "Subject" # or nil
|
35
|
-
pdfinfo.keywords #=> ["Keyword1", "Keyword2"] # or nil
|
36
|
-
pdfinfo.author #=> "Author Name" # or nil
|
37
|
-
pdfinfo.creator #=> "Creator Name" # or nil
|
38
|
-
pdfinfo.producer #=> "Producer Name" # or nil
|
39
|
-
ddfinfo.creation_date #=> 2014-10-26 20:50:45 -0700 # Time object
|
40
|
-
pdfinfo.form #=> "none"
|
41
|
-
pdfinfo.page_count #=> 3
|
42
|
-
pdfinfo.width #=> 612
|
43
|
-
pdfinfo.height #=> 792
|
44
|
-
pdfinfo.size #=> 1521 # file size in bytes
|
45
|
-
pdfinfo.pdf_version #=> "1.3"
|
46
|
-
pdfinfo.encrypted? #=> false # or true
|
47
|
-
pdfinfo.usage_rights #=> {print: true, copy: true, change: true, add_notes: true}
|
48
|
-
pdfinfo.printable? #=> true # or false
|
49
|
-
pdfinfo.copyable? #=> true # or false
|
50
|
-
pdfinfo.changeable? #=> true # or false
|
51
|
-
pdfinfo.modifiable? #=> true # or false. alias for #changeable?
|
52
|
-
pdfinfo.annotatable? #=> true # or false
|
53
|
-
pdfinfo.tagged? #=> false # or true
|
54
|
-
```
|
55
|
-
For encrypted files with a password you can pass in the user or owner password as options
|
56
|
-
|
57
|
-
```ruby
|
58
|
-
pdfinfo = Pdfinfo.new("path/to/encrypted.pdf", user_password: 'foo')
|
59
|
-
# pdfinfo = Pdfinfo.new("path/to/encrypted.pdf", owner_password: 'foo')
|
60
|
-
pdfinfo.encrypted? #=> true
|
61
|
-
pdfinfo.usage_rights #=> {print: false, copy: false, change: false, add_notes: false}
|
62
|
-
```
|
63
|
-
|
64
|
-
You can directly set the location of the executable if its not located in your environment $PATH or you just want to point to a different location.
|
65
|
-
|
66
|
-
```ruby
|
67
|
-
Pdfinfo.pdfinfo_command = '/another/bin/path/pdfinfo'
|
68
|
-
Pdfinfo.pdfinfo_command #=> '/another/bin/path/pdfinfo'
|
69
|
-
```
|
70
|
-
|
71
|
-
## Running specs
|
72
|
-
|
73
|
-
generate pdf fixtures by first running
|
74
|
-
|
75
|
-
$ rake generate_fixtures
|
76
|
-
|
77
|
-
Then run specs by running
|
78
|
-
|
79
|
-
$ rake
|
80
|
-
|
81
|
-
## Contributing
|
82
|
-
|
83
|
-
1. Fork it ( https://github.com/[my-github-username]/pdfinfo/fork )
|
84
|
-
2. Create your feature branch (`git checkout -b my-new-feature`)
|
85
|
-
3. Commit your changes (`git commit -am 'Add some feature'`)
|
86
|
-
4. Push to the branch (`git push origin my-new-feature`)
|
87
|
-
5. Create a new Pull Request
|
88
|
-
|
89
|
-
## TODO
|
90
|
-
* Error handling
|
91
|
-
* type coersion is getting messy in initialize. refactor.
|
92
|
-
* Add #to_hash/#to_h/#as_json method to output all metadata as a hash
|
data/Rakefile
DELETED
data/lib/pdfinfo/version.rb
DELETED
data/lib/tasks/generate_pdf.rake
DELETED
@@ -1,56 +0,0 @@
|
|
1
|
-
require 'time'
|
2
|
-
|
3
|
-
desc 'generates pdf for testing'
|
4
|
-
task :generate_fixtures do
|
5
|
-
class PdfGenerator
|
6
|
-
ROOT_DIR = File.expand_path("../../..", __FILE__)
|
7
|
-
METADATA_OPTIONS = {
|
8
|
-
Title: "Pdfinfo Title",
|
9
|
-
Author: "Pdfinfo Author",
|
10
|
-
Subject: "Pdfinfo Subject",
|
11
|
-
Keywords: "Keyword1 Keyword2",
|
12
|
-
Creator: "Pdfinfo Creator",
|
13
|
-
Producer: "Pdfinfo Producer",
|
14
|
-
CreationDate: Time.parse("2014-10-26 18:23:25 -0700")
|
15
|
-
}
|
16
|
-
ENCRYPTION_PERMISSIONS = {
|
17
|
-
print_document: false,
|
18
|
-
modify_contents: false,
|
19
|
-
copy_contents: false,
|
20
|
-
modify_annotations: false
|
21
|
-
}
|
22
|
-
ENCRYPTION_OPTIONS = {
|
23
|
-
user_password: 'foo',
|
24
|
-
owner_password: 'bar',
|
25
|
-
permissions: ENCRYPTION_PERMISSIONS
|
26
|
-
}
|
27
|
-
|
28
|
-
def self.generate(dest_path, opts = {})
|
29
|
-
new(opts).write_to(dest_path)
|
30
|
-
end
|
31
|
-
|
32
|
-
def initialize(opts = {})
|
33
|
-
@encryption = opts[:encryption]
|
34
|
-
end
|
35
|
-
|
36
|
-
# @param [String] dest_path path relative to gem root directory to write file to
|
37
|
-
def write_to(dest_path)
|
38
|
-
require 'prawn'
|
39
|
-
|
40
|
-
dest_path = File.join(ROOT_DIR, dest_path)
|
41
|
-
|
42
|
-
Prawn::Document.generate(dest_path, skip_page_creation: true, info: METADATA_OPTIONS, page_size: 'A4') do |pdf|
|
43
|
-
1.upto(5) do |n|
|
44
|
-
pdf.start_new_page
|
45
|
-
pdf.text("Page #{n}")
|
46
|
-
end
|
47
|
-
|
48
|
-
pdf.encrypt_document(ENCRYPTION_OPTIONS) if @encryption
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
|
54
|
-
PdfGenerator.generate("spec/fixtures/pdfs/test.pdf")
|
55
|
-
PdfGenerator.generate("spec/fixtures/pdfs/encrypted.pdf", encryption: true)
|
56
|
-
end
|
data/pdfinfo.gemspec
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'pdfinfo/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "pdfinfo"
|
8
|
-
spec.version = Pdfinfo::VERSION
|
9
|
-
spec.authors = ["Ryan Venegas"]
|
10
|
-
spec.email = ["RVenegas2@Gmail.com"]
|
11
|
-
spec.summary = %q{Simple ruby wrapper around the pdfinfo executable}
|
12
|
-
spec.description = %q{Simple ruby wrapper around the pdfinfo executable}
|
13
|
-
spec.homepage = ""
|
14
|
-
spec.license = "MIT"
|
15
|
-
|
16
|
-
spec.files = `git ls-files -z`.split("\x0")
|
17
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
-
spec.require_paths = ["lib"]
|
19
|
-
|
20
|
-
spec.add_development_dependency "bundler", "~> 1.6"
|
21
|
-
spec.add_development_dependency "prawn"
|
22
|
-
spec.add_development_dependency "rspec", '~> 3.1.0'
|
23
|
-
spec.add_development_dependency "rake"
|
24
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
Title: Pdfinfo Title
|
2
|
-
Subject: Pdfinfo Subject
|
3
|
-
Keywords: Keyword1 Keyword2
|
4
|
-
Author: Pdfinfo Author
|
5
|
-
Creator: Pdfinfo Creator
|
6
|
-
Producer: Pdfinfo Producer
|
7
|
-
CreationDate: Sun Oct 26 18:23:25 2014
|
8
|
-
Tagged: no
|
9
|
-
Form: none
|
10
|
-
Pages: 5
|
11
|
-
Encrypted: yes (print:no copy:no change:no addNotes:no)
|
12
|
-
Page size: 595.28 x 841.89 pts (A4) (rotated 0 degrees)
|
13
|
-
File size: 2861 bytes
|
14
|
-
Optimized: no
|
15
|
-
PDF version: 1.3
|
@@ -1,15 +0,0 @@
|
|
1
|
-
Title: Pdfinfo Title
|
2
|
-
Subject: Pdfinfo Subject
|
3
|
-
Keywords: Keyword1 Keyword2
|
4
|
-
Author: Pdfinfo Author
|
5
|
-
Creator: Pdfinfo Creator
|
6
|
-
Producer: Pdfinfo Producer
|
7
|
-
CreationDate: Sun Oct 26 18:23:25 2014
|
8
|
-
Tagged: no
|
9
|
-
Form: none
|
10
|
-
Pages: 5
|
11
|
-
Encrypted: no
|
12
|
-
Page size: 595.28 x 841.89 pts (A4) (rotated 0 degrees)
|
13
|
-
File size: 2867 bytes
|
14
|
-
Optimized: no
|
15
|
-
PDF version: 1.3
|
data/spec/pdfinfo_spec.rb
DELETED
@@ -1,309 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
RSpec.describe Pdfinfo do
|
4
|
-
let(:pdf_path) { fixture_path('pdfs/test.pdf') }
|
5
|
-
let(:pdfinfo) { described_class.new(pdf_path) }
|
6
|
-
let(:encrypted_response) { File.read(fixture_path('shell_responses/encrypted.txt')).chomp }
|
7
|
-
let(:unencrypted_response) { File.read(fixture_path('shell_responses/test.txt')).chomp }
|
8
|
-
let(:mock_response) { unencrypted_response }
|
9
|
-
|
10
|
-
|
11
|
-
def modified_response(response, key, new_value = '')
|
12
|
-
response.sub(/(?<=#{key}:)(.+)$/, new_value)
|
13
|
-
end
|
14
|
-
|
15
|
-
before(:each) do |ex|
|
16
|
-
unless ex.metadata[:skip_mock_response]
|
17
|
-
allow(Open3).to receive(:capture2).and_return([mock_response, nil])
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
specify "mock responses match", :skip_mock_response do
|
22
|
-
expect(`pdfinfo -upw foo #{fixture_path('pdfs/encrypted.pdf')}`.chomp).to eq(encrypted_response)
|
23
|
-
expect(`pdfinfo #{fixture_path('pdfs/test.pdf')}`.chomp).to eq(unencrypted_response)
|
24
|
-
end
|
25
|
-
|
26
|
-
describe '.pdfinfo_command' do
|
27
|
-
it "falls back to pdfinfo" do
|
28
|
-
Pdfinfo.pdfinfo_command = nil
|
29
|
-
expect(Pdfinfo.pdfinfo_command).to eq('pdfinfo')
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'allows the command to be changed' do
|
33
|
-
Pdfinfo.pdfinfo_command = '/another/bin/path/pdfinfo'
|
34
|
-
expect(Pdfinfo.pdfinfo_command).to eq('/another/bin/path/pdfinfo')
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
describe '.exec' do
|
39
|
-
context 'with no options given' do
|
40
|
-
it 'runs the pdfinfo command without flags' do
|
41
|
-
expect(Open3).to receive(:capture2).with("pdfinfo -enc UTF-8 path/to/file.pdf")
|
42
|
-
Pdfinfo.new("path/to/file.pdf")
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context "passing in :user_password" do
|
47
|
-
it 'runs the pdfinfo command passing the user password flag' do
|
48
|
-
expect(Open3).to receive(:capture2).with("pdfinfo -enc UTF-8 -upw foo path/to/file.pdf")
|
49
|
-
Pdfinfo.new("path/to/file.pdf", user_password: 'foo')
|
50
|
-
end
|
51
|
-
end
|
52
|
-
context 'passing in :owner_password' do
|
53
|
-
it 'runs the pdfinfo command passing the user password flag' do
|
54
|
-
expect(Open3).to receive(:capture2).with("pdfinfo -enc UTF-8 -opw bar path/to/file.pdf")
|
55
|
-
Pdfinfo.new("path/to/file.pdf", owner_password: 'bar')
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
context "when passed a path with spaces" do
|
60
|
-
it 'should escape the file path' do
|
61
|
-
expect(Open3).to receive(:capture2).with("pdfinfo -enc UTF-8 path/to/file\\ with\\ spaces.pdf")
|
62
|
-
Pdfinfo.new("path/to/file with spaces.pdf")
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
describe '#title' do
|
68
|
-
context 'when given a title' do
|
69
|
-
it 'returns the title' do
|
70
|
-
expect(pdfinfo.title).to eq('Pdfinfo Title')
|
71
|
-
end
|
72
|
-
end
|
73
|
-
context 'when title is not present' do
|
74
|
-
let(:mock_response) { modified_response(unencrypted_response, 'Title', '') }
|
75
|
-
it 'returns nil' do
|
76
|
-
expect(pdfinfo.title).to be_nil
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
describe '#subject' do
|
82
|
-
context 'when given a subject' do
|
83
|
-
it 'returns the subject' do
|
84
|
-
expect(pdfinfo.subject).to eq('Pdfinfo Subject')
|
85
|
-
end
|
86
|
-
end
|
87
|
-
context 'when subject value is not present' do
|
88
|
-
let(:mock_response) { modified_response(unencrypted_response, 'Subject', '') }
|
89
|
-
it 'returns nil' do
|
90
|
-
expect(pdfinfo.subject).to be_nil
|
91
|
-
end
|
92
|
-
end
|
93
|
-
context 'when subject key is not present' do
|
94
|
-
let(:mock_response) { unencrypted_response.sub(/^Subject.*\n/, '') }
|
95
|
-
it 'returns nil' do
|
96
|
-
expect(pdfinfo.subject).to be_nil
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
describe '#keywords' do
|
102
|
-
context 'when given keywords' do
|
103
|
-
it 'returns the keywords' do
|
104
|
-
expect(pdfinfo.keywords).to eq(['Keyword1', 'Keyword2'])
|
105
|
-
end
|
106
|
-
end
|
107
|
-
context 'when keywords value is not present' do
|
108
|
-
let(:mock_response) { modified_response(unencrypted_response, 'Keywords', '') }
|
109
|
-
it 'returns an empty array' do
|
110
|
-
expect(pdfinfo.keywords).to eq([])
|
111
|
-
end
|
112
|
-
end
|
113
|
-
context 'when keywords key is not present' do
|
114
|
-
let(:mock_response) { unencrypted_response.sub(/^Keywords.*\n/, '') }
|
115
|
-
it 'returns an empty array' do
|
116
|
-
expect(pdfinfo.keywords).to eq([])
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
describe '#author' do
|
122
|
-
context 'when given an author' do
|
123
|
-
it 'returns the author' do
|
124
|
-
expect(pdfinfo.author).to eq('Pdfinfo Author')
|
125
|
-
end
|
126
|
-
end
|
127
|
-
context 'when author is not present' do
|
128
|
-
let(:mock_response) { modified_response(unencrypted_response, 'Author', '') }
|
129
|
-
it 'returns nil' do
|
130
|
-
expect(pdfinfo.author).to be_nil
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
describe '#creation_date' do
|
136
|
-
context 'when given an author' do
|
137
|
-
it 'returns a time object' do
|
138
|
-
expect(pdfinfo.creation_date).to be_an_instance_of(Time)
|
139
|
-
end
|
140
|
-
it 'returns the time correctly parsed' do
|
141
|
-
expect(pdfinfo.creation_date).to eq(Time.parse("2014-10-26 18:23:25 -0700"))
|
142
|
-
end
|
143
|
-
end
|
144
|
-
context 'when creation date value is not present' do
|
145
|
-
let(:mock_response) { modified_response(unencrypted_response, 'CreationDate', '') }
|
146
|
-
it 'returns nil' do
|
147
|
-
expect(pdfinfo.creation_date).to be_nil
|
148
|
-
end
|
149
|
-
end
|
150
|
-
context 'when creation date key is not present' do
|
151
|
-
let(:mock_response) { unencrypted_response.sub(/^CreationDate.*\n/, '') }
|
152
|
-
it 'returns nil' do
|
153
|
-
expect(pdfinfo.creation_date).to be_nil
|
154
|
-
end
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
describe '#creator' do
|
159
|
-
context 'when given a creator' do
|
160
|
-
it { expect(pdfinfo.creator).to eq('Pdfinfo Creator') }
|
161
|
-
end
|
162
|
-
context 'when creator is not present' do
|
163
|
-
let(:mock_response) { modified_response(unencrypted_response, 'Creator', '') }
|
164
|
-
it { expect(pdfinfo.creator).to be_nil }
|
165
|
-
end
|
166
|
-
end
|
167
|
-
|
168
|
-
describe '#producer' do
|
169
|
-
context 'when given a creator' do
|
170
|
-
it { expect(pdfinfo.producer).to eq('Pdfinfo Producer') }
|
171
|
-
end
|
172
|
-
context 'when creator is not present' do
|
173
|
-
let(:mock_response) { modified_response(unencrypted_response, 'Producer', '') }
|
174
|
-
it { expect(pdfinfo.producer).to be_nil }
|
175
|
-
end
|
176
|
-
end
|
177
|
-
|
178
|
-
describe '#tagged?' do
|
179
|
-
context 'when tagged' do
|
180
|
-
let(:mock_response) { modified_response(unencrypted_response, 'Tagged', 'yes') }
|
181
|
-
it { expect(pdfinfo.tagged?).to eq(true) }
|
182
|
-
end
|
183
|
-
context 'when not tagged' do
|
184
|
-
let(:mock_response) { modified_response(unencrypted_response, 'Tagged', 'no') }
|
185
|
-
it { expect(pdfinfo.tagged?).to eq(false) }
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
describe '#form' do
|
190
|
-
it { expect(pdfinfo.form).to eq('none') }
|
191
|
-
end
|
192
|
-
|
193
|
-
describe '#page_count' do
|
194
|
-
it 'returns a fixnum value of the number of pages' do
|
195
|
-
expect(pdfinfo.page_count).to eq(5)
|
196
|
-
end
|
197
|
-
end
|
198
|
-
|
199
|
-
describe '#encrypted?' do
|
200
|
-
context 'given encrypted pdf' do
|
201
|
-
let(:mock_response) { encrypted_response }
|
202
|
-
it { expect(pdfinfo.encrypted?).to eq(true) }
|
203
|
-
end
|
204
|
-
context 'given unencrypted pdf' do
|
205
|
-
let(:mock_response) { unencrypted_response }
|
206
|
-
it { expect(pdfinfo.encrypted?).to eq(false) }
|
207
|
-
end
|
208
|
-
end
|
209
|
-
|
210
|
-
describe '#usage_rights' do
|
211
|
-
context 'given encrypted pdf (all flags off)' do
|
212
|
-
let(:mock_response) { encrypted_response }
|
213
|
-
it 'returns a permissions hash' do
|
214
|
-
expect(pdfinfo.usage_rights).to eq({
|
215
|
-
print: false,
|
216
|
-
copy: false,
|
217
|
-
change: false,
|
218
|
-
add_notes: false
|
219
|
-
})
|
220
|
-
end
|
221
|
-
end
|
222
|
-
context 'given unencrypted pdf' do
|
223
|
-
let(:mock_response) { unencrypted_response }
|
224
|
-
it 'returns a permissions hash' do
|
225
|
-
expect(pdfinfo.usage_rights).to eq({
|
226
|
-
print: true,
|
227
|
-
copy: true,
|
228
|
-
change: true,
|
229
|
-
add_notes: true
|
230
|
-
})
|
231
|
-
end
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
describe '#printable?' do
|
236
|
-
context 'given a pdf that is printable' do
|
237
|
-
let(:mock_response) { unencrypted_response }
|
238
|
-
it { expect(pdfinfo.printable?).to eq(true) }
|
239
|
-
end
|
240
|
-
context 'given a pdf that is not printable' do
|
241
|
-
let(:mock_response) { encrypted_response }
|
242
|
-
it { expect(pdfinfo.printable?).to eq(false) }
|
243
|
-
end
|
244
|
-
end
|
245
|
-
|
246
|
-
describe '#copyable?' do
|
247
|
-
context 'given a pdf that is copyable' do
|
248
|
-
let(:mock_response) { unencrypted_response }
|
249
|
-
it { expect(pdfinfo.copyable?).to eq(true) }
|
250
|
-
end
|
251
|
-
context 'given a pdf that is not copyable' do
|
252
|
-
let(:mock_response) { encrypted_response }
|
253
|
-
it { expect(pdfinfo.copyable?).to eq(false) }
|
254
|
-
end
|
255
|
-
end
|
256
|
-
|
257
|
-
describe '#changeable?' do
|
258
|
-
context 'given a pdf that is changeable' do
|
259
|
-
let(:mock_response) { unencrypted_response }
|
260
|
-
it { expect(pdfinfo.changeable?).to eq(true) }
|
261
|
-
end
|
262
|
-
context 'given a pdf that is not changeable' do
|
263
|
-
let(:mock_response) { encrypted_response }
|
264
|
-
it { expect(pdfinfo.changeable?).to eq(false) }
|
265
|
-
end
|
266
|
-
end
|
267
|
-
|
268
|
-
describe "#modifiable? (alias to changeable?)" do
|
269
|
-
context 'given a pdf that is changeable' do
|
270
|
-
let(:mock_response) { unencrypted_response }
|
271
|
-
it { expect(pdfinfo.modifiable?).to eq(true) }
|
272
|
-
end
|
273
|
-
context 'given a pdf that is not changeable' do
|
274
|
-
let(:mock_response) { encrypted_response }
|
275
|
-
it { expect(pdfinfo.modifiable?).to eq(false) }
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
describe '#annotatable?' do
|
280
|
-
context 'given a pdf that is annotatable' do
|
281
|
-
let(:mock_response) { unencrypted_response }
|
282
|
-
it { expect(pdfinfo.annotatable?).to eq(true) }
|
283
|
-
end
|
284
|
-
context 'given a pdf that is not annotatable' do
|
285
|
-
let(:mock_response) { encrypted_response }
|
286
|
-
it { expect(pdfinfo.annotatable?).to eq(false) }
|
287
|
-
end
|
288
|
-
end
|
289
|
-
|
290
|
-
describe '#width' do
|
291
|
-
it { expect(pdfinfo.width).to eq(595.28) }
|
292
|
-
end
|
293
|
-
|
294
|
-
describe '#height' do
|
295
|
-
it { expect(pdfinfo.height).to eq(841.89) }
|
296
|
-
end
|
297
|
-
|
298
|
-
describe '#size' do
|
299
|
-
it 'returns a fixnm value for the file size in bytes' do
|
300
|
-
expect(pdfinfo.file_size).to eq(2867)
|
301
|
-
end
|
302
|
-
end
|
303
|
-
|
304
|
-
describe '#pdf_version' do
|
305
|
-
it 'returns a string value for the PDF spec version' do
|
306
|
-
expect(pdfinfo.pdf_version).to eq('1.3')
|
307
|
-
end
|
308
|
-
end
|
309
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require File.expand_path("#{__dir__}/../lib/pdfinfo")
|
2
|
-
|
3
|
-
RSpec.configure do |config|
|
4
|
-
config.expect_with :rspec do |expectations|
|
5
|
-
# expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
6
|
-
end
|
7
|
-
|
8
|
-
config.mock_with :rspec do |mocks|
|
9
|
-
mocks.verify_partial_doubles = true
|
10
|
-
end
|
11
|
-
|
12
|
-
config.filter_run :focus
|
13
|
-
config.run_all_when_everything_filtered = true
|
14
|
-
|
15
|
-
config.disable_monkey_patching!
|
16
|
-
|
17
|
-
config.warnings = false
|
18
|
-
|
19
|
-
if config.files_to_run.one?
|
20
|
-
config.default_formatter = 'doc'
|
21
|
-
end
|
22
|
-
|
23
|
-
# config.profile_examples = 10
|
24
|
-
|
25
|
-
config.order = :random
|
26
|
-
|
27
|
-
Kernel.srand config.seed
|
28
|
-
|
29
|
-
config.before(:each) do
|
30
|
-
Pdfinfo.instance_variable_set(:@pdfinfo_command, nil)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def fixture_path(path)
|
35
|
-
Pathname.new(File.expand_path(File.join("../fixtures", path), __FILE__))
|
36
|
-
end
|