Skip to content

Commit 13799e0

Browse files
committed
Merge pull request #20 from jonmagic/pdftotext-path
Allow passing full path to pdftotext
2 parents 4e82746 + 39cac51 commit 13799e0

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

lib/grim/page.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ class Page
88
#
99
# pdf - the pdf this page belongs to
1010
# index - the index of the page in the array of pages
11+
# options - A Hash of options.
12+
# :pdftotext_path - The String path of where to find the pdftotext
13+
# binary to use when extracting text
14+
# (default: "pdftotext").
1115
#
12-
def initialize(pdf, index)
16+
def initialize(pdf, index, options = {})
1317
@pdf = pdf
1418
@index = index
1519
@number = index + 1
20+
@pdftotext_path = options[:pdftotext_path] || 'pdftotext'
1621
end
1722

1823
# Extracts the selected page and turns it into an image.
@@ -45,7 +50,7 @@ def save(path, options={})
4550
# Returns a String.
4651
#
4752
def text
48-
`#{["pdftotext", "-enc", "UTF-8", "-f", @number, "-l", @number, Shellwords.escape(@pdf.path), "-"].join(' ')}`
53+
`#{[@pdftotext_path, "-enc", "UTF-8", "-f", @number, "-l", @number, Shellwords.escape(@pdf.path), "-"].join(' ')}`
4954
end
5055
end
51-
end
56+
end

lib/grim/pdf.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,15 @@ class Pdf
99
# variables if pdf is found.
1010
#
1111
# path - A String or Path to the pdf
12+
# options - A Hash of options.
13+
# :pdftotext_path - The String path of where to find the pdftotext
14+
# binary to use when extracting text
15+
# (default: "pdftotext").
1216
#
13-
def initialize(path)
17+
def initialize(path, options = {})
1418
raise Grim::PdfNotFound unless File.exists?(path)
1519
@path = path
20+
@pdftotext_path = options[:pdftotext_path] || 'pdftotext'
1621
end
1722

1823
# Shells out to ghostscript to read the pdf with the pdf_info.ps script
@@ -43,14 +48,14 @@ def count
4348
#
4449
def [](index)
4550
raise Grim::PageNotFound unless index >= 0 && index < count
46-
Grim::Page.new(self, index)
51+
Grim::Page.new(self, index, pdftotext_path: @pdftotext_path)
4752
end
4853

4954
def each
5055
(0..(count-1)).each do |index|
51-
yield Grim::Page.new(self, index)
56+
yield Grim::Page.new(self, index, pdftotext_path: @pdftotext_path)
5257
end
5358
end
5459

5560
end
56-
end
61+
end

spec/lib/grim/page_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@
4747
describe "#text" do
4848
it "should return the text from the selected page" do
4949
pdf = Grim::Pdf.new(fixture_path("smoker.pdf"))
50-
pdf[1].text.should == "Step 1: get someone to print this curve for you to scale, 72” wide\n\nStep 2: Get a couple 55 gallon drums\n\n\f"
50+
pdf[1].text.should == "Step 1: get someone to print this curve for you to scale, 72” wide\nStep 2: Get a couple 55 gallon drums\n\n\f"
51+
end
52+
53+
it "works with full path to pdftotext" do
54+
pdftotext_path = `which pdftotext`.chomp
55+
pdf = Grim::Pdf.new(fixture_path("smoker.pdf"), pdftotext_path: pdftotext_path)
56+
pdf[1].text.should == "Step 1: get someone to print this curve for you to scale, 72” wide\nStep 2: Get a couple 55 gallon drums\n\n\f"
5157
end
5258
end
5359
end

0 commit comments

Comments
 (0)