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

Solution 1:

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

Solution 2:

There’s a Python script hidden in Automator.app that joins .PDF-files

/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py --help
Usage: join [--output <file>] [--shuffle] [--verbose]

Example usage:

/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py --output all.pdf *.pdf

Solution 3:

You can use pdfunite distributed with poppler. You can install poppler with Homebrew:

brew install poppler

And now use it:

pdfunite input1.pdf input2.pdf input3.pdf output.pdf

poppler also comes with these other commands: pdfdetach, pdffonts, pdfimages, pdfinfo, pdfseparate, pdftocairo, pdftohtml, pdftoppm, pdftops, pdftotext, in addition to pdfunite.