最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Converting PDF to image by SpatiePdfToImage ends with FailedToExecuteCommand 'gs' - Stack Overflow

programmeradmin3浏览0评论

I am trying to create thumbnail from PDF file by php and Spatie\PdfToImage library. My code is here:

 $pdf = new Pdf(pathToPdf);
 $pdf->setPage(1);
 $pdf->setResolution(10);
 $pdf->saveImage(pathWhenIwantToSaveImage);

It is nothing special. But the row where I am calling saveImage method is throwing excpetion with this message:

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=/data/web/virtuals/67726/virtual/tmp/magick-L4e_2y5Q_rBNgFg-6RZhkumzIU9HqfOi%d' '-f/data/web/virtuals/67726/virtual/tmp/magick-KqFqpWJXMJ8NZhb9PwidsJheIlGU3YSa' '-f/data/web/virtuals/67726/virtual/tmp/magick-j7E1hfdmMsgcd0owgFQGhhrgHefC0UX2'' (-1) @ error/delegate.c/ExternalDelegateCommand/519 [] []

I checkd the file paths, folder write/read permissions and everything looks ok.

Please I need help with where the problem might be.

I am trying to create thumbnail from PDF file by php and Spatie\PdfToImage library. My code is here:

 $pdf = new Pdf(pathToPdf);
 $pdf->setPage(1);
 $pdf->setResolution(10);
 $pdf->saveImage(pathWhenIwantToSaveImage);

It is nothing special. But the row where I am calling saveImage method is throwing excpetion with this message:

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=/data/web/virtuals/67726/virtual/tmp/magick-L4e_2y5Q_rBNgFg-6RZhkumzIU9HqfOi%d' '-f/data/web/virtuals/67726/virtual/tmp/magick-KqFqpWJXMJ8NZhb9PwidsJheIlGU3YSa' '-f/data/web/virtuals/67726/virtual/tmp/magick-j7E1hfdmMsgcd0owgFQGhhrgHefC0UX2'' (-1) @ error/delegate.c/ExternalDelegateCommand/519 [] []

I checkd the file paths, folder write/read permissions and everything looks ok.

Please I need help with where the problem might be.

Share Improve this question asked Jan 19 at 13:32 Jakub KulhánekJakub Kulhánek 134 bronze badges 4
  • What is the class of that exception? What is the version of the package you are using? On first glance Pdf seems not to be from the Spatie\PdfToImage library as it has no setPage(), no setResolution() and no saveImage() method, ref: github.com/spatie/pdf-to-image/blob/main/src/Pdf.php#L3 -- it is perhaps best to document the full classname of the exception first. – hakre Commented Jan 19 at 17:14
  • Hello, the version of Spatie\PdfToImage is 1.8 and the exception is: exceptionImagickException I will try to update PdfToImage library and I will check config of our webhosting. – Jakub Kulhánek Commented Jan 20 at 6:28
  • Was not asking for you to update it, but it is necessary you edit in more details to your question to get better feedback. Unless someone comes by and knows all the details but that is rare. (the error message is a good start, but also with an exception it's good to show the exception class to better locate it and as well the version of the dependencies is just good practice, no need to be shy about, we know not all setups have the latest versions - they can't) – hakre Commented Jan 20 at 9:17
  • We can close this issue. I used PDF.js instead of php library – Jakub Kulhánek Commented Jan 23 at 11:58
Add a comment  | 

1 Answer 1

Reset to default 1

You are using GhostScript so dispense with the Magic and work direct it is far simpler but you need GhostScript to be directly addressed.

Ghostscript commands may be platform and application specific . It is clear you are using very old commands driven by Magic which is not required.

For such a simple task, lets look backwards! WHY would you want "PNG Alpha" for "Thumbnails"? That would not be a good end point, as JPEG at such a crude scale would be OK or RGB256.png clearer and smaller than alpha.

Locate where your GhostScript is installed and trim all those commands down to:

gs -sDEVICE=png256 -r96 -g96x134 -dPDFFitPage -ooutput.png -dFirstPage=1 -dLastPage=1 -f "variable in.pdf"

For all pages remove the First and Last page directives, and add %d in the -ooutput. To alter scale multiply 96 as that is ideal for Web canvas, and alter the -g96x134 to the number of pixels you want.

Above command for a Stackoverflow page will look like:
At -r96 -g96x134 result = 1.97 KB (2,019 bytes)

For -r192 -g192x269 result = 5.94 KB (6,089 bytes)

If you want better quality at the cost of file size use -sDEVICE=png16m rather than 256 colours.

Result = 18.2 KB (18,688 bytes). When a page has a lot of text, there is a small gain in quality and slightly lower size 17.6 KB (18,079 bytes) adding -dTextAlphaBits=4.

发布评论

评论列表(0)

  1. 暂无评论