Reputation: 1256
I am using Spatie\PdfToImage in my Symfony application to change PDFs into images. Here is the function I am using:
public function savePdfPreviewImage($fullFilePath, $thumbnailPath)
{
$pdf = new Pdf($fullFilePath);
$pdf->saveImage($thumbnailPath);
return $this;
}
When given a path to a PDF, the library returns this message:
An image could not be created from the given input
How can I go about finding a solution to this?
So far I have tried verifying with an ls
that the file exists in the place where the app thinks it is. I have also tried opening the file -- which has a .pdf
file extension -- in a PDF reader to verify that it is not corrupt. Neither of those two actions yielded any clues.
=====
Edit 1: I traced this message back to the Imagine.php
file, where I removed an error-suppression line. That gave me this slightly less opaque message:
Warning: imagecreatefromstring(): Data is not in a recognized format
====
Edit 2: I have also verified that ghostscript is installed. The gs
command is available from my server environment. I have also verified that the path provided for $thumbnailPath
is a valid path/filename ending in .jpg.
Upvotes: 1
Views: 612
Reputation: 1256
I figured out what the problem was.
The conversion to PDF was actually behaving just fine. What was misbehaving was a later call within the application to the Imagine.php library, unsuccessfully resizing the image that my code successfully created. Here is the code that allowed me to see this:
public function savePdfPreviewImage($fullFilePath, $thumbnailPath)
{
//$pdf = new Pdf($fullFilePath);
//$pdf->saveImage($thumbnailPath); //This gives us "An image could not be created from the given input" and "Data is not in a recognized format"
//Let's try it with a manual call to GhostScript instead ...
exec(
'gs -o ' . //This creates the image successfully, but the error still shows up.
$thumbnailPath . //That means the error isn't coming from here, since we're no longer calling any external PHP libraries.
' -sDEVICE=jpeg ' .
$fullFilePath
);
return $this;
}
Upvotes: 1
Reputation: 1029
You do not need a lib for this, imagick alone will do fine.
$pdf = new \Imagick();
$pdf->setColorspace(\Imagick::COLORSPACE_SRGB);
$pdf->readImage($srcPath);
$pdf->setIteratorIndex($pageNo);
$pdf->writeImage($outPath);
(Imagick will recognize the file extension from $outPath)
Upvotes: 0