简体   繁体   中英

Imagick multiple pages PDF to JPG fatal error

What does not work:

  • Converting filename-multiple-pages.pdf[0] PDF file to JPG
  • Converting filename-multiple-pages.pdf PDF file to JPG

Fatal error: Uncaught exception 'ImagickException' with message 'Postscript delegate failed `/path/to/filename-multiple-pages.pdf': No such file or directory @ error/pdf.c/ReadPDFImage/664' in ...

When I try this with a sollution found on the webs with fopen and then using readImageFile of the fopen handle:

Fatal error: Uncaught exception 'ImagickException' with message 'Postscript delegate failed `/tmp/magick-rGGsdy9f': No such file or directory @ error/pdf.c/ReadPDFImage/664'

What does work:

  • Converting filename-multiple-pages.pdf[1] PDF file to JPG (the second page)
  • Converting filename-single-page.pdf PDF to JPG [/list]

The used PHP codes:

<?php

  // this does work for a single page file
  // it does NOT work for multiple page file
  // it does NOT work when using pdffile.pdf[0]
  // it DOES work when using pdffile.pdf[1]

  $filename = '/path/to/pdffile.pdf';  
  $im = new Imagick();
  $im->readImage($filename);
  $im = $im->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
  $im->scaleImage(150, 150, true);
  $im->writeImage('/path/to/image/pdffile.jpg');
?>


<?php
  // i used alternative code which gave me the second /tmp/ dir error (see above)

  $filename = '/path/to/pdffile.pdf';    
  $pdf_handle = fopen($filename, 'rb');
  $doc_preview = new Imagick();
  $doc_preview->setResolution(150,150);
  $doc_preview->readImageFile($pdf_handle);
  $doc_preview->setIteratorIndex(0);
  $doc_preview->setImageFormat('jpeg');
  $doc_preview->writeImage('/path/to/image/pdffile.jpg');
  $doc_preview->clear();
  $doc_preview->destroy();

?>

Installed modules by hosting provider

  • ImageMagick v6.7.2.7-5
  • Ghostscript 8.70

Does anyone have any idea what to do?

After some research I found out that the hosting provider didn't install recent versions. After a few days of testing and debugging they managed to install the latest versions and it all works now. The code in my post is good and can be used by others :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM