40

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

I know it can be done using Preview.app

57

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.

  • Worked for me. One great thing here is avoiding having to install extra packages that you may never use again. Thanks. – gvrocha Jan 27 at 18:48
  • Worked for me on El Capitan 10.11.6. – pabuisson May 11 at 13:06
21

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
  • 4
    @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 6 '17 at 15:26
  • 1
    @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 6 '17 at 17:05
  • 6
    this worked for me, the py command didn't – Thieme Hennis Mar 27 '18 at 20:23
  • 1
    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 8:06
2

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.

  • 1
    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 at 19:27
  • 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 at 19:34
1

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 at 18:29
  • 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 at 18:29
  • 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: github.com/paultopia/pdfmerge/releases/latest – Paul Gowder Jan 19 at 17:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .