pdftoimage 0.1.1
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.
- data/.bundle/config +2 -0
- data/.document +3 -0
- data/.rspec +1 -0
- data/.yardopts +1 -0
- data/ChangeLog.rdoc +4 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +33 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +38 -0
- data/Rakefile +26 -0
- data/gemspec.yml +12 -0
- data/lib/pdftoimage.rb +74 -0
- data/lib/pdftoimage/image.rb +97 -0
- data/lib/pdftoimage/version.rb +4 -0
- data/pdftoimage.gemspec +10 -0
- data/spec/3pages.pdf +0 -0
- data/spec/pdftoimage_spec.rb +31 -0
- data/spec/spec_helper.rb +4 -0
- metadata +119 -0
data/.bundle/config
ADDED
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format documentation
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup rdoc --title "pdftoimage Documentation" --protected
|
data/ChangeLog.rdoc
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
pdftoimage (0.1.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: http://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.1.2)
|
10
|
+
ore-core (0.1.0)
|
11
|
+
rake (0.8.7)
|
12
|
+
rspec (2.0.1)
|
13
|
+
rspec-core (~> 2.0.1)
|
14
|
+
rspec-expectations (~> 2.0.1)
|
15
|
+
rspec-mocks (~> 2.0.1)
|
16
|
+
rspec-core (2.0.1)
|
17
|
+
rspec-expectations (2.0.1)
|
18
|
+
diff-lcs (>= 1.1.2)
|
19
|
+
rspec-mocks (2.0.1)
|
20
|
+
rspec-core (~> 2.0.1)
|
21
|
+
rspec-expectations (~> 2.0.1)
|
22
|
+
yard (0.6.1)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
bundler (~> 1.0.0)
|
29
|
+
ore-core (~> 0.1.0)
|
30
|
+
pdftoimage!
|
31
|
+
rake (~> 0.8.7)
|
32
|
+
rspec (~> 2.0.0)
|
33
|
+
yard (~> 0.6.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Rob Flynn
|
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.rdoc
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
= pdftoimage
|
2
|
+
|
3
|
+
* {Homepage}[http://rubygems.org/gems/pdftoimage]
|
4
|
+
|
5
|
+
== Description
|
6
|
+
|
7
|
+
PDFToImage is a ruby gem which allows for conversion of a PDF document into
|
8
|
+
images. It uses poppler_utils to first convert the document to PNG and then
|
9
|
+
allows usage of ImageMagick to convert the image into other formats.
|
10
|
+
|
11
|
+
The reasoning behind using poppler_utils is due to the fact that ghostscript
|
12
|
+
occasionally has trouble with certain PDF documents which poppler_utils seems
|
13
|
+
to be able to parse without error.
|
14
|
+
|
15
|
+
== Examples
|
16
|
+
|
17
|
+
require 'pdftoimage'
|
18
|
+
images = PDFToImage.open('somefile.pdf')
|
19
|
+
images.each do |img|
|
20
|
+
img.resize('50%').save("output/page-#{img.page}.jpg")
|
21
|
+
end
|
22
|
+
|
23
|
+
== Requirements
|
24
|
+
|
25
|
+
poppler_utils
|
26
|
+
|
27
|
+
ImageMagick
|
28
|
+
|
29
|
+
|
30
|
+
== Install
|
31
|
+
|
32
|
+
$ gem install pdftoimage
|
33
|
+
|
34
|
+
== Copyright
|
35
|
+
|
36
|
+
Copyright (c) 2010 Rob Flynn
|
37
|
+
|
38
|
+
See LICENSE.txt for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler'
|
5
|
+
rescue LoadError => e
|
6
|
+
STDERR.puts e.message
|
7
|
+
STDERR.puts "Run `gem install bundler` to install Bundler."
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
|
11
|
+
begin
|
12
|
+
Bundler.setup(:development)
|
13
|
+
rescue Bundler::BundlerError => e
|
14
|
+
STDERR.puts e.message
|
15
|
+
STDERR.puts "Run `bundle install` to install missing gems."
|
16
|
+
exit e.status_code
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake'
|
20
|
+
|
21
|
+
require 'rspec/core/rake_task'
|
22
|
+
RSpec::Core::RakeTask.new
|
23
|
+
task :default => :spec
|
24
|
+
|
25
|
+
require 'yard'
|
26
|
+
YARD::Rake::YardocTask.new
|
data/gemspec.yml
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
name: pdftoimage
|
2
|
+
summary: "A ruby gem for converting PDF documents into a series of images."
|
3
|
+
description: "A ruby gem for converting PDF documents into a series of images. This module is based off poppler_utils and ImageMagick."
|
4
|
+
license: MIT
|
5
|
+
authors: Rob Flynn
|
6
|
+
homepage: http://rubygems.org/gems/pdftoimage
|
7
|
+
email: rob@thingerly.com
|
8
|
+
has_yard: true
|
9
|
+
|
10
|
+
development_dependencies:
|
11
|
+
bundler: ~> 1.0.0
|
12
|
+
yard: ~> 0.6.0
|
data/lib/pdftoimage.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'tmpdir'
|
2
|
+
require 'pdftoimage/version'
|
3
|
+
require 'pdftoimage/image'
|
4
|
+
|
5
|
+
module PDFToImage
|
6
|
+
class PDFError < RuntimeError; end
|
7
|
+
end
|
8
|
+
|
9
|
+
module PDFToImage
|
10
|
+
# A class variable for storing the location of our temp folder
|
11
|
+
@@pdf_temp_dir = File.join(Dir.tmpdir())
|
12
|
+
|
13
|
+
begin
|
14
|
+
tmp = `pdftoppm -v 2>&1`
|
15
|
+
raise(PDFToImage::PDFError, "poppler_utils not installed") unless tmp.index('Poppler')
|
16
|
+
rescue Errno::ENOENT
|
17
|
+
raise PDFToImage::PDFError, "poppler_utils not installed"
|
18
|
+
end
|
19
|
+
|
20
|
+
begin
|
21
|
+
tmp = `identify -version 2>&1`
|
22
|
+
raise(PDFToImage::PDFError, "ImageMagick not installed") unless tmp.index('ImageMagick')
|
23
|
+
rescue Errno::ENOENT
|
24
|
+
raise PDFToImage::PDFError, "ImageMagick not installed"
|
25
|
+
end
|
26
|
+
|
27
|
+
class << self
|
28
|
+
|
29
|
+
# Opens a PDF document and prepares it for splitting into images.
|
30
|
+
#
|
31
|
+
# @param [filename] The filename of the PDF to open
|
32
|
+
# @return [Array] An array of images
|
33
|
+
def open(filename)
|
34
|
+
target_path = random_filename
|
35
|
+
|
36
|
+
if not File.exists?(filename)
|
37
|
+
raise PDFError, "File '#{filename}' not found."
|
38
|
+
end
|
39
|
+
|
40
|
+
cmd = "pdftoppm -png #{filename} #{target_path}"
|
41
|
+
|
42
|
+
`#{cmd}`
|
43
|
+
if $? != 0
|
44
|
+
raise PDFError, "Error reading PDF."
|
45
|
+
end
|
46
|
+
|
47
|
+
pngs = Dir.glob("#{target_path}*.png")
|
48
|
+
|
49
|
+
images = []
|
50
|
+
|
51
|
+
pngs.each do |png|
|
52
|
+
image = Image.new(png)
|
53
|
+
images << image
|
54
|
+
end
|
55
|
+
|
56
|
+
return images.sort!
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
# Generate a random file name in the system's tmp folder
|
62
|
+
def random_filename
|
63
|
+
File.join(@@pdf_temp_dir, random_name)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Generate a random name of {#length} characters.
|
67
|
+
def random_name(length = 15)
|
68
|
+
@@chars ||= ("a".."z").to_a + ("A".."Z").to_a + ("1".."9").to_a
|
69
|
+
return 'pdftoimage-' + Array.new(length, '').collect{@@chars[rand(@@chars.size)]}.join
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module PDFToImage
|
2
|
+
|
3
|
+
class Image
|
4
|
+
attr_reader :filename
|
5
|
+
attr_reader :width
|
6
|
+
attr_reader :height
|
7
|
+
attr_reader :format
|
8
|
+
attr_reader :page
|
9
|
+
attr_reader :args
|
10
|
+
|
11
|
+
# We currently only support resizing, as that's all I need at the moment
|
12
|
+
# selfish, but I need to return to the main project
|
13
|
+
CUSTOM_IMAGE_METHODS = [
|
14
|
+
"resize"
|
15
|
+
]
|
16
|
+
|
17
|
+
CUSTOM_IMAGE_METHODS.each do |method|
|
18
|
+
define_method(method.to_sym) do |*args|
|
19
|
+
@args << "-#{method} #{args.join(' ')}"
|
20
|
+
|
21
|
+
self
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Image constructor
|
26
|
+
#
|
27
|
+
# @param [filename] The name of the image file to open
|
28
|
+
def initialize(filename)
|
29
|
+
@args = []
|
30
|
+
|
31
|
+
@filename = filename
|
32
|
+
|
33
|
+
info = identify()
|
34
|
+
|
35
|
+
@width = info[:width]
|
36
|
+
@height = info[:height]
|
37
|
+
@format = info[:format]
|
38
|
+
|
39
|
+
tmp_base = File.basename(filename, File.extname(filename))
|
40
|
+
pieces = tmp_base.split('-')
|
41
|
+
@page = pieces[-1].to_i
|
42
|
+
end
|
43
|
+
|
44
|
+
# Saves the converted image to the specified location
|
45
|
+
#
|
46
|
+
# @param [outname] The output filename of the image
|
47
|
+
#
|
48
|
+
def save(outname)
|
49
|
+
cmd = "convert "
|
50
|
+
|
51
|
+
if not @args.empty?
|
52
|
+
cmd += "#{@args.join(' ')} "
|
53
|
+
end
|
54
|
+
|
55
|
+
cmd += "#{@filename} #{outname}"
|
56
|
+
|
57
|
+
`#{cmd}`
|
58
|
+
if $? != 0
|
59
|
+
raise PDFError, "Error converting file: #{cmd}"
|
60
|
+
end
|
61
|
+
|
62
|
+
return true
|
63
|
+
end
|
64
|
+
|
65
|
+
def <=>(img)
|
66
|
+
if @page == img.page
|
67
|
+
return 0
|
68
|
+
elsif @page < img.page
|
69
|
+
return -1
|
70
|
+
else
|
71
|
+
return 1
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def identify
|
78
|
+
cmd = "identify #{@filename}"
|
79
|
+
|
80
|
+
result = `#{cmd}`
|
81
|
+
unless $?.success?
|
82
|
+
raise PDFToImage::PDFError, "Error executing #{cmd}"
|
83
|
+
end
|
84
|
+
|
85
|
+
info = result.strip.split(' ')
|
86
|
+
dimensions = info[2].split('x')
|
87
|
+
|
88
|
+
return {
|
89
|
+
:format => info[1],
|
90
|
+
:width => dimensions[0],
|
91
|
+
:height => dimensions[1]
|
92
|
+
}
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
data/pdftoimage.gemspec
ADDED
data/spec/3pages.pdf
ADDED
Binary file
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pdftoimage'
|
3
|
+
|
4
|
+
describe PDFToImage do
|
5
|
+
it "should have a VERSION constant" do
|
6
|
+
PDFToImage.const_get('VERSION').should_not be_empty
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "3pages.pdf" do
|
10
|
+
it "should have three pages" do
|
11
|
+
@pages = PDFToImage.open('spec/3pages.pdf')
|
12
|
+
@pages.size.should equal 3
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should allow saving" do
|
16
|
+
@pages = PDFToImage.open('spec/3pages.pdf')
|
17
|
+
@pages[0].save('spec/tmp.jpg')
|
18
|
+
File.exists?('spec/tmp.jpg').should equal true
|
19
|
+
File.unlink('spec/tmp.jpg')
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should allow resizing" do
|
23
|
+
@pages = PDFToImage.open('spec/3pages.pdf')
|
24
|
+
@pages[0].resize('50%').save('spec/tmp2.jpg')
|
25
|
+
File.exists?('spec/tmp2.jpg').should equal true
|
26
|
+
File.unlink('spec/tmp2.jpg')
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pdftoimage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Rob Flynn
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-11-10 00:00:00 +00:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: yard
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 6
|
33
|
+
- 0
|
34
|
+
version: 0.6.0
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: bundler
|
39
|
+
prerelease: false
|
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
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
description: A ruby gem for converting PDF documents into a series of images. This module is based off poppler_utils and ImageMagick.
|
54
|
+
email: rob@thingerly.com
|
55
|
+
executables: []
|
56
|
+
|
57
|
+
extensions: []
|
58
|
+
|
59
|
+
extra_rdoc_files:
|
60
|
+
- README.rdoc
|
61
|
+
- LICENSE.txt
|
62
|
+
- ChangeLog.rdoc
|
63
|
+
files:
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
- spec/3pages.pdf
|
66
|
+
- lib/pdftoimage/version.rb
|
67
|
+
- gemspec.yml
|
68
|
+
- ChangeLog.rdoc
|
69
|
+
- .rspec
|
70
|
+
- pdftoimage.gemspec
|
71
|
+
- .yardopts
|
72
|
+
- .bundle/config
|
73
|
+
- lib/pdftoimage/image.rb
|
74
|
+
- Gemfile
|
75
|
+
- Rakefile
|
76
|
+
- LICENSE.txt
|
77
|
+
- lib/pdftoimage.rb
|
78
|
+
- README.rdoc
|
79
|
+
- Gemfile.lock
|
80
|
+
- spec/pdftoimage_spec.rb
|
81
|
+
- .document
|
82
|
+
has_rdoc: yard
|
83
|
+
homepage: http://rubygems.org/gems/pdftoimage
|
84
|
+
licenses:
|
85
|
+
- MIT
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
|
89
|
+
require_paths:
|
90
|
+
- lib
|
91
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
hash: 3
|
97
|
+
segments:
|
98
|
+
- 0
|
99
|
+
version: "0"
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
hash: 23
|
106
|
+
segments:
|
107
|
+
- 1
|
108
|
+
- 3
|
109
|
+
- 6
|
110
|
+
version: 1.3.6
|
111
|
+
requirements: []
|
112
|
+
|
113
|
+
rubyforge_project: pdftoimage
|
114
|
+
rubygems_version: 1.3.7
|
115
|
+
signing_key:
|
116
|
+
specification_version: 3
|
117
|
+
summary: A ruby gem for converting PDF documents into a series of images.
|
118
|
+
test_files:
|
119
|
+
- spec/pdftoimage_spec.rb
|