PHP code example of bnbwebexpertise / pdf-to-image

1. Go to this page and download the library: Download bnbwebexpertise/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/ */

    

bnbwebexpertise / pdf-to-image example snippets


$pdf = new Bnb\PdfToImage\Pdf($pathToPdf);
$pdf->saveImage($pathToWhereImageShouldBeStored);

$pdf->getNumberOfPages(); //returns an int

$pdf->setPage(2)
    ->saveImage($pathToWhereImageShouldBeStored); //saves the second page

$pdf->setOutputFormat('png')
    ->saveImage($pathToWhereImageShouldBeStored); //the output wil be a png, no matter what

$beforeImageReadSettings = function (\Imagick $imagick, $page) {
    $imagick->setColorspace(\Imagick::COLORSPACE_SRGB);

    return $imagick;
};

$beforeImageWriteSettings = function (\Imagick $imagick, $page) {
    $imagick->resizeImage(1024, 1024, \Imagick::FILTER_LANCZOS, 0, true);
    $imagick->setImageColorspace(\Imagick::COLORSPACE_GRAY);

    return $imagick;
};

$pdf = new Pdf($this->testFile, $beforeImageReadSettings, $beforeImageWriteSettings)
    ->saveImage($pathToWhereImageShouldBeStored); //the output will be resized to a grayscale image with a best-fit dimension of 1024x1024