Running a command line argument from ruby

Straight up. I have to use windows @ work but I still require some
guidance.

I’m looking to append some pdfs. Pdftk
(PDFtk - The PDF Toolkit) works in the cmd
prompt.

Lets say I have
01_cool.pdf
02_cool.pdf

If I’m in cmd I can type

pdftk *.pdf cat output combined.pdf
#=> A new file called combined.pdf which is the concatenation of the two
pdfs

I need to be able to run this in ruby because I would like to eventually
append all files that have the same names in two directories and put it
into one final directory. To test pdftk is working this is the code I
came up with.

pdfs = Dir[“*.pdf”].sort.join(" ")
pdftk #{pdfs} cat output combined.pdf

Which results in an error: "No file or directory -pdftk [“01_cool.pdf”,
“02_cool.pdf”] cat output combined.pdf

On Thu, Apr 26, 2012 at 1:07 PM, Christopher D. [email protected]
wrote:

into one final directory. As a test this is the code I came up with.

pdfs = Dir[“*.pdf”].sort.join(" ")
pdftk #{pdfs} cat output combined.pdf

Which results in an error: "No file or directory -pdftk [“01_cool.pdf”,
“02_cool.pdf”] cat output combined.pdf

Inspect the string before running it:

pdfs = Dir[“*.pdf”].sort.join(" ")
cmd = “pdftk #{pdfs} cat output combined.pdf”
puts cmd
#{cmd}

martin

Martin DeMello wrote in post #1058543:

On Thu, Apr 26, 2012 at 1:07 PM, Christopher D. [email protected]
wrote:

into one final directory. As a test this is the code I came up with.

pdfs = Dir[“*.pdf”].sort.join(" ")
pdftk #{pdfs} cat output combined.pdf

Which results in an error: "No file or directory -pdftk [“01_cool.pdf”,
“02_cool.pdf”] cat output combined.pdf

Inspect the string before running it:

pdfs = Dir[“*.pdf”].sort.join(" ")
cmd = “pdftk #{pdfs} cat output combined.pdf”
puts cmd
#{cmd}

martin

Martin, thanks for getting back to me.

So I ran your code which resulted in the same error
pdfappend.rb:18:in ``': No such file or directory - pdftk 01_cool.pdf
02_cool.pdf cat output combined.pdf (Errno::ENOENT) from
pdfappend.rb:18:in ‘’

I also tried putting quotations around the #{cmd} and that run with no
errors but did not output my new file

Martin DeMello wrote in post #1058550:

On Thu, Apr 26, 2012 at 1:38 PM, Christopher D. [email protected]
wrote:

So I ran your code and it put out the line but then had an error
pdfappend.rb:18:in ``': No such file or directory - pdftk 01_cool.pdf
02_cool.pdf cat output combined.pdf (Errno::ENOENT) from
pdfappend.rb:18:in ‘’

I also tried putting quotations around the #{cmd} and that run with no
errors but did not output my new file

It looks like it isn’t finding the pdftk executable. I’m not sure how
windows does path lookups - try calling it with the full path to pdftk
(e.g. “c:\program files\pdftk.exe #{pdfs}”) instead of just pdftk. You
will probably need to escape the backslashes too.

martin

Thank you for the follow up.

So the pdftk definitely works but only in the command line. If I write
out

pdftk 01_cool.pdf 02_cool.pdf cat output combined.pdf
#=> combined.pdf will be made and it is a combination of the former pdfs

I looked at your suggestion and tried several iterations such as:

ex = Dir[“P:\project\bin\pdftk.exe”]
file = Dir[“combined.pdf”] #This is the same as
Dir[“P:\project\bin\combined.pdf”]

puts Dir.pwd

cmd = “#{ex} 01_cool.pdf 02_cool.pdf cat output #{file}”
puts cmd
#{cmd}

I get an error ``': No such file or directory - [] 01_cool.pdf
02_cool.pdf cat output [] Errno::ENOENT

I would like to point out that it looks like the directory is not being
pikced up which is denoted by [] in the error line.

If I make cmd = “pdftk 01_cool.pdf 02_cool.pdf cat output combined.pdf”

It will fill in the [] (obviously because it is a string) but is still
not finding the directory… so I’m thinking it might have to do with the
combined.pdf part of the code?

This is very perplexing to me… I would appreciate hearing any other
suggestions

I couldn’t edit my above post but I figured it out. Thank you martin!

Like martin mentioned you need to escape the slashes.

Glad you got it working. One further note: you don’t need to say things
like

ex = Dir[“P:\project\bin\pdftk.exe”]
file = Dir[“P:\project\bin\combined.pdf”]

all the Dir[…] command does is expand shell wildcards for you. So
Dir["*.pdf"] will return an array of all the pdf files in the current
directory, but Dir[“combined.pdf”] will just return back
[“combined.pdf”] since there are no wildcards to expand.

martin

On Thu, Apr 26, 2012 at 1:38 PM, Christopher D. [email protected]
wrote:

So I ran your code and it put out the line but then had an error
pdfappend.rb:18:in ``': No such file or directory - pdftk 01_cool.pdf
02_cool.pdf cat output combined.pdf (Errno::ENOENT) from
pdfappend.rb:18:in ‘’

I also tried putting quotations around the #{cmd} and that run with no
errors but did not output my new file

It looks like it isn’t finding the pdftk executable. I’m not sure how
windows does path lookups - try calling it with the full path to pdftk
(e.g. “c:\program files\pdftk.exe #{pdfs}”) instead of just pdftk. You
will probably need to escape the backslashes too.

martin