Mahaveer's avatar

Pdf convert to image .

Imagick not working when pdf convert to image giving error exceptions.

$imagick = new \Imagick($pdfPath);
$imagick->setResolution(300,300);
 $imagick->setImageFormat('jpg'); //Set format
 file_put_contents($newImagePath, $imagick);
ImagickException (415)
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=magick-oFQPj3LUdB8KYtAfJLlOVAUlj6bYnxvF%d" "-fmagick-WYK7RdvbmnaZd85vXgzVGG07o6GdgWlD" "-fmagick-feVTdEJzQaO75F1cGnCofbyC3lurnCq1"' (The system cannot find the file specified. ) @ error/delegate.c/ExternalDelegateCommand/516
0 likes
15 replies
Sinnbeck's avatar

What does this give you?

dd(file_exists($pdfPath));
Mahaveer's avatar
// This is image upload dir path after convert to image
$newImagePath = 'C:\xampp\htdocs\app/images/floorplan_images/2022/10/4573980-1664914949.jpg';
// This is pdf file dir path
$pdfPath = 'C:\xampp\htdocs\app/upload/floor_plan/3290-683626239.pdf';
bobbybouwmann's avatar
Level 88

You shouldn't be using file_put_contents here but instead `$imagick->writeImage('thumb.jpg');

// instantiate Imagick 
$imagick = new Imagick($pdfPath);

$imagick->setResolution(300, 300);
$imagick->setImageFormat('jpg');    
$imagick->writeImage('image.jpg'); 
$imagick->clear(); 
Sinnbeck's avatar

@Mahaveer so the file does not exist and the error says that the file does not exist :) make sure that the file actually e exists before proceeding

Sinnbeck's avatar

@Mahaveer so the dd gives true but exact same error with

The system cannot find the file specified

Mahaveer's avatar

@Sinnbeck But, When i upload image instead pdf. so it worked fine. How is possible?

bobbybouwmann's avatar

Also, note that if your PDF contains multiple pages you need to select which page you want to covert to an image

$imagick = new Imagick($pdfPath);

$pages = $im->getNumberImages(); 

for ($i = 0; $i < $pages; $i++) {
    // Create new instance for each page
    $imagick = new Imagick($pdfPath . '[' . $i . ']');
    $imagick->setResolution(300, 300);
    $imagick->setImageFormat('jpg');    
    $imagick->writeImage('image' . $i . '.jpg'); 
}
juanborras's avatar

Hey there! I have the same problem... somehow imagick is not able to execute gs from within laravel "runtime" if I use php artisan tinker, everything works fine. The error is this:

[2024-04-26 08:20:24] local.ERROR: ImagickException: 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-wylNd26c0oMLm8Z7eqDBMi3yqZNgAki0%d' '-f/var/tmp/magick-e5JddLznjSeKeu-nn-uJXQUOmqJJem7V' '-f/var/tmp/magick-G7lnpXKs6YNqwtn1f7zsFZjbQPkcB7rU'' (32512) @ error/ghostscript-private.h/ExecuteGhostscriptCommand/74 in /Users/.......ommitted....../vendor/spatie/pdf-to-image/src/Pdf.php:44

It happens when:

$pdf = new Pdf($pdfPath);

Where $pdfPath is a valid pdf path.

Any ideas?

Please or to participate in this conversation.