Running a command line argument from rudy

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

I'm looking to append some pdfs. Pdftk
(http://www.pdflabs.com/tools/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. 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

···

--
Posted via http://www.ruby-forum.com/\.

Inspect the string before running it:

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

martin

···

On Thu, Apr 26, 2012 at 1:07 PM, Christopher D. <lists@ruby-forum.com> 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

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

Like martin mentioned you need to escape the slashes.

···

--
Posted via http://www.ruby-forum.com/.

Martin DeMello wrote in post #1058543:

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 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 '<main>'

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

···

On Thu, Apr 26, 2012 at 1:07 PM, Christopher D. <lists@ruby-forum.com> > wrote:

--
Posted via http://www.ruby-forum.com/\.

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 Fri, Apr 27, 2012 at 9:15 AM, Christopher D. <lists@ruby-forum.com> wrote:

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

Like martin mentioned you need to escape the slashes.

--
Posted via http://www.ruby-forum.com/\.

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

···

On Thu, Apr 26, 2012 at 1:38 PM, Christopher D. <lists@ruby-forum.com> 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 '<main>'

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:

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 '<main>'

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

Appreciate 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:

puts Dir.pwd
ex = Dir["P:\project\bin\pdftk.exe"]
file = Dir["P:\project\bin\combined.pdf"]

cmd = "#{ex} 01_cool.pdf 02_cool.pdf cat output #{file}"
puts cmd
`#{cmd}`

I get the same error. This is very perplexing to me.. I would enjoy
hearing any other suggestions

···

On Thu, Apr 26, 2012 at 1:38 PM, Christopher D. <lists@ruby-forum.com> > wrote:

--
Posted via http://www.ruby-forum.com/\.