How to combine multiple groups of PDFs using the command line

command linemergepdfterminal

I have 1000 groups of 5 pdf files I need to combine/merge. I aware of the "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" command and have created the 1000 different versions of the command I need. Currently I am copying and pasting each separate line into Terminal to run it. Is there a way to put all 1000 lines into a script and run the script.
Thanks

Best Answer

The functionality to combine PDFs is built into Preview.app, and this can be a handy approach for dealing with small numbers of documents.

Recent versions of macOS also have a Python script that automates this process for multiple PDF files. This script is located at:

/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py

You can run the script directly in Terminal, but it might be handy to add an alias in your ~/.bash_profile:

alias combinepdfs='/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py'

Instead of typing the long location to the script every time, now you can run this script using a single command! (Don't forget to reload after making changes: source ~/.bash_profile.)


Say I have a directory of lecture notes ~/Documents/Lectures that contains the following files:

lec2.pdf  lec3.pdf  lec4.pdf  lec5.pdf  lec6.pdf  lec7.pdf  lec8.pdf  lec9.pdf lec10.pdf lec11.pdf lec12.pdf lec13.pdf lec14.pdf lec15.pdf lec16.pdf lec17.pdf 

I can combine these PDF files using the following command:

$ combinepdfs -o lec-combined.pdf lec{2..17}.pdf

The general usage pattern is listed in the help command:

$ combinepdfs --help

Usage: join [--output <file>] [--shuffle] [--verbose]

There is also an extra feature to shuffle pages:

--shuffle
#       Take a page from each PDF input file in turn before taking another from each file.
#       If this option is not specified then all of the pages from a PDF file are appended
#       to the output PDF file before the next input PDF file is processed.