blog.robotex.de

Using the pdf_toolkit gem on Windows

When trying to user the pdf_toolkit gem on windows, I got following error:

     Errno::ENOENT:
       No such file or directory - /dev/null

When you look at the gems source (lib/pdf/toolkit.rb) at line 175, you'll see this:

STDERR.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')

This does not work on my machine. So I created my first monkey patch in new file at Rails.root/lib/pdf_toolkit_patch.rb:

module PDF
  class Toolkit
    class <<self
      private
        def call_program(*args,&block)

          old_stream = nil
          options = args.last.is_a?(Hash) ? args.pop : {}
          options[:mode] ||= 'r' if block_given?
          unless options[:silence_stderr] == false
            old_stream = STDERR.dup
            #STDERR.reopen(RUBY_PLATFORM =~ /mswin/ ? 'NUL:' : '/dev/null')
            STDERR.reopen(RUBY_PLATFORM =~ /i386-mingw32/ ? 'NUL:' : '/dev/null')
            STDERR.sync = true
          end
          if options[:mode]
            command = (args.map {|arg| %{"#{arg.gsub('"','\\"')}"}}).join(" ")
            retval = IO.popen(command,options[:mode],&block)
            retval
            # block_given? ? $?.success? : retval
          else
            system(*args)
          end
        ensure
          STDERR.reopen(old_stream) if old_stream
        end
    end
  end
end

Then I added following line to the file, where I use the pdf-toolkit:

require 'pdf_toolkit_patch'

That's it! My first ruby monkeypatch!


Back