pdf-to-image: FailedToExecuteCommand when trying to ping PDF file

I’m getting this error when I try to instantiate a class from the package.

use Spatie\PdfToImage\Pdf;

$pdf = new Pdf($path);

$this->imagick->pingImage($pdfFile);   //line that return error

Error

FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r2.0x2.0' -dPrinted=false  '-sOutputFile=/var/tmp/magick-8gW2cA35obsZyaZngn8W8PSMR76gespA%d' '-f/var/tmp/magick-PFXyDbNvs6SLodhOoFaPBIuHTWmUvTzz' '-f/var/tmp/magick-4XhdiiUiXUbvuP5Azd8MBwT7H-FLuEVc'' (32512) @ error/ghostscript-private.h/ExecuteGhostscriptCommand/74

Versions php: 8.1 gs: 10.02.0 imagick: 7.1.1

Thank you

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 7
  • Comments: 15

Most upvoted comments

easiest way to solve this is to put this line in AppServerProvider::boot()

        putenv('PATH=' . getenv('PATH') . ':/opt/homebrew/bin');

I had the same problem in my local Laravel Herd environment. It turned out that the ENV variable PATH didn’t include my homebrew bin path /opt/homebrew/bin where I’ve installed Ghostscript. After I’ve added it, everything worked fine.

@juanborras If you are not able to extend the variable, you can use putenv() before instantiating the Pdf class.

easiest way to solve this is to put this line in AppServerProvider::boot()

    putenv('PATH=' . getenv('PATH') . ':/opt/homebrew/bin');

Thank you so much this worked

@LucaMino @abrahampo1 @afsakar I’ve released v3.0 of the package - can you try upgrading and see if your issue is resolved? Thanks! 👍

Took a while to work this one out - anyway for anyone who is using Herd and Mac this might help

  1. run from your command line the below code - this is going to tell you where you GS is installed.
which gs
  1. make sure you have the package installed from Org_Heigl i.e.
use Org_Heigl\Ghostscript\Ghostscript;
  1. in your code define the path to ghost script using the path that was returned form step 1
Ghostscript::setGsPath('/opt/homebrew/bin/gs');

this should as a whole look something like this in your controller / service:

<?php
    
    use App\Http\Controllers\Controller;
    use Illuminate\Support\Facades\Artisan;
    use Org_Heigl\Ghostscript\Ghostscript;
    use Spatie\PdfToImage\Pdf;
    use Imagick;
    
    class PdfToImageConverter extends Controller
    {
        public function pdfToImage($path_to_image = null, $output_path = null)
        {
            Ghostscript::setGsPath('/opt/homebrew/bin/gs');
            $pathToPdf = storage_path('pdf.pdf');
            $pathToWhereImageShouldBeStored = storage_path('pdfImageFile.png');
            $pdf = new Pdf($pathToPdf);
            $pdf->saveImage($pathToWhereImageShouldBeStored);
        }
    }

Hope this helps Quick note: not able to debug this further.