Print to PDF from Chrome

I want to Print/Save to PDF in Google Chrome using macOS Sonoma - how can it be done?

In Google Chrome you save to a PDF by selecting File > Print and then set the destination to Save as PDF. Perhaps you already know this.

I looked at the Google Chrome dictionary and didn’t see any way to save or export to a PDF.

You could use GUI scripting, and the following is a rudimentary example that worked on my Sonoma computer. However, I think you’re better off just doing this manually as explained in the first paragraph above.

-- this script assumes that destination in the Chrome print dialog is set to "Save as PDF"

set theTime to (hours as text) & minutes & seconds of (current date)
set the clipboard to theTime

tell application "Google Chrome" to activate

tell application "System Events" to tell process "Google Chrome"
	set frontmost to true
	click menu item "Print…" of menu "File" of menu bar 1
	delay 0.5
	key code 36 -- enter key
	delay 0.5
	click menu item "Paste" of menu "Edit" of menu bar 1
	delay 0.5
	key code 36 -- enter key
end tell

Thanks!

There’s no way to enforce setting the Print dialog to “Save as PDF” using AppleScript ?

I suspect there is, but my gui-scripting skills are poor, and I don’t know how to do that. I’ve included a screencapture below just in case another forum member might have an idea.

It looks like you can do this via the command line, use this in shell script:

CHROME='/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'

"$CHROME" --headless --print-to-pdf="$HOME/Documents/filename.pdf" "http://example.com"

This worked for me in Terminal.

Taken from here:
How can I download a webpage to a PDF in Terminal? - Ask Different (stackexchange.com)

…and here’s the full command list if you want to specify additional settings:
https://peter.sh/experiments/chromium-command-line-switches/