1

I have a directory with lots of PDFs that have spaces in their file names.

file 1.pdf
file 2.pdf
file 3.pdf
# And so on

I ran this command in that directory.

pdftk `ls -v` cat output combined-report.pdf

But the terminal spat out a bunch of errors like this.

Error: Unable to find file.
Error: Failed to open PDF file: 
   file
Error: Unable to find file.
Error: Failed to open PDF file: 
   1.pdf

How do I combine the PDFs using pdftk or any other package in Arch Linux? To clarify, I want to combine the files in the order printed by ls -v

Username
  • 3,463
  • 11
  • 68
  • 111
  • 1
    You should read "[Why you shouldn't parse the output of ls(1)](http://mywiki.wooledge.org/ParsingLs)" – Stephen P Jul 06 '20 at 23:48

2 Answers2

2

Just use a wildcard while creating combining the pdfs like:

pdftk *.pdf cat output newfile.pdf

Or else you could use something like this:

pdftk file\ 1.pdf file\ 2.pdf cat output newfile.pdf

param373r
  • 41
  • 1
  • 9
  • Nah, because the order that command combines the PDFs in goes like "1, 10, 11, 12..." instead of "1, 2, 3...". I edited my question to clarify – Username Jul 06 '20 at 19:37
  • 1
    Then, you can write a small bash script where you iterate over the files inside that directory... I could have done it for you... But I don't know bash that much. Rest good luck with that. ;) – param373r Jul 06 '20 at 19:40
1

Try this:

find . -name 'file*.pdf' -print0 | sort -z -V | xargs -0 -I{} pdftk {} cat output combined-report.pdf

or this:

ls -v file*.pdf | xargs -d'\n' -I{} pdftk {} cat output combined-report.pdf

In the first line, "-print0", "-z", and "-0" tell the corresponding command to use null as delimiter. The "-V" parameter for sort specifies "version sort" which I think should produce the sorting you wanted. Normally, the parameters that are piped are appended to the end of xargs. "-I{}" specifies a placeholder, "{}", that you can use to put them in the middle of the command.

The second line is similar, except that it takes parameter from "ls", and use newline '\n' as delimiter.

Note: there are potential problems with using "ls". See the link posted by @stephen-p.

Febtober
  • 110
  • 6
  • 1
    In both cases the code creates a PDF that is a copy of the last PDF in the directory. – Username Jul 07 '20 at 21:02
  • @Username Try to change the "*.pdf" to only match your input file. e.g. file*.pdf as in your example. Otherwise the wildcard expansion will also include combined-report.pdf that's created in the same directory. – Febtober Jul 07 '20 at 21:24
  • Didn't realize the asterisk became a markup for italic in my last comment. I edited the answer to be more precise about input filenames. – Febtober Jul 07 '20 at 21:32