Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Reimplemented being able to pass in convert/gs paths.

  • Loading branch information...
commit 3671c1d72d83f47baf328dc9a389c02558634325 1 parent 060b99c
Jonathan Hoyt authored
3  Gemfile
... ... @@ -1,4 +1,5 @@
1 1 source :rubygems
2 2 gemspec
3 3 gem 'rake'
4   -gem 'rspec'
  4 +gem 'rspec'
  5 +gem 'ruby-debug'
6 README.textile
Source Rendered
@@ -52,12 +52,12 @@ We also support using other processors (the default is whatever version of Image
52 52
53 53 <pre><code>
54 54 # specifying one processor with specific ImageMagick and GhostScript paths
55   - Grim.processor = Grim::ImageMagickProcessor.new('/path/to/convert', /path/to/gs/bin/')
  55 + Grim.processor = Grim::ImageMagickProcessor.new({:imagemagick_path => "/path/to/convert", :ghostscript_path => "/path/to/gs"})
56 56
57 57 # multiple processors with fallback if first fails, useful if you need multiple versions of convert/gs
58 58 Grim.processor = Grim::MultiProcessor.new(
59   - Grim::ImageMagickProcessor.new('/path/to/convert/bin/', /path/to/ghostscript/bin/'),
60   - Grim::ImageMagickProcessor.new('/path/to/convert/bin/', /path/to/ghostscript/bin/')
  59 + Grim::ImageMagickProcessor.new({:imagemagick_path => "/path/to/6.7/convert", :ghostscript_path => "/path/to/9.04/gs"}),
  60 + Grim::ImageMagickProcessor.new({:imagemagick_path => "/path/to/6.6/convert", :ghostscript_path => "/path/to/9.02/gs"})
61 61 )
62 62
63 63 pdf = Grim.reap('/path/to/pdf)
1  grim.gemspec
@@ -12,7 +12,6 @@ Gem::Specification.new do |s|
12 12 s.description = %q{Grim is a simple gem for extracting a page from a pdf and converting it to an image as well as extract the text from the page as a string. It basically gives you an easy to use api to ghostscript, imagemagick, and pdftotext specific to this use case.}
13 13
14 14 s.rubyforge_project = "grim"
15   - s.add_dependency 'safe_shell', '~> 1.0.0'
16 15
17 16 s.files = `git ls-files`.split("\n")
18 17 s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
2  lib/grim.rb
... ... @@ -1,5 +1,5 @@
1 1 # encoding: UTF-8
2   -require 'safe_shell'
  2 +require 'shellwords'
3 3
4 4 module Grim
5 5 extend self
36 lib/grim/image_magick_processor.rb
@@ -5,41 +5,33 @@ class ImageMagickProcessor
5 5 WarningRegex = /\*\*\*\*.*\n/
6 6
7 7 def initialize(options={})
8   - @imagemagick_bin_path = options[:imagemagick_bin_path]
9   - @ghostscript_bin_path = options[:ghostscript_bin_path]
  8 + @imagemagick_path = options[:imagemagick_path] || 'convert'
  9 + @ghostscript_path = options[:ghostscript_path]
10 10 @original_path = ENV['PATH']
11 11 end
12 12
13 13 def count(path)
14   - modify_path
15   - result = SafeShell.execute("gs", "-dNODISPLAY", "-q", "-sFile=#{path}", File.expand_path('../../../lib/pdf_info.ps', __FILE__))
16   - release_path
  14 + command = ["-dNODISPLAY", "-q",
  15 + "-sFile=#{Shellwords.shellescape(path)}",
  16 + File.expand_path('../../../lib/pdf_info.ps', __FILE__)]
  17 + @ghostscript_path ? command.unshift(@ghostscript_path) : command.unshift('gs')
  18 + result = `#{command.join(' ')}`
17 19 result.gsub(WarningRegex, '').to_i
18 20 end
19 21
20 22 def save(pdf, index, path, options)
21   - modify_path
22   -
23 23 width = options.fetch(:width, Grim::WIDTH)
24 24 density = options.fetch(:density, Grim::DENSITY)
25 25 quality = options.fetch(:quality, Grim::QUALITY)
26   - output = SafeShell.execute('convert', "-resize", width, "-antialias", "-render",
27   - "-quality", quality, "-colorspace", "RGB",
28   - "-interlace", "none", "-density", density,
29   - "#{pdf.path}[#{index}]", path)
30   -
31   - release_path
  26 + command = [@imagemagick_path, "-resize", width.to_s, "-antialias", "-render",
  27 + "-quality", quality.to_s, "-colorspace", "RGB",
  28 + "-interlace", "none", "-density", density.to_s,
  29 + "#{Shellwords.shellescape(pdf.path)}[#{index}]", path]
  30 + command.unshift("PATH=#{File.dirname(@ghostscript_path)}:#{ENV['PATH']}") if @ghostscript_path
32 31
33   - $? == 0 || raise(UnprocessablePage, output)
34   - end
35   -
36   - def modify_path
37   - ENV['PATH'] = "#{@imagemagick_bin_path}:#{ENV['PATH']}" if @imagemagick_bin_path
38   - ENV['PATH'] = "#{@ghostscript_bin_path}:#{ENV['PATH']}" if @ghostscript_bin_path
39   - end
  32 + result = `#{command.join(' ')}`
40 33
41   - def release_path
42   - ENV['PATH'] = @original_path
  34 + $? == 0 || raise(UnprocessablePage, result)
43 35 end
44 36 end
45 37 end
8 lib/grim/multi_processor.rb
@@ -6,8 +6,8 @@ def initialize(processors)
6 6
7 7 def count(path)
8 8 result = ""
9   - for i in 0..(@processors.length - 1)
10   - result = @processors[i].count(path)
  9 + @processors.each do |processor|
  10 + result = processor.count(path)
11 11 break if result != ""
12 12 end
13 13 result
@@ -15,8 +15,8 @@ def count(path)
15 15
16 16 def save(pdf, index, path, options)
17 17 result = true
18   - for i in 0..(@processors.length - 1)
19   - result = @processors[i].save(pdf, index, path, options)
  18 + @processors.each do |processor|
  19 + result = processor.save(pdf, index, path, options)
20 20 break if result
21 21 end
22 22 raise UnprocessablePage unless result
2  lib/grim/page.rb
@@ -45,7 +45,7 @@ def save(path, options={})
45 45 # Returns a String.
46 46 #
47 47 def text
48   - SafeShell.execute("pdftotext", "-enc", "UTF-8", "-f", @number, "-l", @number, @pdf.path, "-")
  48 + `#{["pdftotext", "-enc", "UTF-8", "-f", @number, "-l", @number, Shellwords.escape(@pdf.path), "-"].join(' ')}`
49 49 end
50 50 end
51 51 end
75 spec/lib/grim/image_magick_processor_spec.rb
@@ -18,16 +18,6 @@
18 18 it "should return page count" do
19 19 @processor.count(fixture_path("smoker.pdf")).should == 25
20 20 end
21   -
22   - it "should call modify_path" do
23   - @processor.should_receive(:modify_path)
24   - @processor.count(fixture_path("smoker.pdf"))
25   - end
26   -
27   - it "should call release_path" do
28   - @processor.should_receive(:release_path)
29   - @processor.count(fixture_path("smoker.pdf"))
30   - end
31 21 end
32 22
33 23 describe "#save" do
@@ -48,18 +38,6 @@
48 38 width, height = dimensions_for_path(@path)
49 39 width.should == 1024
50 40 end
51   -
52   - it "should call modify_path" do
53   - SafeShell.stub(:execute){true}
54   - @processor.should_receive(:modify_path)
55   - @processor.save(@pdf, 0, @path, {})
56   - end
57   -
58   - it "should call release_path" do
59   - SafeShell.stub(:execute){true}
60   - @processor.should_receive(:release_path)
61   - @processor.save(@pdf, 0, @path, {})
62   - end
63 41 end
64 42
65 43 describe "#save with width option" do
@@ -106,57 +84,4 @@
106 84 (lower_time < higher_time).should be_true
107 85 end
108 86 end
109   -
110   - describe "#modify_path" do
111   - describe "imagemagick_bin_path set" do
112   - it "should add /path/to/convert/ to beginning of path" do
113   - processor = Grim::ImageMagickProcessor.new({:imagemagick_bin_path => '/path/to/convert/'})
114   - processor.modify_path
115   - ENV['PATH'].split(':')[0].should == '/path/to/convert/'
116   - end
117   -
118   - after(:each) do
119   - ENV['PATH'] = @reset_to
120   - end
121   - end
122   -
123   - describe "ghostscript_bin_path set" do
124   - it "should add /path/to/convert/ to beginning of path" do
125   - processor = Grim::ImageMagickProcessor.new({:ghostscript_bin_path => '/path/to/gs/'})
126   - processor.modify_path
127   - ENV['PATH'].split(':')[0].should == '/path/to/gs/'
128   - end
129   -
130   - after(:each) do
131   - ENV['PATH'] = @reset_to
132   - end
133   - end
134   -
135   - describe "imagemagick_bin_path and ghostscript_bin_path set" do
136   - it "should add /path/to/convert/ to beginning of path" do
137   - processor = Grim::ImageMagickProcessor.new({:imagemagick_bin_path => '/path/to/convert/', :ghostscript_bin_path => '/path/to/gs/'})
138   - processor.modify_path
139   - ENV['PATH'].split(':')[0].should == '/path/to/gs/'
140   - ENV['PATH'].split(':')[1].should == '/path/to/convert/'
141   - end
142   -
143   - after(:each) do
144   - ENV['PATH'] = @reset_to
145   - end
146   - end
147   - end
148   -
149   - describe "#release_path" do
150   - before(:each) do
151   - ENV['PATH'] = '/grim/reaper/:/the/scythe/is/inevitable/'
152   - @processor = Grim::ImageMagickProcessor.new({:imagemagick_bin_path => '/path/to/convert/', :ghostscript_bin_path => '/path/to/gs/'})
153   - @processor.modify_path
154   - end
155   -
156   - it "should set path back to original" do
157   - ENV['PATH'].should == '/path/to/gs/:/path/to/convert/:/grim/reaper/:/the/scythe/is/inevitable/'
158   - @processor.release_path
159   - ENV['PATH'].should == '/grim/reaper/:/the/scythe/is/inevitable/'
160   - end
161   - end
162 87 end

0 comments on commit 3671c1d

Please sign in to comment.
Something went wrong with that request. Please try again.