Problem combining PDFs in Preview

I have a strange new problem in Preview.  I am trying to combine several different multi-page PDF's into one pdf.  I started with a sixty-page PDF.  I added another forty-page PDF by opening both documents.  I grabbed the thumbnails from the second document and dragged them into the space behind the last thumbnail in the first document.  So far so good.  Preview combined the two different documents in the desired order.


But now I am trying to add another hundred-page document.  I find that I can add only one or two pages at a time from this third document into the existing combination of the first two.  If I try to add more than two pages at a time, Preview gets stuck.  I then have to do a force-quit to reopen.   When Preview restarts, it opens the third document but not the first document (which combines the first two documents and the pages that I have been able to bring in from the third).   I have to manually open the first document.   

 

Neither PDF is encrypted.  These are just ordinary PDFs that seem indistinguishable from each other in format.  


What's going on here?  Does anyone know.?

Mac mini, macOS 12.5

Posted on Aug 25, 2022 11:35 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 25, 2022 11:52 AM

Without the specific PDFs to merge in order, its anyone's guess what is going on with Preview. There are PDFs whose content Preview simply cannot handle properly and that may be the simple answer. Without a true PDF editor ($), you can't merge PDFs on Monterey 12.3.1 and later because Apple removed the Python 2.7.18 distribution and that broke the Python code in Automator's Combine PDF action. But you can still combine PDFs…


Copy and paste the following AppleScript into Script Editor, click the compile (hammer) button, and then run it. It will prompt you for multple PDFs that you choose using the ⌘+key to add them in order. They will be combined into merged.pdf on your Desktop. If this works to merge those same PDFs, then you won't need to waste more time with Preview.


(*
  merge_pdf.applescript
  
  Prompt user for PDF filenames to merge using the ⌘-key for multiple selection.
  PDF will be merged in the order of their selection and written to an arbitrary
  PDF filename on the Desktop (in this example, merged.pdf)
  
  Reference: https://discussions.apple.com/thread/253764288
  Tested: macOS 11.6.8, 12.5.1
  Updated from 2021-06-24 version
  VikingOSX, 2022-05-31, Apple Support Communities, No warranty/support implied.
*)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "PDFKit"
use scripting additions

property NSString : a reference to current application's NSString
property NSArray : a reference to current application's NSArray
property NSURL : a reference to current application's NSURL
property PDFDocument : a reference to current application's PDFDocument
property merged_pdf_name : "~/Desktop/merged.pdf"

set PDFUrl to NSArray's array()'s mutableCopy()
set PDFRest to NSArray's array()

# change to absolute path
set mergedPDF to (NSString's stringWithString:merged_pdf_name)'s stringByStandardizingPath()

set msg to "Select multiple PDF using the ⌘-key in the order of your merge preference."
set pdfList to (choose file with prompt msg of type {"com.adobe.pdf"} default location (path to desktop) with multiple selections allowed)

# guard
if (count of pdfList) is 1 then return

# Change HFS paths to NSURL paths in list
repeat with afile in pdfList
	(PDFUrl's addObject:(NSURL's fileURLWithPath:(POSIX path of afile)))
end repeat

# Objective-C arrays are zero based, so get first PDF selection as base for merge
set outpdf to PDFDocument's alloc()'s initWithURL:(PDFUrl's objectAtIndex:0)
set PDFRest to PDFUrl's subarrayWithRange:(current application's NSMakeRange(1, (PDFUrl's |count|()) - 1))

repeat with pdfdoc in PDFRest
	set thisPDF to (PDFDocument's alloc()'s initWithURL:pdfdoc)
	repeat with n from 0 to (thisPDF's pageCount())
		(outpdf's insertPage:(thisPDF's pageAtIndex:n) atIndex:(outpdf's pageCount()))
	end repeat
end repeat
outpdf's writeToFile:mergedPDF
return


Similar questions

7 replies
Question marked as Top-ranking reply

Aug 25, 2022 11:52 AM in response to R_55a

Without the specific PDFs to merge in order, its anyone's guess what is going on with Preview. There are PDFs whose content Preview simply cannot handle properly and that may be the simple answer. Without a true PDF editor ($), you can't merge PDFs on Monterey 12.3.1 and later because Apple removed the Python 2.7.18 distribution and that broke the Python code in Automator's Combine PDF action. But you can still combine PDFs…


Copy and paste the following AppleScript into Script Editor, click the compile (hammer) button, and then run it. It will prompt you for multple PDFs that you choose using the ⌘+key to add them in order. They will be combined into merged.pdf on your Desktop. If this works to merge those same PDFs, then you won't need to waste more time with Preview.


(*
  merge_pdf.applescript
  
  Prompt user for PDF filenames to merge using the ⌘-key for multiple selection.
  PDF will be merged in the order of their selection and written to an arbitrary
  PDF filename on the Desktop (in this example, merged.pdf)
  
  Reference: https://discussions.apple.com/thread/253764288
  Tested: macOS 11.6.8, 12.5.1
  Updated from 2021-06-24 version
  VikingOSX, 2022-05-31, Apple Support Communities, No warranty/support implied.
*)

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "PDFKit"
use scripting additions

property NSString : a reference to current application's NSString
property NSArray : a reference to current application's NSArray
property NSURL : a reference to current application's NSURL
property PDFDocument : a reference to current application's PDFDocument
property merged_pdf_name : "~/Desktop/merged.pdf"

set PDFUrl to NSArray's array()'s mutableCopy()
set PDFRest to NSArray's array()

# change to absolute path
set mergedPDF to (NSString's stringWithString:merged_pdf_name)'s stringByStandardizingPath()

set msg to "Select multiple PDF using the ⌘-key in the order of your merge preference."
set pdfList to (choose file with prompt msg of type {"com.adobe.pdf"} default location (path to desktop) with multiple selections allowed)

# guard
if (count of pdfList) is 1 then return

# Change HFS paths to NSURL paths in list
repeat with afile in pdfList
	(PDFUrl's addObject:(NSURL's fileURLWithPath:(POSIX path of afile)))
end repeat

# Objective-C arrays are zero based, so get first PDF selection as base for merge
set outpdf to PDFDocument's alloc()'s initWithURL:(PDFUrl's objectAtIndex:0)
set PDFRest to PDFUrl's subarrayWithRange:(current application's NSMakeRange(1, (PDFUrl's |count|()) - 1))

repeat with pdfdoc in PDFRest
	set thisPDF to (PDFDocument's alloc()'s initWithURL:pdfdoc)
	repeat with n from 0 to (thisPDF's pageCount())
		(outpdf's insertPage:(thisPDF's pageAtIndex:n) atIndex:(outpdf's pageCount()))
	end repeat
end repeat
outpdf's writeToFile:mergedPDF
return


Aug 25, 2022 12:43 PM in response to R_55a

This should be as simple as copy/paste the AppleScript that I provided into Script Editor. Click the hammer icon to compile it which it appears you did, and then click the ▶︎ button to run the script. In the File Chooser, click the first PDF, and then press the ⌘-key while selecting other PDFs. The order in which you select the PDFs will be their combined order. When you have finished selecting the PDFs, click the Choose button and in a moment, a merged.pdf will appear on your Desktop. You needn't learn or write any AppleScript to use this tool.


If you click the Cancel button on that File chooser, then you abort the AppleScript as the error "User Canceled" number -128 shows that you did.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Problem combining PDFs in Preview

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.