Is there a quick one-liner to combine multiple pdfs into one?
I know it can be done using Preview.app
Is there a quick one-liner to combine multiple pdfs into one?
I know it can be done using Preview.app
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 -s in ln -s ... ..., and without it, a hard link is created. Probably wouldn't matter, however though I'd mention it.
On newer versions of MacOS the command is
"/System/Library/Automator/Combine PDF Pages.action/Contents/MacOS/join" -o PATH/TO/YOUR/MERGED/FILE.pdf /PATH/TO/ORIGINAL/1.pdf /PATH/TO/ANOTHER/2.pdf /PATH/TO/A/WHOLE/DIR/*.pdf
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
gs so for future reference this solution would be just as convenient.
– htor
Dec 06 '17 at 15:26
gs installed by default and probably why Bartosz Petryński is suggesting it in his answer.
– user3439894
Dec 06 '17 at 15:51
I found Coherent PDF Command Line Tool to be the best option. It is free (for non-commercial use), 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
cpdf *.pdf -o output.pdf works beautifully if you have many pdfs to combine in one directory.
– spops
Jan 11 '21 at 21:53
You can use pdfunite which is part of the Poppler package and can be installed via Homebrew (brew install poppler).
pdfunite 1.pdf 2.pdf 3.pdf combined_pdf.pdf
Merges the three PDF files into combined_pdf.pdf.
find . -name \*.pdf -newer toDec3.pdf -print0 | parallel -0 -X -j1 -x pdfunite {} toDec12.pdf
– Joshua Goldberg
Dec 17 '21 at 18:59
Unimplemented Feature: Could not merge encrypted files ('file.pdf') trying to merge file.pdf which is not apparently encrypted (it opens in Preview.app just fine without any apparent decryption). ghostscript (see other answers) merged this file without issue.
– Von
Apr 17 '23 at 15:51
building on on @Bartosz Petryński's nice answer, we can make own minimal cpdf utility on top of GhostScript:
brew install gs
cpdf () {
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile="$1" "${@:2}"
}
then use it like this:
cpdf merged.pdf file1.pdf file2.pdf file3.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.
time command in conjunction with your joinpdfs.py script, the built-in join.py script and two files ~200 MB each, your joinpdfs.py script was 9 seconds faster than the built-in join.py script, with of course the added benefit of having the TOC intact. Nice! +1
– user3439894
Jan 18 '19 at 19:27
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.
join.py script from the Combine PDF Pages action in Automator, nothing else needs to be downloaded or installed, yet to use yours I'd have to download and install it. Also didn't see a binary at the link.
– user3439894
Jan 18 '19 at 18:29
If you want to be able to combine multiple pages onto one sheet, I wrote a tool that can do that (or just a normal combine). Mac PDFNUp
You can get the binary download on the releases button.
Usage: pdfnup --output <out> --nup 1/2/6 <filename>
brew cask install combine-pdfs
This is a GUI app that supports merging many pdf's. very powerful.
not exactly a commandline utility, adobe now offers an online utility to do just that:
join.pyit points topython. I do not have the command, butpython3. I solved it withbrew unlink python, but I get withpython "/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py"an error in the print line because it is written for python 2. And your link in the answer does not exist. – Timo Jun 26 '22 at 11:46"/System/Library/Automator/Combine PDF Pages.action/Contents/MacOS/join" -o ~/out.pdf pdf1.pdf pdf2.pdf– hulk Jan 24 '23 at 14:36