My workflow for merging PDFs in Big Sur / Monterey

After updating my main machine to macOS Big Sur I discovered that Preview.app can no longer reliably combine several PDF files into one document. It works fine for small PDFs that only have a handful of pages, but I can make it crash with 100% reproducibility by trying to merge two 50-page documents into one. It seems this issue also exists in macOS Monterey: Preview crashes while combining PDFs | MacRumors Forums I also found several references to it on Apple’s support forum, so I’m definitely not alone.

I’m perfectly fine with merging PDFs in the terminal using Ghostscript – Ghostscript is very fast and it also optimises the output by significantly reducing its size without any degradation in visual quality. However, my wife also wants me to update her MacBook Pro to Big Sur, and this is going to be an issue for her as well. She definitely won’t be combining PDFs from the command line anytime soon, and installing Homebrew and Ghostscript on her machine is not an option.

It turns out that Automator has an action for combining PDF files that works fine. After giving the problem some thought (and also extensive googling), I ended up with the following Automator quick action:


With this action, I’m able to select PDF files in Finder, right click and choose “Merge PDF files” from the Quick Actions submenu. The output file is named " (merged).pdf" It works a treat!

The only downside is that it always combines PDFs in alphabetical order. In order to avoid renaming the files manually, I wrote the following script and added it to FastScripts:

-- prepends "_" (underscore) to names of selected files
tell application "Finder"
	set selectedFiles to (get selection)
	repeat with i from 1 to count of selectedFiles
		set currentFile to item i of selectedFiles
		set oldName to name of currentFile
		set newName to "_" & oldName
		set name of currentFile to newName
	end repeat
end tell

I tested it on a book that was delivered as a collection of PDF files with names like AppendixA.pdf, AppendixB.pdf, Chapter01.pdf…Chapter25.pdf and Foreword.pdf. First I quickly renamed some files using the “prepend underscore” script:

__Foreword.pdf
_Chapter01.pdf
[...]
_Chapter25.pdf
AppendixA.pdf
AppendixB.pdf

Then I simply selected all files with Cmd+A and ran the quick action on them. The book in question contained ~1400 pages, and while the whole process took about 3 minutes, it was definitely much faster than dragging and dropping pages in Preview by hand. So it seems I should actually thank Apple for breaking Preview and giving me an incentive to automate PDF merging :smile:

7 Likes

Thanks… I use a simpler process. In case of interest:

With the recent upgrade to macOS 12.2 Apple warned me that the “CombinePDFs” that is in Automator won’t work in a future version of the operating system. Don’t know the problem. Hope they fix it. Meantime seems to work.

1 Like

Ah, so the old CGPDFDocument API is going away. Apple has added a new PDFKit API a while ago, but I suspect the reason why Preview crashes when combining PDFs is precisely because it uses PDFKit now. :smiling_face_with_tear: