PHP code example of spatie / pdf-to-image

1. Go to this page and download the library: Download spatie/pdf-to-image library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spatie / pdf-to-image example snippets


$pdf = new \Spatie\PdfToImage\Pdf($pathToPdf);
$pdf->save($pathToWhereImageShouldBeStored);

/** @var int $numberOfPages */
$numberOfPages = $pdf->pageCount();

/** @var bool $isSupported */
$isSupported = $pdf->isValidOutputFormat('jpg');

$pdf->selectPage(2)
    ->save($pathToWhereImageShouldBeStored); //saves the second page

$pdf->selectPages(2, 4, 5)
    ->save($directoryToWhereImageShouldBeStored); //saves the 2nd, 4th and 5th pages

$pdf->format(\Spatie\PdfToImage\Enums\OutputFormat::Webp)
    ->save($pathToWhereImageShouldBeStored); //the saved image will be in webp format

$pdf->quality(90) // set an output quality of 90%
    ->save($pathToWhereImageShouldBeStored);

$pdf->resolution(300) // resolution of 300 dpi
    ->save($pathToWhereImageShouldBeStored);

$pdf
   ->thumbnailSize(400) // set thumbnail width to 400px; height is calculated automatically
   ->save($pathToWhereImageShouldBeStored);

// or:
$pdf
   ->thumbnailSize(400, 300) // set thumbnail width to 400px and the height to 300px
   ->save($pathToWhereImageShouldBeStored);

$pdf->size(400) // set the width to 400px; height is calculated automatically
    ->save($pathToWhereImageShouldBeStored);

$pdf->size(400, 300) // set the width to 400px and the height to 300px
    ->save($pathToWhereImageShouldBeStored);

/** @var \Spatie\PdfToImage\DTOs\PageSize $size */
$size = $pdf->getSize();

$width = $size->width;
$height = $size->height;

$pdf->saveAllPages($directoryToWhereImagesShouldBeStored);

$pdf->layerMethod(\Spatie\PdfToImage\Enums\LayerMethod::Merge);

// or disable layer merging:
$pdf->layerMethod(\Spatie\PdfToImage\Enums\LayerMethod::None);

$pdf->backgroundColor('white') // simple text for 'white' color
    ->save($pathToWhereImageShouldBeStored);

$pdf->backgroundColor('#fff') // code for 'white' color
    ->save($pathToWhereImageShouldBeStored);

$pdf->backgroundColor('rgb(255,255,255)') // rgb for 'white' color
    ->save($pathToWhereImageShouldBeStored);