Search code examples
pdftk

Pdftk file merge: catenation is not picking up files in desired sequence


I am merging all pdf files in a folder. Files are named as

Q1.pdf Q2.pdf Q3.pdf ........ Q30.pdf

Command I am using

pdftk *.pdf cat output all.pdf

But in the output file (all.pdf) merged sequence is appearing as

Q10.pdf Q11.pdf ....Q19.pdf Q1.pdf Q20.pdf....Q29.pdf Q2.pdf 

The sequence I want is

Q1.pdf Q2.pdf ..... Q9.pdf Q10.0df Q11.pdf ...

What option should I add in pdftk command?


Solution

  • I suppose there is no such option but you can simply rename files before concatenating:

    find ./ -name Q\?.pdf | xargs -I  % sh -c 'name=`basename %`; newname=`echo $name | sed 's/Q/Q0/'`; mv $name $newname'
    

    (I'm almost sure it could be done in a simpler way but I just can't find that way right now)

    where:

    find ./ -name Q\?.pdf - finds files that have name starting with Q character, followed by a single character and .pdf extension and prints them into standard output

    xargs executes commands given after -c option on every filename printed by find commnad

    basename gives you filename without path to it (no folder names like "./" if you have searched in current folder)