Skip to content

Commit faeb63e

Browse files
committed
Merge pull request #22 from Rubikan/master
Solution for Issue #21
2 parents 862f08e + df145d9 commit faeb63e

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

README.textile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,16 @@ We also support using other processors (the default is whatever version of Image
6363
pdf = Grim.reap('/path/to/pdf)
6464
</code></pre>
6565

66+
It is also supported to use another ghostscript executable, which is especially interesting for windows users since the executable is named differently there.
67+
68+
<pre><code>
69+
# specifying another ghostscript executable, win64 in this example
70+
# the ghostscript/bin folder still has to be in the PATH for this to work
71+
Grim.processor = Grim::ImageMagickProcessor.new({:ghostscript_path => "gswin64c.exe"})
72+
73+
pdf = Grim.reap('/path/to/pdf')
74+
</code></pre>
75+
6676
h2. Reference
6777

6878
* "jonmagic.com: Grim":http://jonmagic.com/blog/archives/2011/09/06/grim/

lib/grim/image_magick_processor.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@ class ImageMagickProcessor
33

44
# ghostscript prints out a warning, this regex matches it
55
WarningRegex = /\*\*\*\*.*\n/
6+
DefaultImagemagickPath = 'convert'
7+
DefaultGhostScriptPath = 'gs'
68

79
def initialize(options={})
8-
@imagemagick_path = options[:imagemagick_path] || 'convert'
9-
@ghostscript_path = options[:ghostscript_path]
10+
@imagemagick_path = options[:imagemagick_path] || DefaultImagemagickPath
11+
@ghostscript_path = options[:ghostscript_path] || DefaultGhostScriptPath
1012
@original_path = ENV['PATH']
1113
end
1214

1315
def count(path)
14-
command = ["-dNODISPLAY", "-q",
16+
command = [@ghostscript_path, "-dNODISPLAY", "-q",
1517
"-sFile=#{Shellwords.shellescape(path)}",
1618
File.expand_path('../../../lib/pdf_info.ps', __FILE__)]
17-
@ghostscript_path ? command.unshift(@ghostscript_path) : command.unshift('gs')
1819
result = `#{command.join(' ')}`
1920
result.gsub(WarningRegex, '').to_i
2021
end
@@ -28,7 +29,7 @@ def save(pdf, index, path, options)
2829
"-quality", quality.to_s, "-colorspace", colorspace,
2930
"-interlace", "none", "-density", density.to_s,
3031
"#{Shellwords.shellescape(pdf.path)}[#{index}]", path]
31-
command.unshift("PATH=#{File.dirname(@ghostscript_path)}:#{ENV['PATH']}") if @ghostscript_path
32+
command.unshift("PATH=#{File.dirname(@ghostscript_path)}:#{ENV['PATH']}") if @ghostscript_path && @ghostscript_path != DefaultGhostScriptPath
3233

3334
result = `#{command.join(' ')}`
3435

spec/lib/grim/image_magick_processor_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@
2020
end
2121
end
2222

23+
describe "#count with windows executable", :windows => true do
24+
before(:each) do
25+
@processor = Grim::ImageMagickProcessor.new({:ghostscript_path => "gswin64c.exe"})
26+
end
27+
28+
it "should return page count" do
29+
expect(@processor.count(fixture_path("smoker.pdf"))).to eq(25)
30+
end
31+
end
32+
2333
describe "#save" do
2434
before(:all) do
2535
@path = tmp_path("to_png_spec.png")

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'benchmark'
33
require 'rubygems'
44
require 'bundler/setup'
5+
require 'rbconfig'
56

67
require 'grim'
78

@@ -28,4 +29,5 @@ def tmp_path(name)
2829

2930
RSpec.configure do |config|
3031
config.include(FileHelpers)
32+
config.filter_run_excluding :windows => true if RbConfig::CONFIG['host_os'].match(/mswin|mingw|cygwin/) == nil
3133
end

0 commit comments

Comments
 (0)