How to combine pdf files from terminal
I’ve previously written about how to easily create a pdf from images in terminal, but when you need to merge multiple pdfs to one it’s another ballgame1. Usually I’ve resorted to Adobe Acrobat (Hello trial and paywall) or Preview. But preview is rather cumbersome. Luckily it seems like there is an easy terminal-based automator-script for this already included in OSX. Here is a quick guide to get you up and running.
Merge pdf pages
The script itself runs like this. So you could just copy this into your terminal and modify stuff.
"/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 however found it easier to create an alias so that I can run a shorter command.
How to set up an alias for pdfMerge
- Open your
~/.bash_profile
file in a text-editor - Add a new line with the text:
alias pdfMerge='"/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py" -o'
- Save the file.
- Quit the editor. Your new alias
pdfMerge
will be set for the next terminal you open. - Open a new Terminal window and try it out:
- Type in
pdfMerge output.pdf *.pdf
which means use Automator to convert all pdf-files in this folder and combine them to a pdf called output.pdf. - You now have one pdf-file with all your pdfs combined!
ProTip: Decide order
If you want the file in a specific order I recommend naming your files with numbers. F.ex: 1-onboarding.pdf, 2-signing.pdf. You can also type your files directly out like convert output.pdf onboarding.pdf signing.pdf
Source
PS
Yeah. I know that the output-file is at the end on the imagemagick solution, and first on the pdfMerge-solution. A bit annoying.
-
You can hack around this by still using imagemagic, but it doesn’t just combine them, it changes resolution etc. ↩