0

I have a sample code below to read a PDF document and convert to PNG. But i got the error below.

Fatal error: Uncaught ImagickException: PDFDelegateFailed `The system cannot find the file specified. ' @ error/pdf.c/ReadPDFImage/794 in C:\laragon\www\test\pdf2png.php:3 Stack trace: #0 C:\laragon\www\test\pdf2png.php(3): Imagick->__construct('C:/laragon/www/...') #1 {main} thrown in C:\laragon\www\test\pdf2png.php on line 3

<?php

$imagick = new Imagick('C:/laragon/www/test/ticket.pdf[0]');
$imagick->setImageFormat( 'png' );
file_put_contents('ticket.png', $imagick);

?>
Dave
  • 5,108
  • 16
  • 30
  • 40
hivolih247
  • 81
  • 1
  • 9
  • Seems pretty straightforward. The path and file you passed don't exist. That `.pdf[0]` in the file extension is a bit suspect. – Dave Mar 25 '20 at 18:53
  • I suspect those daft Windows paths are confusing ImageMagick, specifically the `C:/` stuff. Just to test, try copying the PDF to the directory where the script is executing. – Mark Setchell Mar 25 '20 at 19:45
  • @Dave .pdf[0] is the first page of a multipage pdf – Bonzo Mar 25 '20 at 20:20
  • But is it an actual file or are you trying to tell Imagick to extract the first page? – Dave Mar 25 '20 at 20:22
  • @MarkSetchell Yes, the ticket.pdf is actually in the directory where the script is executing, C:/laragon/www/test/ticket.pdf – hivolih247 Mar 25 '20 at 21:08
  • Ok, so try referring to it without the path as `"ticket.pdf[0]"` and, failing that, as `"ticket.pdf"` to see if you can at least get something working. – Mark Setchell Mar 25 '20 at 21:13
  • @MarkSetchell Tried both and also 'C:/laragon/www/test/ticket.pdf' and all the forward/backward slash with double/single combinations and even without "C:\" and all failed – hivolih247 Mar 25 '20 at 21:40
  • @MarkSetchell Could this be the issue -> https://github.com/Imagick/imagick/issues/96 – hivolih247 Mar 25 '20 at 21:46
  • What do you mean *”even without the C:\\”? That was specifically what I was suggesting! Ok, try printing the current working directory to a file to see if it’s running from the directory you think. – Mark Setchell Mar 25 '20 at 21:47
  • It's entirely possible. I don't use Windows and I don't know for sure at all, hence why I have only tried making hopefully helpful suggestions as comments rather than formally answering with anything I have tested. – Mark Setchell Mar 25 '20 at 22:15
  • Does it work for any PDF file or just that one? If no PDF files work, then... Do you have Ghostscript installed? ImageMagick needs to use Ghostscript to process PDF files. Modify your delegates.xml file to give it the full path to Ghostscript where is says "gs". PHP does not use the same PATH arguments and perhaps cannot find Ghostscript. Modify your policy.xml file to allow ImageMagick to use PDF files. See https://stackoverflow.com/questions/52861946/imagemagick-not-authorized-to-convert-pdf-to-an-image/52863413#52863413 – fmw42 Mar 25 '20 at 22:55
  • @fmw42 No, I don't have Ghostscript. That might be the prob. – hivolih247 Mar 26 '20 at 15:06
  • @MarkSetchell do you have Ghostscript installed? – hivolih247 Mar 26 '20 at 17:48

1 Answers1

1

AS @Mark Setchell says work on the file in the same folder to see if it works.

Also try this:

//$imagick = new Imagick("C:/laragon/www/test/ticket.pdf[0]");
$imagick = new Imagick("ticket.pdf[0]");
$imagick->setImageFormat( 'png' );
$imagick->writeImage('ticket.png');
Bonzo
  • 5,169
  • 1
  • 19
  • 27
  • I tried and got the error "Fatal error: Uncaught ImagickException: UnableToOpenBlob 'ticket.pdf': No such file or directory @ error/blob.c/OpenBlob/3315 in C:\laragon\www\test\pdf2png.php:3 Stack trace: #0 C:\laragon\www\test\pdf2png.php(3): Imagick->__construct('ticket.pdf[0]') #1 {main} thrown in C:\laragon\www\test\pdf2png.php on line 3" – hivolih247 Mar 25 '20 at 21:02
  • 1
    I have just tested my code and it works for me. Is line 3 writeImage? – Bonzo Mar 25 '20 at 21:52
  • I just corrected my code to resemble your code and I still get the same error. Where do you place the ticket.pdf? Under C:\laragon\www\test? – hivolih247 Mar 26 '20 at 07:01
  • ticket.pdf and pdf2png.php are in the same folder. – Bonzo Mar 26 '20 at 08:14
  • Yeah, same here. Hmmm...that's weird. – hivolih247 Mar 26 '20 at 12:08
  • Can you convert a jpg to a png and that will narrow down the problem as you may not have Ghostscript installed? – Bonzo Mar 26 '20 at 12:43
  • I just copied a ticket.jpg file to the folder of pdf2png.php and change my code to take in ticket.jpg and I still get the error "Fatal error: Uncaught ImagickException: UnableToOpenBlob 'ticket.jpg': No such file or directory " – hivolih247 Mar 26 '20 at 17:51