Skip to content
Advertisement

Spatie/pdf-to-image get number of images always returns 0

I’m using the spatie/pdf-to-image with laravel to create thumbnails of uploaded pdf files. What is weird is that the code was working at one point now it isn’t. $pdf->getNumberOfPages(); always returns 0 no matter how many pages the pdf contains.

This is the constructor in the Spatie Pdf class. As you can see it is just using imagick’s getNumberImages();

JavaScript

This is a simplified version of my controller:

JavaScript

The output of the dump is:

JavaScript

I know for a fact this was working at one point and I’m leaning towards imagick as the culprit. No errors and no exceptions. Is there a simple way to test this theory?

Advertisement

Answer

I know this is old, but I think I find the issue. I was running into the same problem, but it only happens when I was an instance of IlluminateHttpUploadedFile.

To solve it, I actually had to save the file to the local disk (not S3 or any other remote disk) and then manipulate the saved PDF. Worked prefectly!

Here’s a sample of my code (note: I loop manually due to business rules in the application.

JavaScript

Then, of course, go back through and clean up any files that didn’t need saving.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement