diff --git a/lib/active_pdftk.rb b/lib/active_pdftk.rb index 6e1f61a..9e14400 100644 --- a/lib/active_pdftk.rb +++ b/lib/active_pdftk.rb @@ -4,4 +4,4 @@ require 'active_pdftk/field' require 'active_pdftk/form' require 'active_pdftk/fdf' -require 'active_pdftk/xfdf' \ No newline at end of file +require 'active_pdftk/xfdf' diff --git a/lib/active_pdftk/call.rb b/lib/active_pdftk/call.rb index f213eac..0f232e1 100644 --- a/lib/active_pdftk/call.rb +++ b/lib/active_pdftk/call.rb @@ -66,7 +66,8 @@ def initialize(args = {}) # # As you know command line programs take often a bunch of arguments, in a very specific syntax. that is our case. # In order to call pdftk in a easy way, we build a DSL, it has a hash patern, with four keys : input, operation, output, options. - # an additional key +:path+ can be set to specify the path to the pdftk binary. + # There are two additional keys. +:path+ can be set to specify the path to the pdftk binary. + # +tmpdir+ can be set to specify the path of the temp directory. # :input => some_input, :operation => some_operation, :output => some_output, :options => some_options # # ===Input @@ -184,7 +185,7 @@ def pdftk(dsl_statements = {}) dsl_statements = @default_statements.merge(dsl_statements) cmd = "#{@default_statements[:path]} #{set_cmd(dsl_statements)}" if dsl_statements[:operation].to_s.match(/burst|unpack_files/) - cmd.insert(0, "cd #{Dir.tmpdir} && ") + cmd.insert(0, "cd #{tmpdir} && ") end Open3.popen3(cmd) do |stdin, stdout, stderr| if @input @@ -196,7 +197,7 @@ def pdftk(dsl_statements = {}) raise(CommandError, {:stderr => @error, :cmd => cmd}) unless (@error = stderr.read).empty? end if dsl_statements[:operation].to_s.match(/burst|unpack_files/) && dsl_statements[:output].nil? - Dir.tmpdir + tmpdir else @output end @@ -295,6 +296,14 @@ def pdftk_version %x{#{@default_statements[:path]} --version 2>&1}.scan(/pdftk (\S*) a Handy Tool/).join end + # Return the directory to use as the temp directory + # + # @return [String] + # + def tmpdir + @default_statements[:tmpdir] || Dir.tmpdir + end + # Return the path of the pdftk library if it can be located. # # @return [String, nil] return nil if the library cannot be found on the system diff --git a/spec/active_pdftk/call_spec.rb b/spec/active_pdftk/call_spec.rb index 89bb40a..f5068b2 100644 --- a/spec/active_pdftk/call_spec.rb +++ b/spec/active_pdftk/call_spec.rb @@ -6,6 +6,7 @@ before :all do options = {} options[:path] = ENV['path'] unless ENV['path'].nil? + options[:tmpdir] = '/custom_temp' @pdftk = ActivePdftk::Call.new(options) end @@ -13,6 +14,10 @@ @pdftk.default_statements[:path].should_not be_nil end + it "should set the tmpdir" do + @pdftk.default_statements[:tmpdir].should == '/custom_temp' + end + it "should check the ENV vars" do unless ENV['path'].nil? || ENV['version'].nil? ENV['path'].should_not be_nil @@ -255,4 +260,14 @@ end end end + + context "#tmpdir" do + before :all do + @pdftk = ActivePdftk::Call.new + end + + it "should use the system tmpdir when a tmpdir is not explicity set" do + @pdftk.tmpdir.should == Dir.tmpdir + end + end end diff --git a/spec/active_pdftk/wrapper_spec.rb b/spec/active_pdftk/wrapper_spec.rb index a0b7ad2..0742d9c 100644 --- a/spec/active_pdftk/wrapper_spec.rb +++ b/spec/active_pdftk/wrapper_spec.rb @@ -58,8 +58,8 @@ def map_output_type(output_specified) it "should pass the defaults statements to the call instance." do path = ActivePdftk::Call.new.locate_pdftk - @pdftk_opt = ActivePdftk::Wrapper.new(:path => path, :operation => {:fill_form => 'a.fdf'}, :options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'}) - @pdftk_opt.default_statements.should == {:path => path, :operation => {:fill_form => 'a.fdf'}, :options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'}} + @pdftk_opt = ActivePdftk::Wrapper.new(:path => path, :tmpdir => '/other_tmp', :operation => {:fill_form => 'a.fdf'}, :options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'}) + @pdftk_opt.default_statements.should == {:path => path, :tmpdir => '/other_tmp', :operation => {:fill_form => 'a.fdf'}, :options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'}} end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 92b3254..6e27b68 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,7 @@ require File.expand_path('../../lib/active_pdftk', __FILE__) require 'rspec' +require 'digest' Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f} @@ -13,4 +14,4 @@ config.filter_run :focus => true config.run_all_when_everything_filtered = true -end \ No newline at end of file +end