Search code examples
ruby-on-railsrubypdftk

Ruby/Rails: pass arguments to command line application from rake task


How do you pass arguments from within a rake task and execute a command-line application from Ruby/Rails?

Specifically I'm trying to use pdftk (I can't find a comparable gem) to split some PDFs into individual pages. Args I'd like to pass are within < > below:

$ pdftk <filename.pdf> burst output <filename_%04d.pdf>

Solution

  • In the ruby code for your rake task:

    `pdftk #{input_filename} burst output #{output_filename}`
    

    You could also do:

    system("pdftk #{input_filename} burst output #{output_filename}")
    

    system() just returns true or false. backticks or %x() returns whatever output the system call generates. Usually backticks are preferred, but if you don't care, then you could use system(). I always use backticks because it's more concise.

    More info here: http://rubyquicktips.com/post/5862861056/execute-shell-commands