It's boring when it works!

blog:join_pdf

Merge sorted PDF files

I had the problem that I had to merge 326 PDF files into one. These files are catalog pages of an online flip catalog that was not available as a single download file. So I saved all these single pages, the file names are bk_1.pdf, bk_2.pdf …. bk_326.pdf.

To create a PDF file from these files, I use pdfjam (also known as pdfjoin). The problem was that the file list is not sorted, so the resulting PDF file doesn't have the correct page order. pdfjam can't sort the input files, so we need something else for that.

This task could be done by the tools find and sort. But, the sort parameter is not at the beginning of the filename, so we need a sort option to do this job properly and pass the resulting filename as input to pdfjam.

find ../Downloads -name bk_*.pdf -print | sort -V | xargs pdfjam --landscape -o catalog.pdf

This command line finds any file with the pattern bk_*.pdf and the result is sorted. You need the -V option to sort these filenames with a number in the middle of the alphanumeric filename. The result is the input of xargs, which then calls pdfjam with the sorted list of filenames.