pdf_info 0.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.

Potentially problematic release.


This version of pdf_info might be problematic. Click here for more details.

data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "rspec", "~> 2.5.0"
5
+ gem "bundler", "~> 1.0.0"
6
+ gem "jeweler", "~> 1.5.2"
7
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ git (1.2.5)
6
+ jeweler (1.5.2)
7
+ bundler (~> 1.0.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rake (0.8.7)
11
+ rspec (2.5.0)
12
+ rspec-core (~> 2.5.0)
13
+ rspec-expectations (~> 2.5.0)
14
+ rspec-mocks (~> 2.5.0)
15
+ rspec-core (2.5.1)
16
+ rspec-expectations (2.5.0)
17
+ diff-lcs (~> 1.1.2)
18
+ rspec-mocks (2.5.0)
19
+
20
+ PLATFORMS
21
+ ruby
22
+
23
+ DEPENDENCIES
24
+ bundler (~> 1.0.0)
25
+ jeweler (~> 1.5.2)
26
+ rspec (~> 2.5.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Tom Taylor
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ pdf_info
2
+ ==
3
+
4
+ Very simple wrapper to the pdfinfo unix tool, to provide the metadata information as a hash.
5
+
6
+ Example
7
+ --
8
+
9
+ require 'pdf/info'
10
+ info = PDF::Info.new('/Users/tom/tmp/magazine.pdf')
11
+ pp info.metadata
12
+
13
+ Gives you the following output:
14
+
15
+ {:version=>1.3,
16
+ :pages=>
17
+ [[819.213, 1077.17],
18
+ [819.213, 1077.17],
19
+ [819.213, 1077.17],
20
+ [819.213, 1077.17],
21
+ [819.213, 1077.17],
22
+ [819.213, 1077.17],
23
+ [819.213, 1077.17],
24
+ [819.213, 1077.17],
25
+ [819.213, 1077.17],
26
+ [819.213, 1077.17],
27
+ [819.213, 1077.17],
28
+ [819.213, 1077.17]],
29
+ :page_count=>12,
30
+ :encrypted=>false}
31
+
32
+ Each of the pages has an individual size - that's just how PDFs are. If you want more of the metadata that pdfinfo outputs, send us a patch.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "pdf_info"
16
+ gem.homepage = "http://github.com/newspaperclub/pdf_info"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{Wraps the pdfinfo command line tool to provide a hash of metadata}
19
+ gem.email = "tom@newspaperclub.com"
20
+ gem.authors = ["Tom Taylor"]
21
+ end
22
+ Jeweler::RubygemsDotOrgTasks.new
23
+
24
+ require 'rspec/core'
25
+ require 'rspec/core/rake_task'
26
+ RSpec::Core::RakeTask.new(:spec) do |spec|
27
+ spec.pattern = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "pdf_info #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,12 @@
1
+ module PDF
2
+ class Info
3
+ class UnexpectedExitError < RuntimeError
4
+ attr_accessor :exit_code
5
+ end
6
+
7
+ class UnknownError < RuntimeError; end
8
+ class FileError < RuntimeError; end
9
+ class OutputError < RuntimeError; end
10
+ class BadPermissionsError < RuntimeError; end
11
+ end
12
+ end
data/lib/pdf/info.rb ADDED
@@ -0,0 +1,70 @@
1
+ require 'pdf/info/exceptions'
2
+
3
+ module PDF
4
+ class Info
5
+ @@command_path = "pdfinfo"
6
+
7
+ def self.command_path=(path)
8
+ @@command_path = path
9
+ end
10
+
11
+ def self.command_path
12
+ @@command_path
13
+ end
14
+
15
+ def initialize(pdf_path)
16
+ @pdf_path = pdf_path
17
+ end
18
+
19
+ def command
20
+ output = `#{self.class.command_path} "#{@pdf_path}" -f 1 -l -1`
21
+ exit_code = $?
22
+ case exit_code
23
+ when 0
24
+ return output
25
+ else
26
+ exit_error = PDF::Info::UnexpectedExitError.new
27
+ exit_error.exit_code = exit_code
28
+ raise exit_error
29
+ end
30
+ end
31
+
32
+ def metadata
33
+ begin
34
+ process_output(command)
35
+ rescue UnexpectedExitError => e
36
+ case e.exit_code
37
+ when 1
38
+ raise FileError
39
+ when 2
40
+ raise OutputError
41
+ when 3
42
+ raise BadPermissionsError
43
+ else
44
+ raise UnknownError
45
+ end
46
+ end
47
+ end
48
+
49
+ def process_output(output)
50
+ rows = output.split("\n")
51
+ metadata = {}
52
+ rows.each do |row|
53
+ pair = row.split(':', 2)
54
+ case pair.first
55
+ when "Pages"
56
+ metadata[:page_count] = pair.last.to_i
57
+ when "Encrypted"
58
+ metadata[:encrypted] = pair.last == 'yes'
59
+ when "PDF version"
60
+ metadata[:version] = pair.last.to_f
61
+ when /^Page.*size$/
62
+ metadata[:pages] ||= []
63
+ metadata[:pages] << pair.last.scan(/[\d.]+/).map(&:to_f)
64
+ end
65
+ end
66
+ return metadata
67
+ end
68
+
69
+ end
70
+ end
data/pdf_info.gemspec ADDED
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{pdf_info}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tom Taylor"]
12
+ s.date = %q{2011-04-15}
13
+ s.email = %q{tom@newspaperclub.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE.txt",
16
+ "README.md"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ "Gemfile",
21
+ "Gemfile.lock",
22
+ "LICENSE.txt",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/pdf/info.rb",
27
+ "lib/pdf/info/exceptions.rb",
28
+ "pdf_info.gemspec",
29
+ "spec/output/successful.txt",
30
+ "spec/pdf_info_spec.rb",
31
+ "spec/spec_helper.rb",
32
+ "spec/support/output_helper.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/newspaperclub/pdf_info}
35
+ s.licenses = ["MIT"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.6.2}
38
+ s.summary = %q{Wraps the pdfinfo command line tool to provide a hash of metadata}
39
+ s.test_files = [
40
+ "spec/pdf_info_spec.rb",
41
+ "spec/spec_helper.rb",
42
+ "spec/support/output_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rspec>, ["~> 2.5.0"])
50
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
51
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
52
+ else
53
+ s.add_dependency(%q<rspec>, ["~> 2.5.0"])
54
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
55
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<rspec>, ["~> 2.5.0"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
61
+ end
62
+ end
63
+
@@ -0,0 +1,22 @@
1
+ Creator: Adobe InDesign CS2 (4.0.5)
2
+ Producer: Adobe PDF Library 7.0
3
+ CreationDate: Mon Jun 14 13:59:52 2010
4
+ ModDate: Mon Jun 14 14:02:15 2010
5
+ Tagged: no
6
+ Pages: 12
7
+ Encrypted: no
8
+ Page 1 size: 819.213 x 1077.17 pts
9
+ Page 2 size: 819.213 x 1077.17 pts
10
+ Page 3 size: 819.213 x 1077.17 pts
11
+ Page 4 size: 819.213 x 1077.17 pts
12
+ Page 5 size: 819.213 x 1077.17 pts
13
+ Page 6 size: 819.213 x 1077.17 pts
14
+ Page 7 size: 819.213 x 1077.17 pts
15
+ Page 8 size: 819.213 x 1077.17 pts
16
+ Page 9 size: 819.213 x 1077.17 pts
17
+ Page 10 size: 819.213 x 1077.17 pts
18
+ Page 11 size: 819.213 x 1077.17 pts
19
+ Page 12 size: 819.213 x 1077.17 pts
20
+ File size: 15650809 bytes
21
+ Optimized: no
22
+ PDF version: 1.4
@@ -0,0 +1,90 @@
1
+ require 'spec_helper'
2
+
3
+ describe PDF::Info do
4
+
5
+ describe "default #command_path" do
6
+ subject { PDF::Info }
7
+ its(:command_path) { should == "pdfinfo" }
8
+ end
9
+
10
+ describe "#command_path=" do
11
+ subject { PDF::Info }
12
+ before(:each) { subject.command_path = "/usr/bin/pdfinfo" }
13
+ its(:command_path) { should == "/usr/bin/pdfinfo" }
14
+ end
15
+
16
+ describe ".metadata" do
17
+
18
+ context "success" do
19
+ before(:each) do
20
+ @pdf_info = PDF::Info.new('test.pdf')
21
+ @pdf_info.stub!(:command).and_return(output('successful.txt'))
22
+ end
23
+ subject { @pdf_info.metadata }
24
+ its([:page_count]) { should == 12 }
25
+ its([:pages]) { should have(12).items }
26
+
27
+ it "should have the correct page size set" do
28
+ subject[:pages].each do |page|
29
+ page.should == [819.213, 1077.17]
30
+ end
31
+ end
32
+
33
+ its([:version]) { should == 1.4 }
34
+ its([:encrypted]) { should be_false }
35
+ end
36
+
37
+ context "error opening file" do
38
+ before(:each) do
39
+ @pdf_info = PDF::Info.new('test.pdf')
40
+ unexpected_error = PDF::Info::UnexpectedExitError.new
41
+ unexpected_error.exit_code = 1
42
+ @pdf_info.stub!(:command).and_raise(unexpected_error)
43
+ end
44
+
45
+ it "should raise an error" do
46
+ lambda { @pdf_info.metadata }.should raise_error(PDF::Info::FileError)
47
+ end
48
+ end
49
+
50
+ context "error opening output" do
51
+ before(:each) do
52
+ @pdf_info = PDF::Info.new('test.pdf')
53
+ unexpected_error = PDF::Info::UnexpectedExitError.new
54
+ unexpected_error.exit_code = 2
55
+ @pdf_info.stub!(:command).and_raise(unexpected_error)
56
+ end
57
+
58
+ it "should raise an error" do
59
+ lambda { @pdf_info.metadata }.should raise_error(PDF::Info::OutputError)
60
+ end
61
+ end
62
+
63
+ context "bad permissions" do
64
+ before(:each) do
65
+ @pdf_info = PDF::Info.new('test.pdf')
66
+ unexpected_error = PDF::Info::UnexpectedExitError.new
67
+ unexpected_error.exit_code = 3
68
+ @pdf_info.stub!(:command).and_raise(unexpected_error)
69
+ end
70
+
71
+ it "should raise an error" do
72
+ lambda { @pdf_info.metadata }.should raise_error(PDF::Info::BadPermissionsError)
73
+ end
74
+ end
75
+
76
+ context "unknown error" do
77
+ before(:each) do
78
+ @pdf_info = PDF::Info.new('test.pdf')
79
+ unexpected_error = PDF::Info::UnexpectedExitError.new
80
+ unexpected_error.exit_code = 4
81
+ @pdf_info.stub!(:command).and_raise(unexpected_error)
82
+ end
83
+
84
+ it "should raise an error" do
85
+ lambda { @pdf_info.metadata }.should raise_error(PDF::Info::UnknownError)
86
+ end
87
+ end
88
+ end
89
+
90
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'pdf/info'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+ config.include OutputHelper
12
+ end
@@ -0,0 +1,7 @@
1
+ module OutputHelper
2
+
3
+ def output(filename)
4
+ File.read(File.join(File.dirname(__FILE__), '..', 'output', filename))
5
+ end
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pdf_info
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Tom Taylor
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-15 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ type: :development
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 27
30
+ segments:
31
+ - 2
32
+ - 5
33
+ - 0
34
+ version: 2.5.0
35
+ name: rspec
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ prerelease: false
39
+ type: :development
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 23
46
+ segments:
47
+ - 1
48
+ - 0
49
+ - 0
50
+ version: 1.0.0
51
+ name: bundler
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ prerelease: false
55
+ type: :development
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 7
62
+ segments:
63
+ - 1
64
+ - 5
65
+ - 2
66
+ version: 1.5.2
67
+ name: jeweler
68
+ version_requirements: *id003
69
+ description:
70
+ email: tom@newspaperclub.com
71
+ executables: []
72
+
73
+ extensions: []
74
+
75
+ extra_rdoc_files:
76
+ - LICENSE.txt
77
+ - README.md
78
+ files:
79
+ - .document
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - VERSION
86
+ - lib/pdf/info.rb
87
+ - lib/pdf/info/exceptions.rb
88
+ - pdf_info.gemspec
89
+ - spec/output/successful.txt
90
+ - spec/pdf_info_spec.rb
91
+ - spec/spec_helper.rb
92
+ - spec/support/output_helper.rb
93
+ has_rdoc: true
94
+ homepage: http://github.com/newspaperclub/pdf_info
95
+ licenses:
96
+ - MIT
97
+ post_install_message:
98
+ rdoc_options: []
99
+
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 3
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ hash: 3
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ requirements: []
121
+
122
+ rubyforge_project:
123
+ rubygems_version: 1.6.2
124
+ signing_key:
125
+ specification_version: 3
126
+ summary: Wraps the pdfinfo command line tool to provide a hash of metadata
127
+ test_files:
128
+ - spec/pdf_info_spec.rb
129
+ - spec/spec_helper.rb
130
+ - spec/support/output_helper.rb