### Install Spatie PDF to Image Source: https://github.com/spatie/pdf-to-image/blob/main/README.md Installs the spatie/pdf-to-image package using Composer. This command is for PHP 8.2+. ```bash composer require spatie/pdf-to-image ``` -------------------------------- ### Get PDF Page Count Source: https://github.com/spatie/pdf-to-image/blob/main/README.md Retrieves the total number of pages in a PDF document. ```php $pdf = new \Spatie\PdfToImage\Pdf($pathToPdf); /** @var int $numberOfPages */ $numberOfPages = $pdf->pageCount(); ``` -------------------------------- ### Get PDF Page Dimensions Source: https://github.com/spatie/pdf-to-image/blob/main/README.md Retrieves the dimensions (width and height) of a PDF page, useful for checking high-resolution documents. ```php $pdf = new \Spatie\PdfToImage\Pdf($pathToPdf); /** @var \Spatie\PdfToImage\DTOs\PageSize $size */ $size = $pdf->getSize(); $width = $size->width; $height = $size->height; ``` -------------------------------- ### Run Unit Tests with PEST Source: https://github.com/spatie/pdf-to-image/blob/main/README.md This bash command executes the unit tests for the spatie/pdf-to-image package using the PEST testing framework. ```bash ./vendor/bin/pest ``` -------------------------------- ### Configure PATH for PHP FPM Source: https://github.com/spatie/pdf-to-image/blob/main/README.md This snippet shows how to add the 'gs' binary to the PATH environment variable for PHP FPM to resolve 'FailedToExecuteCommand 'gs'' errors. It involves modifying the php-fpm.conf file. ```php env[PATH] = /usr/local/bin:/usr/bin:/bin ``` -------------------------------- ### Set Imagick Layer Method and Background Color Source: https://github.com/spatie/pdf-to-image/blob/main/README.md Configures the layer merging method for Imagick and sets a background color for the output image. Supports text, hex codes, and RGB values for colors. ```php $pdf = new \Spatie\PdfToImage\Pdf($pathToPdf); // Enable layer merging $pdf->layerMethod(\'Spatie\PdfToImage\Enums\LayerMethod::Merge\'); // Disable layer merging $pdf->layerMethod(\'Spatie\PdfToImage\Enums\LayerMethod::None\'); // Set background color to white using text $pdf->backgroundColor('white') ->save($pathToWhereImageShouldBeStored); // Set background color to white using hex code $pdf->backgroundColor('#fff') ->save($pathToWhereImageShouldBeStored); // Set background color to white using RGB $pdf->backgroundColor('rgb(255,255,255)') ->save($pathToWhereImageShouldBeStored); ``` -------------------------------- ### Set Output Format and Quality Source: https://github.com/spatie/pdf-to-image/blob/main/README.md Allows specifying the output image format (e.g., Webp) and setting the compression quality (0-100). ```php $pdf = new \Spatie\PdfToImage\Pdf($pathToPdf); // Save in Webp format $pdf->format(\'Spatie\PdfToImage\Enums\OutputFormat::Webp\') ->save($pathToWhereImageShouldBeStored); // Set output quality to 90% $pdf->quality(90) ->save($pathToWhereImageShouldBeStored); ``` -------------------------------- ### Select and Save Specific PDF Pages Source: https://github.com/spatie/pdf-to-image/blob/main/README.md Allows selecting a single page or multiple specific pages from a PDF for conversion. The `selectPage()` method selects one page, while `selectPages()` can take multiple page numbers. ```php $pdf = new \Spatie\PdfToImage\Pdf($pathToPdf); // Saves the second page $pdf->selectPage(2) ->save($pathToWhereImageShouldBeStored); // Saves the 2nd, 4th, and 5th pages $pdf->selectPages(2, 4, 5) ->save($directoryToWhereImageShouldBeStored); ``` -------------------------------- ### Set Output Resolution and Thumbnail Size Source: https://github.com/spatie/pdf-to-image/blob/main/README.md Configures the resolution (DPI) of the output image and the desired thumbnail dimensions (width and height). ```php $pdf = new \Spatie\PdfToImage\Pdf($pathToPdf); // Set resolution to 300 dpi $pdf->resolution(300) ->save($pathToWhereImageShouldBeStored); // Set thumbnail width to 400px (height calculated automatically) $pdf->thumbnailSize(400) ->save($pathToWhereImageShouldBeStored); // Set thumbnail width to 400px and height to 300px $pdf->thumbnailSize(400, 300) ->save($pathToWhereImageShouldBeStored); ``` -------------------------------- ### Basic PDF to Image Conversion Source: https://github.com/spatie/pdf-to-image/blob/main/README.md Converts a PDF file to an image and saves it. The output format is determined by the file extension (jpg, jpeg, png, webp) or defaults to jpg. ```php $pdf = new \Spatie\PdfToImage\Pdf($pathToPdf); $pdf->save($pathToWhereImageShouldBeStored); ``` -------------------------------- ### Configure Imagick Security Policy for PDF Source: https://github.com/spatie/pdf-to-image/blob/main/README.md This XML snippet demonstrates how to update the ImageMagick policy to allow PDF operations, resolving 'attempt to perform an operation not allowed by the security policy 'PDF'' errors. It should be added to the policy.xml file. ```xml ``` -------------------------------- ### Check Supported Output Format Source: https://github.com/spatie/pdf-to-image/blob/main/README.md Verifies if a given file extension is a supported output format for image conversion. ```php $pdf = new \Spatie\PdfToImage\Pdf($pathToPdf); /** @var bool $isSupported */ $isSupported = $pdf->isValidOutputFormat('jpg'); ``` -------------------------------- ### Set Output Image Size Source: https://github.com/spatie/pdf-to-image/blob/main/README.md Specifies the desired width and height for the output image. If only width is provided, height is calculated proportionally. ```php $pdf = new \Spatie\PdfToImage\Pdf($pathToPdf); // Set width to 400px (height calculated automatically) $pdf->size(400) ->save($pathToWhereImageShouldBeStored); // Set width to 400px and height to 300px $pdf->size(400, 300) ->save($pathToWhereImageShouldBeStored); ``` -------------------------------- ### Save All PDF Pages to Images Source: https://github.com/spatie/pdf-to-image/blob/main/README.md Converts every page of the PDF document into separate image files and saves them to the specified directory. ```php $pdf = new \Spatie\PdfToImage\Pdf($pathToPdf); $pdf->saveAllPages($directoryToWhereImagesShouldBeStored); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.