Enable WordPress PDF thumbnails for local installations

I’m using Laravel Herd for my local WordPress development, and yesterday I ran into an issue where PDF thumbnails were not being generated in the media library---despite Imagick being installed and active.

Trying to generate the thumbnails with WP-CLI surfaced the issue:

wp media regenerate 2326
Found 1 image to regenerate.
sh: gs: command not found
sh: gs: command not found
Warning: FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH
-dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2
'-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r128x128'
-dPrinted=false -dFirstPage=1 -dLastPage=1 '-sOutputFile=...' '-f...' '-f...'
(32512) @ error/ghostscript-private.h/ExecuteGhostscriptCommand/75 (ID 2326)

As it turns out, PHP couldn’t find the Ghostscript command, which is required for reading and interpreting PDF files.

The solution was to install Imagick and Ghostscript through Homebrew, and point my PHP configuration to those installations. First, install the required packages through Homebrew:

brew install imagemagick
brew install ghostscript

Then, add the following line to your PHP FPM configuration:

env[PATH] = /opt/homebrew/bin

You should now be able to generate thumbnails for your PDF files. Use this handy command to automatically regenerate thumbnails for all existing PDF files:

wp media regenerate $(wp post list --post_type=attachment --post_mime_type=application/pdf --format=ids)