Is there a quick one-liner to combine multiple pdfs into one?
I know it can be done using Preview.app
Ask Different is a question and answer site for power users of Apple hardware and software. It only takes a minute to sign up.Sign up to join this community
Have a look at "Combining PDF files on the command line in OSX" in Joining PDF Files in OS X From the Command Line.
It turns out that from Tiger onwards, OSX ships with a Python script that does exactly what you need. The script is already executable, and Python is pre-installed on OS X, so all you need to do to run it is opening the Terminal and typing
"/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
Also on the linked page it suggests making a symbolic link for the
join.py file to make typing easier however they omitted the
ln -s ... ..., and without it, a hard link is created. Probably wouldn't matter, however though I'd mention it.
Just install Ghostscript using Brew with command:
brew install gs
Then run command with all files listed:
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf source1.pdf source2.pdf source3.pdf
Apple's python script in the Automator action is very slow, as it uses CoreGraphics's CGPDFDocument APIs, rather than the newer PDFKit framework. It also imports the entire CoreGraphics library, rather than just the required APIs.
An alternative, faster python script, can be found here:
This script also adds a Table of Contents to the PDF, listing each component file (and merging existing ToCs), which Apple's does not.
It can be used on the command line (with PDF filenames as arguments), or in an Automator shell script action, to make a Quick Action/Service for the Finder.
FWIW, I've written a quick little program that lets you do this without having to rely on external dependencies like the system python and such. On github here: pdfmerge and pretty simple to use, can either pass it a list of PDF files to merge with
pdfmerge in1.pdf in2.pdf ... out.pdf, pass it a list of files to merge in a text file like
pdfmerge infileslist.txt out.pdf or just do the current directory in ABC order with
pdfmerge out.pdf. I wrote it as a learning project, so free and open and you can get the latest binary from the releases tab on github.
I found the free Coherent PDF Command Line Tool to be the best option. It is very fast, lossless, and does not mess up orientation or hyperlinks as some other solutions did. Format is:
cpdf file1.pdf file2.pdf -o output.pdf