Converting a pdf to an image is easy using PHP, but the API kinda sucks.

$imagick = new Imagick('file.pdf[0]');
file_put_contents($pathToImage, $imagick);

The pdf-to-image-package aims to fix that. Here is the equivalent code:

$pdf = new Spatie\PdfToImage\Pdf('file.pdf');

If the path you pass to saveImage has an extension jpg, jpeg, or png the image will be saved in that format. Otherwise the output will be a jpg.

To convert all pages of the pdf to images you can run:

$pdf = new Spatie\PdfToImage\Pdf('file.pdf');
foreach (range(1, $pdf->getNumberOfPages()) as $pageNumber) {