pdftoimage 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog.rdoc +6 -1
- data/Gemfile.lock +1 -1
- data/gemspec.yml +2 -1
- data/lib/pdftoimage.rb +55 -13
- data/lib/pdftoimage/image.rb +24 -32
- data/lib/pdftoimage/version.rb +1 -1
- data/spec/pdftoimage_spec.rb +10 -2
- metadata +5 -5
data/ChangeLog.rdoc
CHANGED
@@ -1,4 +1,9 @@
|
|
1
|
-
=== 0.1.
|
1
|
+
=== 0.1.2 / 2010-11-11
|
2
|
+
* Added support for blocks upon opening a PDF
|
3
|
+
* Image objects now support the "quality" method for JPEG/MIFF/PNG compression levels.
|
4
|
+
* PDF conversion is now deferred until saving. This greatly speeds up the conversion process in cases where you only want a few pages out of a large document converted.
|
5
|
+
|
6
|
+
=== 0.1.1 / 2010-11-10
|
2
7
|
|
3
8
|
* Initial release:
|
4
9
|
|
data/Gemfile.lock
CHANGED
data/gemspec.yml
CHANGED
@@ -3,10 +3,11 @@ summary: "A ruby gem for converting PDF documents into a series of images."
|
|
3
3
|
description: "A ruby gem for converting PDF documents into a series of images. This module is based off poppler_utils and ImageMagick."
|
4
4
|
license: MIT
|
5
5
|
authors: Rob Flynn
|
6
|
-
homepage:
|
6
|
+
homepage: https://github.com/robflynn/pdftoimage
|
7
7
|
email: rob@thingerly.com
|
8
8
|
has_yard: true
|
9
9
|
|
10
10
|
development_dependencies:
|
11
11
|
bundler: ~> 1.0.0
|
12
12
|
yard: ~> 0.6.0
|
13
|
+
|
data/lib/pdftoimage.rb
CHANGED
@@ -30,33 +30,75 @@ module PDFToImage
|
|
30
30
|
#
|
31
31
|
# @param [filename] The filename of the PDF to open
|
32
32
|
# @return [Array] An array of images
|
33
|
-
def open(filename)
|
34
|
-
target_path = random_filename
|
35
|
-
|
33
|
+
def open(filename, &block)
|
36
34
|
if not File.exists?(filename)
|
37
35
|
raise PDFError, "File '#{filename}' not found."
|
38
36
|
end
|
39
37
|
|
40
|
-
|
38
|
+
pages = page_count(filename)
|
39
|
+
|
40
|
+
# Array of images
|
41
|
+
images = []
|
42
|
+
|
43
|
+
1.upto(pages) { |n|
|
44
|
+
dimensions = page_size(filename, n)
|
45
|
+
image = Image.new(filename, random_filename, n, dimensions)
|
46
|
+
images << image
|
47
|
+
}
|
48
|
+
|
49
|
+
images.each(&block) if block_given?
|
50
|
+
|
51
|
+
return images
|
41
52
|
|
42
|
-
|
53
|
+
end
|
54
|
+
|
55
|
+
# Executes the specified command, returning the output.
|
56
|
+
#
|
57
|
+
# @param [cmd] The command to run
|
58
|
+
# @return [String] The output of the command
|
59
|
+
def exec(cmd, error = nil)
|
60
|
+
output = `#{cmd}`
|
43
61
|
if $? != 0
|
44
|
-
|
62
|
+
if error == nil
|
63
|
+
raise PDFError, "Error executing command: #{cmd}"
|
64
|
+
else
|
65
|
+
raise PDFError, error
|
66
|
+
end
|
45
67
|
end
|
46
68
|
|
47
|
-
|
69
|
+
return output
|
70
|
+
end
|
48
71
|
|
49
|
-
|
72
|
+
private
|
50
73
|
|
51
|
-
|
52
|
-
|
53
|
-
|
74
|
+
def page_size(filename, page)
|
75
|
+
cmd = "pdfinfo -f #{page} -l #{page} #{filename}"
|
76
|
+
output = exec(cmd)
|
77
|
+
|
78
|
+
matches = /^Page.*?size:.*?(\d+).*?(\d+)/.match(output)
|
79
|
+
if matches.nil?
|
80
|
+
raise PDFError, "Unable to determine page size."
|
54
81
|
end
|
55
82
|
|
56
|
-
|
83
|
+
scale = 2.08333333333333333
|
84
|
+
dimension = {
|
85
|
+
:width => (matches[1].to_i * scale).to_i,
|
86
|
+
:height => (matches[2].to_i * scale).to_i
|
87
|
+
}
|
88
|
+
|
89
|
+
dimension
|
57
90
|
end
|
58
91
|
|
59
|
-
|
92
|
+
def page_count(filename)
|
93
|
+
cmd = "pdfinfo #{filename}"
|
94
|
+
output = exec(cmd)
|
95
|
+
matches = /^Pages:.*?(\d+)$/.match(output)
|
96
|
+
if matches.nil?
|
97
|
+
raise PDFError, "Error determining page count."
|
98
|
+
end
|
99
|
+
|
100
|
+
return matches[1].to_i
|
101
|
+
end
|
60
102
|
|
61
103
|
# Generate a random file name in the system's tmp folder
|
62
104
|
def random_filename
|
data/lib/pdftoimage/image.rb
CHANGED
@@ -1,17 +1,20 @@
|
|
1
1
|
module PDFToImage
|
2
2
|
|
3
|
+
# A class which is instantiated by PDFToImage when a PDF document
|
4
|
+
# is opened.
|
3
5
|
class Image
|
6
|
+
attr_reader :pdf_name
|
4
7
|
attr_reader :filename
|
5
8
|
attr_reader :width
|
6
9
|
attr_reader :height
|
7
|
-
attr_reader :format
|
8
10
|
attr_reader :page
|
9
11
|
attr_reader :args
|
12
|
+
attr_reader :opened
|
10
13
|
|
11
|
-
#
|
12
|
-
# selfish, but I need to return to the main project
|
14
|
+
# ImageMagick methods that we currently support.
|
13
15
|
CUSTOM_IMAGE_METHODS = [
|
14
|
-
"resize"
|
16
|
+
"resize",
|
17
|
+
"quality"
|
15
18
|
]
|
16
19
|
|
17
20
|
CUSTOM_IMAGE_METHODS.each do |method|
|
@@ -25,20 +28,16 @@ module PDFToImage
|
|
25
28
|
# Image constructor
|
26
29
|
#
|
27
30
|
# @param [filename] The name of the image file to open
|
28
|
-
def initialize(filename)
|
31
|
+
def initialize(pdf_name, filename, page, page_size)
|
29
32
|
@args = []
|
30
33
|
|
34
|
+
@pdf_name = pdf_name
|
31
35
|
@filename = filename
|
36
|
+
@opened = false
|
37
|
+
@width = page_size[:width]
|
38
|
+
@height = page_size[:height]
|
32
39
|
|
33
|
-
|
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
|
40
|
+
@page = page
|
42
41
|
end
|
43
42
|
|
44
43
|
# Saves the converted image to the specified location
|
@@ -46,6 +45,9 @@ module PDFToImage
|
|
46
45
|
# @param [outname] The output filename of the image
|
47
46
|
#
|
48
47
|
def save(outname)
|
48
|
+
|
49
|
+
generate_temp_file
|
50
|
+
|
49
51
|
cmd = "convert "
|
50
52
|
|
51
53
|
if not @args.empty?
|
@@ -54,10 +56,7 @@ module PDFToImage
|
|
54
56
|
|
55
57
|
cmd += "#{@filename} #{outname}"
|
56
58
|
|
57
|
-
|
58
|
-
if $? != 0
|
59
|
-
raise PDFError, "Error converting file: #{cmd}"
|
60
|
-
end
|
59
|
+
PDFToImage::exec(cmd)
|
61
60
|
|
62
61
|
return true
|
63
62
|
end
|
@@ -74,22 +73,15 @@ module PDFToImage
|
|
74
73
|
|
75
74
|
private
|
76
75
|
|
77
|
-
def
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
76
|
+
def generate_temp_file
|
77
|
+
if @opened == false
|
78
|
+
cmd = "pdftoppm -png -f #{@page} -l #{@page} #{@pdf_name} #{@filename}"
|
79
|
+
PDFToImage::exec(cmd)
|
80
|
+
@filename = "#{@filename}-#{@page}.png"
|
81
|
+
@opened = true
|
83
82
|
end
|
84
83
|
|
85
|
-
|
86
|
-
dimensions = info[2].split('x')
|
87
|
-
|
88
|
-
return {
|
89
|
-
:format => info[1],
|
90
|
-
:width => dimensions[0],
|
91
|
-
:height => dimensions[1]
|
92
|
-
}
|
84
|
+
return true
|
93
85
|
end
|
94
86
|
|
95
87
|
end
|
data/lib/pdftoimage/version.rb
CHANGED
data/spec/pdftoimage_spec.rb
CHANGED
@@ -19,13 +19,21 @@ describe PDFToImage do
|
|
19
19
|
File.unlink('spec/tmp.jpg')
|
20
20
|
end
|
21
21
|
|
22
|
-
it "should allow resizing" do
|
22
|
+
it "should allow resizing and quality control" do
|
23
23
|
@pages = PDFToImage.open('spec/3pages.pdf')
|
24
|
-
@pages[0].resize('50%').save('spec/tmp2.jpg')
|
24
|
+
@pages[0].resize('50%').quality('80%').save('spec/tmp2.jpg')
|
25
25
|
File.exists?('spec/tmp2.jpg').should equal true
|
26
26
|
File.unlink('spec/tmp2.jpg')
|
27
27
|
end
|
28
28
|
|
29
|
+
it "should work with blocks" do
|
30
|
+
counter = 0
|
31
|
+
PDFToImage.open("spec/3pages.pdf") do |page|
|
32
|
+
counter = counter + 1
|
33
|
+
end
|
34
|
+
counter.should equal 3
|
35
|
+
end
|
36
|
+
|
29
37
|
|
30
38
|
end
|
31
39
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pdftoimage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 31
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Rob Flynn
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-11-
|
18
|
+
date: 2010-11-11 00:00:00 +00:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -80,7 +80,7 @@ files:
|
|
80
80
|
- spec/pdftoimage_spec.rb
|
81
81
|
- .document
|
82
82
|
has_rdoc: yard
|
83
|
-
homepage:
|
83
|
+
homepage: https://github.com/robflynn/pdftoimage
|
84
84
|
licenses:
|
85
85
|
- MIT
|
86
86
|
post_install_message:
|