Skip to content

Instantly share code, notes, and snippets.

@stvhwrd
Last active March 26, 2023 11:19
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stvhwrd/e292751b4b3ccfe8b1af to your computer and use it in GitHub Desktop.
Save stvhwrd/e292751b4b3ccfe8b1af to your computer and use it in GitHub Desktop.
Combining PDF files on the command line in OSX

###While this script does concatenate pdf files, I haven't yet figured out in which order it does so. On a sample of 19 pdf files, logically named 01something.pdf, 02something.pdf, 03something.pdf, etc. the order of the merged product seemed almost random...

...from Tiger onwards, OSX ships with a Python script that concatenates pdf files (merges them together). The script is already executable, and Python is pre-installed on OS X, so all you need to do is point it at your pdf files and run

"/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" -o PATH/TO/YOUR/MERGED/FILE.pdf /PATH/TO/ORIGINAL/1.pdf /PATH/TO/ANOTHER/2.pdf /PATH/TO/A/WHOLE/DIR/*.pdf

I prefer putting the link in /usr/local/bin, as it is in the $PATH and therefore I can run the command from anywhere. To set up the link, you need to navigate to the directory where you want the link.

cd /usr/local/bin
sudo ln "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" PDFconcat

Terminal will ask you for your password (you need Admin rights to do this). After the command is run, you will be able to concatenate PDF files anywhere on Terminal by typing

PDFconcat -o PATH/TO/YOUR/MERGED/FILE.pdf /PATH/TO/ORIGINAL/1.pdf /PATH/TO/ANOTHER/2.pdf /PATH/TO/A/WHOLE/DIR/*.pdf

You can even pass a ‘shuffle’ argument to make the script take one page per document in turn instead:

PDFconcat -s -o PATH/TO/YOUR/MERGED/FILE.pdf  /PATH/TO/DIR/*.pdf


credit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment