Skip to content

Instantly share code, notes, and snippets.

@LukeTowers
Created May 11, 2023 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LukeTowers/979776584d455d0eef16629138dda153 to your computer and use it in GitHub Desktop.
Save LukeTowers/979776584d455d0eef16629138dda153 to your computer and use it in GitHub Desktop.
Rasterize PDF as a Automator Quick Action

About

This is a Mac OS Automator Quick Action to rasterize selected PDFs should you ever be faced with having to print, sign, and scan a physical document.

Requirements:

  1. ImageMagick needs to be installed, you can install via brew install imagemagick in the terminal.
  2. \System\Library\CoreServices\Finder.app needs to be added under System Preferences -> Privacy & Security -> Full Disk Access.

Installation:

  1. Download RasterizePDF.sh and make sure it's executable by running chmod +x RasterizePDF.sh.
  2. Open the Automator app and create a new "Quick Action"
  3. Set "Workflow receives current" to "PDF files" and "in" to "Finder". Adjust the Image if desired.
  4. Search for "Run Shell Script" and drag to the right.
  5. Set the "Pass input" option to "as arguments".
  6. Paste the following, adjusting the path to the script as required: `"/Users/MyUser/Path/To/RasterizePDF.sh" "$@"
  7. Save your work and profit!
#!/bin/bash
export PATH="$PATH:/opt/homebrew/bin:/opt/homebrew/sbin"
for filepath in "$@"
do
dir=$(dirname "$filepath")
base=$(basename "$filepath" .pdf)
# Convert the PDF to individual PNGs
convert -define pdf:printed=true -density 150 "$filepath" "$dir/$base-%03d.png"
# Identify the individual page image files
images=()
for img in "$dir/${base}-"*.png; do
images+=("$img")
done
# Convert the images back into a PDF
convert "${images[@]}" "$dir/${base}-Scan.pdf"
# Remove the temporary files
rm "$dir/${base}-"*.png
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment