spatie / pdf-to-image

Convert a pdf to an image
https://murze.be/2015/07/convert-a-pdf-to-an-image-using-php/
MIT License
1.32k stars 228 forks source link

FailedToExecuteCommand when trying to ping PDF file #217

Open LucaMino opened 1 year ago

LucaMino commented 1 year ago

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

afsakar commented 8 months ago

I have same problem right now, did you find anything?

abrahampo1 commented 7 months ago

Same problem here

patinthehat commented 7 months ago

@afsakar @abrahampo1 Could you please report your PHP version, imagick version, OS, OS version, etc.?

juanborras commented 7 months ago

Same problem after mac os update to 14.4.1. Working with Herd, php 8.2, last version of pdf-to-image, gs 10.02.1 (2023-11-01) and ImageMagick 7.1.1-28 Q16-HDRI aarch64 b20675898:20240211

This is the exception: FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r72x72' -dPrinted=false '-sOutputFile=/var/tmp/magick-kT81sdfsHvXhpNoMLLhQjbC02UaOxW8t%d' '-f/var/tmp/magick-vavcnQF1CypRfYIOhyhlO1ivZY5tkWmm' '-f/var/tmp/magick-wVzfxlma_rKL9XLdotUWEOK6dJl7OX4N'' (32512) @ error/ghostscript-private.h/ExecuteGhostscriptCommand/74

afsakar commented 6 months ago

@afsakar @abrahampo1 Could you please report your PHP version, imagick version, OS, OS version, etc.?

Versions: GS: 10.02.1 PHP: 8.3.4 OS: Somona 14.1.1

wallythewebdev commented 4 months ago

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

2) make sure you have the package installed from Org_Heigl i.e.

use Org_Heigl\Ghostscript\Ghostscript;

3) 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.

juanborras commented 4 months ago

Ok Thanks anyway

On 5 Jun 2024, at 11:00, wallythewebdev @.***> wrote:

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

run from your command line the below code - this is going to tell you where you GS is installed. which gs make sure you have the package installed from Org_Heigl i.e. use Org_Heigl\Ghostscript\Ghostscript; 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.

— Reply to this email directly, view it on GitHub https://github.com/spatie/pdf-to-image/issues/217#issuecomment-2149264289, or unsubscribe https://github.com/notifications/unsubscribe-auth/BAIP7M6TXPFDZ67GBJ7A7V3ZF3HUJAVCNFSM6AAAAAA6X5F6MOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBZGI3DIMRYHE. You are receiving this because you commented.

patinthehat commented 4 months ago

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

juanborras commented 4 months ago

Same problem. What about the others? have you the same problem?

[2024-06-12 18:08:13] local.ERROR: ImagickException: FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r144x144' -dPrinted=false -dFirstPage=1 -dLastPage=1 '-sOutputFile=/var/tmp/magick-_a7_GMtGXVqvqLSTv4hqeZrahYp5zTke%d' '-f/var/tmp/magick-CKa3m3v0HT5O_kLCfFz446budgSohG1G' '-f/var/tmp/magick-eF-ZuBXMWN5d__1l0yDpU2GR766lQYjK'' (32512) @ error/ghostscript-private.h/ExecuteGhostscriptCommand/75 in /Users/usuario/IngMomDev/libro/vendor/spatie/pdf-to-image/src/Pdf.php:235 Stack trace:

0 /Users/usuario/IngMomDev/libro/vendor/spatie/pdf-to-image/src/Pdf.php(235): Imagick->readImage('/Users/usuario/...')

1 /Users/usuario/IngMomDev/libro/vendor/spatie/pdf-to-image/src/Pdf.php(188): Spatie\PdfToImage\Pdf->getImageData('/Users/usuario/...', 1)

2 /Users/usuario/IngMomDev/libro/app/Services/PdfService.php(22): Spatie\PdfToImage\Pdf->save('/Users/usuario/...')

juanborras commented 4 months ago

I've executed this cmd directly in the directory I have the file.pdf

'gs' -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha' -dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r144x144' -dPrinted=false -dFirstPage=1 -dLastPage=1 '-sOutputFile=file.jpg' '-ffile.pdf'

and worked without problem.

Any ideas?

MaulikKachchi007 commented 4 months ago

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

In windows, You can replace the file name gswin64c.exe to gs.exe after changing the file name so it's working I have tried file name after convert that working.

cworreschk commented 4 months ago

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.

olivM commented 2 months ago

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

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