Skip to main content

Getting Spatie Media Library’s PDF thumbnail to work with Laravel

I’ve just spent a good couple of hours troubleshooting why Spatie Media Library won’t create a thumbnail of a PDF I upload to my server.

I’ve already setup up the correct code to generate a conversion, but nothing happens.

The real issue is that there are a couple of dependencies to make it work, which is Ghost Script and Imagick. Both of these need to be installed on your server for it to work.

Install Ghost Script
sudo apt-get install ghostscript
Once installed, check it works: gs –version

Install PHP Imagick
sudo apt-get install php-imagick
Once installed, check it works: php -m | grep imagick

However, even when everything is set up properly, I was receiving a server error:
“not authorized @ error/constitute.c/ReadImage/412”

To fix this, you need to SSH in and edit the policy.xml of Imagick located into etc/ImageMagick-6/policy.xml

Look for this line:

 <policy domain="coder" rights="none" pattern="PDF" /> 

and replace with:

 <policy domain="coder" rights="read|write" pattern="PDF" /> 

You also may want to add:

 <policy domain="coder" rights="read|write" pattern="LABEL" /> 

You then need to restart your server, in my caseĀ sudo service nginx restart

Then, restart your PHP FPM Workers.

Only then, should your PDF thumbnails work.

Unfortunately, I couldn’t get it working on Laragon locally.

Leave a Reply