How do I automate opening a PDF in Preview, opening Print, Save as PDF--without opening preview??!

I have PDFs with form field data I have to annotate. I can't annotate them because macOS thinks they are locked. I can open them in Preview without any problem. However I have to Print, then Save as PDF, then open that new PDF to annotate them. It is perhaps the most annoying task I have to do on my Mac on a day to day basis and I have to do it dozens of times.


I'm desperately trying to automate this task using Automator, or Script Editor, or Shortcuts--however no solution works. The closest I've been able to get is macOS creating a new PDF which I can edit, but it strips out all the form field data.


  • macOS can't flatten a PDF with form field data if it's locked
  • macOS can't use OCR on a PDF if it's locked


Is there any way to automate opening a PDF in preview, clicking Print, then Save as PDF 1) without actually opening Preview and 2) without installing 3rd party software?


I know this is an incredibly common task on macOS for a multitude of users and I can't believe Apple wouldn't provide a way to automate this by 2025--so I assume it's here and I'm simply not discovering the right path here.


Posted on Sep 5, 2025 5:24 PM

Reply
18 replies

Oct 20, 2025 11:54 AM in response to AZsport115

Solution:


I downloaded the command line tools for macOS. I then created a Quick Action. Here are the settings for the Quick Action:


For Workflow receives current, set value to PDF Files in Finder.


From the Actions list on the left, drag Run AppleScript into your workflow on the right. Paste the below code into the workflow:



on run {input, parameters}

repeat with theFile in input

set inputFile to POSIX path of theFile

set tempFile to inputFile & ".tmp.pdf"

-- Use Swift to flatten PDF with burnInAnnotationsOption

do shell script "xcrun swift - << 'SWIFTCODE' " & quoted form of inputFile & " " & quoted form of tempFile & "

import Foundation

import PDFKit


let args = CommandLine.arguments

guard args.count == 3 else {

print(\"Error: Invalid arguments\")

exit(1)

}


let inputPath = args[1]

let outputPath = args[2]


// Read the PDF

guard let inputURL = URL(string: \"file://\" + inputPath),

let pdfDoc = PDFDocument(url: inputURL) else {

print(\"Error: Could not read PDF\")

exit(1)

}


// Save with burnInAnnotationsOption to flatten form fields and annotations

let outputURL = URL(fileURLWithPath: outputPath)

let options: [PDFDocumentWriteOption: Any] = [

.burnInAnnotationsOption: true

]


let success = pdfDoc.write(to: outputURL, withOptions: options)


if !success {

print(\"Error: Could not write PDF\")

exit(1)

}


print(\"Success\")

exit(0)

SWIFTCODE

"

-- Replace original with flattened version

do shell script "mv " & quoted form of tempFile & " " & quoted form of inputFile

end repeat

return input

end run




After doing so, you will be able to right click a PDF in Finder, run this Quick Action, and it will create a new PDF for you. That new PDF will flatten all form data and burn in all annotations--just like if you had opened the file in Preview, navigated to the Print dialog box, and then selected Save as PDF. This simulates that process without having to do all of that.

Oct 22, 2025 1:55 PM in response to AZsport115

There were a couple of typo's on the following line:

optDic's setValue(true) forKey:(ca's PDFDocumentBurnInAnnotationOption)


Corrected:

optDict's setValue:(true) forKey:(ca's PDFDocumentBurnInAnnotationsOption)


Otherwise, the proper usage in a Shortcut from the code I provided above:


AppleScript

use framework "Foundation"
use framework "PDFKit"
use scripting additions

property ca : current application
property SUFFIX : "_withOCR"

on run {input, parameters}
	set sysvers to system version of (system info)
	set vers to text 1 thru ((offset of "." in sysvers) - 1) of sysvers
	
	-- select a PDF that you know has scanned text that has not been OCR'd
	-- set thisPDF to POSIX path of (choose file of type "PDF") as text
	display dialog vers as text
	set thisPDF to POSIX path of (input as text)
	set ext to (ca's NSString's stringWithString:thisPDF)'s pathExtension()
	set outPDF to (ca's NSString's stringWithString:thisPDF)'s stringByDeletingPathExtension()
	set outPDF to outPDF's stringByAppendingString:SUFFIX
	set outPDF to outPDF's stringByAppendingPathExtension:ext
	
	set optDict to ca's NSMutableDictionary's dictionary()
	if vers = "14" then
		optDict's setValue:(true) forKey:(ca's PDFDocumentSaveTextFromOCROption)
	else if vers ≥ "15" then
		-- optDict's setValue:(true) forKey:(ca's PDFDDocumentSaveTextFromOCROption)
		-- optDict's setValue:(true) forKey:(ca's PDFDocumentOptimizeImagesForScreenOption)
		-- optDict's setValue:(true) forKey:(ca's PDFDocumentSaveImagesAsJPEGOption)
		optDict's setValue:(true) forKey:(ca's PDFDocumentBurnInAnnotationsOption)
	else
		return
	end if
	
	set pdf to ca's PDFDocument's alloc()'s initWithURL:(ca's NSURL's fileURLWithPath:thisPDF)
	-- write outPDF as an OCR'd PDF
	pdf's writeToFile:outPDF withOptions:optDict
	
	return input
end run


And the Shortcut itself:



I just tested this Shortcut on a PDF with several highlight annotations and this wrote a PDF with all of the annotations burned (flattened) into it. Tested on macOS 26.0.1.

Oct 20, 2025 5:56 PM in response to AZsport115

You could do it with a maco editor like Keyboard Maestro. You would first have to rename the file or duplicate it and rename it to a given file name. I used "x.pdf" in my test. The Keyboard Maestro macro looks like this:




It opens x.pdf, does a ⌘P to print, clicks on PDF, and hits the right buttons to print to PDF, replaces the original x.pdf file and closes the Preview window.


There's a trial version that you can use to test the concept. It's a $36 one time purchase with free upgrades. It's my primary utility for my everyday operation.




[Edited by Moderator]

Oct 21, 2025 5:43 PM in response to AZsport115

Apple's PDFKit recognizes PDF form fields as a PDFAnnotationSubtypeWidget and the original PDF designer that created the form fields chooses to mark them locked or not, and in the case of a Date form, a min and max date range, and the allowed date string format can be configured. Depending on the form design tool, there may be additional JavaScript code in the form controlling its behavior. Preview ignores PDF-based JavaScript.


I created a Date form in a LibreOffice Writer .odt document and then exported to PDF with either PDF, or XML form types. One version was locked and the other not.


One can filter on the annotation's fieldName (the name the form designer gave the field), or by matching the annotation's widgetStringValue to the date string in the form field with a regex pattern. Further, if the form field is not locked, one can update the widgeStringValue with a new date string, and then save the PDF to itself to update its content. In this scenario, one does not need any write options since you are not destroying any annotations (e.g. form fields) by flattening the annotation layer.


Ironically, Preview does not show the Inspector's filling in existing form fields permission as locked, but the Swift code checked if the annotation's isReadOnly flag was not true and that was ignored, when it should have detected the Date form field lock setting I made earlier.

Oct 22, 2025 5:28 AM in response to AZsport115

The thing about Swift is that eventually, someone at Apple will deprecate or change something in the language or frameworks and your Swift handler will break, throwing you into an expletive-rich debugging session.


Here is an AppleScript/Objective-C solution with less code and does not require command line tools installation. It is designed to OCR scanned PDFs, but you could add a Dictionary (optDict) key to use the PDFDocumentBurnInAnnotationsOption (Objective-C) option for PDF flattening.


-- PDFKit_OCR.applescript

-- Applies Optical Character Recognition (OCR) to a scanned PDF so the text
-- is searchable. Optionally, can flatten annotations in a PDF using the
-- PDFDocumentBurnInAnnotationOption key.
-- Tested: macOS Sequoia 15.7.1 and Tahoe 26.0.1
-- VikingOSX, 2025-10-22, Apple Support Communities, No warranties of any kind.

use framework "Foundation"
use framework "PDFKit"
use scripting additions

property ca : current application
property SUFFIX : "_withOCR"

set sysvers to system version of (system info)
set vers to text 1 thru ((offset of "." in sysvers) - 1) of sysvers

-- select a PDF that you know has scanned text that has not been OCR'd
set thisPDF to POSIX path of (choose file of type "PDF") as text
set ext to (ca's NSString's stringWithString:thisPDF)'s pathExtension()
set outPDF to (ca's NSString's stringWithString:thisPDF)'s stringByDeletingPathExtension()
set outPDF to outPDF's stringByAppendingString:SUFFIX
set outPDF to outPDF's stringByAppendingPathExtension:ext

set optDict to ca's NSMutableDictionary's dictionary()
if vers = "14" then
	optDict's setValue:(true) forKey:(ca's PDFDocumentSaveTextFromOCROption)
else if vers ≥ "15" then
	optDict's setValue:(true) forKey:(ca's PDFDDocumentSaveTextFromOCROption)
	optDict's setValue:(true) forKey:(ca's PDFDocumentOptimizeImagesForScreenOption)
	optDict's setValue:(true) forKey:(ca's PDFDocumentSaveImagesAsJPEGOption)
	-- optDict's setValue(true) forKey:(ca's PDFDocumentBurnInAnnotationOption)
else
	return
end if

set pdf to ca's PDFDocument's alloc()'s initWithURL:(ca's NSURL's fileURLWithPath:thisPDF)
-- write outPDF as an OCR'd PDF
pdf's writeToFile:outPDF withOptions:optDict
return



Oct 23, 2025 9:44 AM in response to AZsport115

Even with a PDF selected in the Finder, if you attempt to run the Shortcut directly from the Shortcuts panel of Shortcuts, or with the Shortcut open and clicking its run button, it does nothing since it is designed to be run only as a Quick Action from the Finder's secondary menu and no Shortcut Input was available.


However, you can run the Shortcut from the Terminal in the following manner:

shortcuts run "Shortcut name" -i input_annnotation.pdf


and it will write the output PDF to the same path location. That works on Tahoe 26.0.1.


I have no idea why you got that missing value message, unless possibly:

  1. You ran this Shortcut on an older version of macOS between Monterey and Sequoia where the PDFDocumentBurnInAnnotationsOption enum may not have been implemented in PDFKit yet.
  2. No output PDF filename was available for the writeToFile argument
  3. No valid PDF filename was available to PDFDocument



Oct 22, 2025 1:52 PM in response to Old Toad

The reason I had to open a PDF in Preview, then Print, then Save as PDF is because that was the only way I could figure out how to burn in annotations and save form data placed inside a PDF. I could not find any other way to do that, including using the Create PDF quick action in Finder native to macOS when you select 2 or more PDFs--even that action stripped out the annotations when it combined the PDF with another PDF. Make PDF, Create PDF both in Automator and Shortcuts also did not save annotations.


Because I need to burn in annotations and save form data in PDFs hundreds of times, I was looking for a way to do this all inside Finder, without opening Preview. Doing it through Preview, even via a Shortcut, was at least 30 seconds (on my 2019 Macbook Pro). Given my daily tasks, that adds up.


Thankfully, after installing the commandline tools, the code I posted in an earlier message will burn in annotations and flatten form data in a PDF (resulting in a PDF similar to what I'd see by using Preview) in 1 second, all from a Quick Action I run in Finder! I put the set up at the top in case anybody wants to try it out.


I did notice however, the AppleScript I provided only works when run as a Quick Action. That same AppleScript won't work inside a Shortcut if run it Inside a Shortcut with the Run AppleScript action. I assume it has something to do with how Shortcuts handles/allows/disallows for Swift or commandline actions, but I am speculating as I still don't know.


I'm still trying to find a way to do this inside a Shortcut since there are several things I'd like to do after flattening the PDF, via a Shortcut. Right now I'm running the aforementioned code to flatten the PDF via Quick Action in Finder, then running the Shortcut on that same PDF after it's been flattened.

Oct 25, 2025 6:52 AM in response to AZsport115

When a PDF has only a Finder Lock (small icon in the lower left corner) on the PDF icon, this Shortcut will produce an output PDF with flattened content. Alternatively, if that Finder PDF Icon is blank with a large lock symbol on it, then it is a password encrypted and locked PDF. In that case, the Shortcut will generate an empty PDF because the ability to copy the PDF content is inherently blocked.


In the latter case, one can test if the PDF document isEncrypted. When/if you know the password, then there is an unlockWithPassword method that would do that, and then the Shortcut can produce the expected flattened PDF result. The Shortcut would need to prompt you for that password and provide it to that method. If you don't know the password of an Encrypted/Locked PDF, then the shortcut should just quit as it has no means to produce anything other than the observed empty PDF.


Tested: macOS 15.7.1 with an encrypted PDF created by Master PDF Editor

Sep 5, 2025 5:29 PM in response to AZsport115

I’m not understanding why you need to open a PDF to save it as a PDF, and how do you think you can open Preview but not open Preview.

Can you explain what you’re starting with and why you then need to save it as a PDF when it’s already a PDF.

if you’re entering data into a form field, then save that as a PDF after you finish, editing it. There are built-in PDF tools in the command line that may solve your problem, but I’m not sure cause I don’t understand the point of what you’re doing

Sep 5, 2025 5:56 PM in response to Barney-15E

Hello! I am being sent a PDF by another party. The PDF is generally the same every time, but with certain values overlaid in fields on the PDF. For example, here is a portion of the PDF. Date: is always the same across each PDF, but the value in the field always changes. In the below example, the value in that field is 9/5/25. As you can see from the blue box when I hover my mouse over the date, macOS is treating this area with a field value as a layer or a field input or something other than the base PDF.


If I attempt to edit this PDF I get an error message that it's locked and I can't annotate it. If I duplicate the PDF I can't annotate the duplicated PDF either. The only way I can annotate this PDF is to open the PDF in Preview, open the Print Dialog, and then Save as PDF. The new PDF seems to "lock in" the form field data and preserve all the data in one layer. For example if I open that new PDF and hover my mouse over the date, it no longer has a blue box behind it like in the attached image. It's just one flat PDF.


I don't know how to get the PDF to this state without going through the process of opening the PDF in Preview, open the Print Dialog, and then Save as PDF. Since I have to this dozens of times per day, creating an automation or shortcut would be ideal.


NOTE: If I use a quartz filter or cups to try to create the PDF, it creates a PDF I can annotate, but all the field data such as the date 9/5/25 in this example is lost and becomes blank. Going through the aforementioned process in Preview is the only way I can figure out how to do it.

Oct 22, 2025 12:28 PM in response to Old Toad

I appreciate it, but I need to automate this without opening Preview since I have to flatten hundreds of PDFs. I have an action that opens Preview, then the print dialog bog, and saves as PDF, but quick actions based on accessibility functions (keystrokes, clicks, etc.) are not very stable--they can be affected by CPU temperature, number of apps open, etc. Also you have can't do anything else on the computer while an action runs otherwise you might break it mid-action. Thus the need for a solution without opening Preview.

Oct 22, 2025 12:29 PM in response to VikingOSX

I appreciate the heads up. I made a Quick Action, put a Run AppleScript inside it, ran it, and got this error message:


The action “Run AppleScript” encountered an error: “*** -[__NSDictionaryM setObject:forKey:]: object cannot be nil (key: <NSAppleEventDescriptor: 'obj '{ 'form':'usrp', 'want':'prop', 'seld':'utxt'("PDFDDocumentSaveTextFromOCROption"), 'from':null() }>)”

Oct 22, 2025 2:41 PM in response to VikingOSX

Your updated code worked great, thank you @VikingOSX! I am running the applescript within a quick action.


When I try to run the applescript within a shortcut however, I get this error message:


missing value doesn't understand the "writeToFile_withOptions_" message.


Any idea why that is? The quick action is very helpful. In a perfect world I've love to be able to do this via a shortcut too but keep running into this.

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.

How do I automate opening a PDF in Preview, opening Print, Save as PDF--without opening preview??!

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