MacOS – Mac OS X: How to merge pdf files in a directory according to their file names

macospdfsoftware-recommendation

I want to merge several hundred pdf files in a directory automatically according to their file names.

E.g.

The files
1000.1.pdf
1000.2.pdf
1000.3.pdf
1000.x.pdf
should be merged into 1000.pdf

and

2000.abc.pdf
2000.def.pdf
2000.ghi.pdf
2000.jkl.pdf
2000.5.pdf
into 2000.pdf.

I don’t want to use solutions based on Preview/Automator (if available) because compared to third party software like Adobe Acrobat or PDFpen merging pdf files often (depending on the source documents) results in a significant increase in file size (see e.g. What causes PDF file size to increase when saving in Preview?)

Do you have any recommendations? Thank you!

Best Answer

Try pdftk. It is command-line software that can join PDF files (and do lots of other stuff, too, but that isn't relevant here). You can download it from the official pdftk web page.

Sample syntax:

pdftk old1.pdf old2.pdf old3.pdf cat output new.pdf

will create the file new.pdf that contains the concatenation of the files old1.pdf, old2.pdf, old3.pdf.

To solve your problem, with your example filenames:

pdftk 1000.*.pdf cat 1000.pdf
pdftk 2000.*.pdf cat 2000.pdf

and so on. You can use shell scripting to make this completely automatic if desired (but you'll have to spend a little time on your own learning how to write shell scripts).


Assuming all files are named 1000.x, 2000.x etc. a shell script could look somehow like this

#!/bin/bash

for n in {1..9}; do
    if [[ -r ${n}000.1.pdf ]]; then
        rm -f ${n}000.pdf
        pdftk ${n}000.*.pdf cat ${n}000.pdf && mv ${n}000.*.pdf ~/.Trash/
    fi
done