How to combine pdf files from terminal
I’ve previously written about how to easily create a pdf from images, 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_profilefile 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
pdfMergewill be set for the next terminal you open.
- Open a new Terminal window and try it out:
- Type in
pdfMerge output.pdf *.pdfwhich 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
Yeah. I know that the output-file is at the end on the imagemagick solution, and first on the pdfMerge-solution. A bit annoying.