Convert PDF To Image In PHP

Author - Webner
25.05.2020
|
0 Comments
||

How to convert PDF to image file using PHP?

Before installing PDF to Images package, firstly you need to install two PHP extensions
Imagick and Ghostscript.

Step 1: Install Imagick

sudo apt-get install php-imagick

Step 2: Restart Apache

In the second step, you need to restart the apache, because without it you won’t able to use Imagick in your application.

sudo service apache2 restart

You can check/ verify the installation of Imagick

php -m | grep imagick

Step 3: Install Ghostscript

Before installing Ghostscript, update apt source.

apt-get update

After that install Ghostscript through the following command:

apt-get install ghostscript

You can also check the verify Ghostscript.

ghostscript -v

Step 4: Install-Package spatie/pdf-to-image

The package is installed through the following command:

composer require spatie/pdf-to-image

Code for the conversion of Pdf
The below code is applicable if the Pdf contains 1 page.

$pdf = new Spatie\PdfToImage\Pdf($pathToPdf);
$pdf->saveImage($pathToWhereImageShouldBeStore);

For Multiple pages Pdf.

$pdf = new Spatie\PdfToImage\Pdf($pathToPdf);
foreach (range(1, $pdf->getNumberOfPages()) as $pageNumber) {
$pdf->setPage($pageNumber);
$pdf->saveImage(“Image_name”.$pageNumber.”.png”);

}

$pdf->getNumberOfPages() is used to get Number Of pages.

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *