143

Is there a quick one-liner to combine multiple pdfs into one?

I know it can be done using Preview.app

reacuna
  • 1,680

10 Answers10

172

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.

Newer MacOS versions

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
sdt
  • 103
user3439894
  • 58,676
  • Worked for me. One great thing here is avoiding having to install extra packages that you may never use again. Thanks. – W7GVR Jan 27 '19 at 18:48
  • Worked for me on El Capitan 10.11.6. – Pierre-Adrien May 11 '19 at 13:06
  • 8
    Warning: I had PDFs where it messed up the rotation of several pages – adius Jun 26 '19 at 08:44
  • 6
    This really shouldn't be the accepted answer. This removes hyperlinks in the merged file. Some people are going to be very unhappy when they discover this and don't have access to the original files anymore. Preview.app screws up hyperlinks as well. Instead, use this: https://apple.stackexchange.com/a/365073/247390 – Daan Feb 02 '20 at 20:47
  • 7
    This doesn't seem to work for me anymore, the past year. I just get a blank output. – Chris F Carroll Apr 14 '20 at 16:12
  • Tried with 9 pdfs today on Big Sur, worked fine, thanks! – Devon May 12 '21 at 17:43
  • 3
    Still works on MacOS Monterey, just make sure you run it with Python2 (even though it's EOL ... ) – Gerard Apr 13 '22 at 14:57
  • 1
    Does not work anymore with neweest OSX, ideas? – hhh May 06 '22 at 07:58
  • 6
    In the first line of join.py it points to python. I do not have the command, but python3. I solved it with brew unlink python, but I get with python "/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
  • 1
    This script is written in Python 2, as suggested. I would recommend finding an alternative approach in 2022. – after-ephemera Sep 23 '22 at 22:36
  • 1
    macos ventura: "/System/Library/Automator/Combine PDF Pages.action/Contents/MacOS/join" -o ~/out.pdf pdf1.pdf pdf2.pdf – hulk Jan 24 '23 at 14:36
  • I don't find this script under Ventura (13.3.1) – Von Apr 17 '23 at 15:37
  • This works on Sonoma (v14), but removes the ability to highlight and copy text in the output PDF. – mtbno Mar 20 '24 at 10:55
129

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
  • OS X/macOS already natively has a way to combine PDF files from the command line, so why waste the time of having to first install Command Line Tools for Xcode, Homebrew and then Ghostscript, to do something that can already be done natively without having to install any of the aforementioned packages? – user3439894 Jul 27 '17 at 20:34
  • 9
    @user3439894 many people already have these common packages installed and are already using gs so for future reference this solution would be just as convenient. – htor Dec 06 '17 at 15:26
  • @htor, I have no idea why you are addressing your comment to me, as I'm not the author of the answer you're commenting to! That said, macOS does not have gs installed by default and probably why Bartosz Petryński is suggesting it in his answer. – user3439894 Dec 06 '17 at 15:51
  • 3
    @user3439894 i see the confusion. i'm addressing you because you were questioning the value of this answer. using homebrew to install command line utilities is very common these days, so i wouldn't call it a waste of time. for many people this answer provides an equally good solution as yours. – htor Dec 06 '17 at 17:05
  • @htor, Sorry I didn't even notice I had commented on this answer however I stand by my comment to this answer because the OP stated "Is there a quick one-liner to combine multiple pdfs into one?" and having to fist install Command Line Tools for Xcode, Homebrew and then Ghostscript is not a quick process compared to what is already natively available. Yes, for some users Homebrew is a good tool however the way this answer is written assumes that the OP or anyone else already has Command Line Tools for Xcode, Homebrew installed. Had it been written differently I might not have even commented. – user3439894 Dec 06 '17 at 18:07
  • 11
    this worked for me, the py command didn't – Thieme Hennis Mar 27 '18 at 20:23
  • 3
    As per Thieme Hennis' comment, the join.py script crashes for me in High Sierra with a segmentation fault. But gs works perfectly. – arcdale May 24 '18 at 08:06
  • /System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py – Quetzalcoatl Nov 19 '18 at 07:10
  • 1
    Combining 20X 60KB PDF files:
    Using the Python script >>> 22.1MB
    Using gs >>> 288KB (and way faster)
    – pooya13 Oct 16 '19 at 05:20
  • to install with Nix instead of homebrew: nix-env -iA nixpkgs.ghostscript – Henrik K Jun 10 '20 at 09:04
33

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
gwint
  • 441
  • 1
    This should be the accepted answer. Preview.app as well as join.py screw up hyperlinks. – Daan Feb 02 '20 at 20:52
  • 2
    Also, cpdf *.pdf -o output.pdf works beautifully if you have many pdfs to combine in one directory. – spops Jan 11 '21 at 21:53
  • Yep this is the best, it's also a one stop shop for all things you'd want to do with pdfs.... – FrodmanG Oct 15 '21 at 08:31
  • I used this approach to take a 625mb PDF down to 225mb, and I can't see the difference (used the squeeze option as well, to compress it!). I also tried the automator option, it worked, but much larger file size, as it didnt offer the option to compress (565 mb) – Brad Parks Jan 15 '22 at 14:55
  • 1
    I prefer an installation with brew, so I choose pdfunite. – Timo Jun 26 '22 at 11:53
26

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.

  • 2
    Or use a for-loop with numerical sorting, as suggested by: https://stackoverflow.com/a/23643544/861597 – Francis Laclé Oct 25 '21 at 01:22
  • With GNU Parallel and pdfunite, I used this for combining the most resumes added to a folder since the last time I checked: find . -name \*.pdf -newer toDec3.pdf -print0 | parallel -0 -X -j1 -x pdfunite {} toDec12.pdf – Joshua Goldberg Dec 17 '21 at 18:59
  • I tried pdfunite. It installs with Homebrew as described and works for some pdfs but with one pdf I get the error 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
17

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
7

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.

benwiggy
  • 35,635
  • 2
    As a test, using the 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
  • 1
    BTW 9 seconds might not sound like a lot but it was 1/3 the overall time of the built-in script, so 1/3 the time faster is significant. – user3439894 Jan 18 '19 at 19:34
  • The link doesn't work any more, but versions for Python 2 and 3 can now be found at https://github.com/benwiggy/PDFsuite/blob/master/legacy%20(python%202)/Automator_Scripts/joinpdfs.py and https://github.com/benwiggy/PDFsuite/blob/master/Python3/joinpdfs.py respectively. – mwfearnley Feb 11 '23 at 16:27
3

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.

  • Since Python is by default installed in and as a part of macOS, I do not necessarily consider it an external dependency per se. In other words, to use what is already available out of the box in macOS to join PDF files, the 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
  • 1
    BTW I did peruse your code and it's interesting that you are using two of the core components (Foundation and Quartz) that the python script is already using too. Is there anything in your code that is intrinsically better to sway ones usage of it over what is already offered by default without having to download anything, whereas with yours one has to take additional and extra steps to use it? – user3439894 Jan 18 '19 at 18:29
  • 1
    For people who don't use Python for anything else, it probably makes more sense to use the Apple-provided script. But for people who do use Python, it can be a big hassle to manage multiple versions (the system Python is 2.7, modern Python is 3.x) with different module installations and stuff. On my machine, for example, my $PATH points to a totally different Python. This can be solved with judicious use of shebangs and such, but I find it annoying. YMMV, of course. Also, direct link to binary download page: https://github.com/paultopia/pdfmerge/releases/latest – Paul Gowder Jan 19 '19 at 17:11
1

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>

Loren
  • 211
  • 2
  • 5
-1

Homebrew Formulae

brew cask install combine-pdfs

This is a GUI app that supports merging many pdf's. very powerful.

-2

not exactly a commandline utility, adobe now offers an online utility to do just that:

https://www.adobe.com/acrobat/online/merge-pdf.html

  • 2
    One of the worst things you can do is send your documents, pictures, music, whatever to an online service for “processing.” Any privacy you think you had, is gone out the window. Also, answers that deviate significantly from the OP’s premise aren’t likely to gain much traction – Allan Apr 21 '23 at 12:20
  • 1
    This answer would be most welcome on a question on how to merge PDF in general as opposed to someone seeking to script it locally on a machine. – bmike Apr 22 '23 at 12:59