{"id":13588945,"url":"https://github.com/spatie/pdf-to-image","last_synced_at":"2025-10-20T05:07:13.483Z","repository":{"id":34480658,"uuid":"38419604","full_name":"spatie/pdf-to-image","owner":"spatie","description":"Convert a pdf to an image","archived":false,"fork":false,"pushed_at":"2025-04-22T07:37:17.000Z","size":222,"stargazers_count":1373,"open_issues_count":6,"forks_count":230,"subscribers_count":19,"default_branch":"main","last_synced_at":"2025-05-10T10:47:26.035Z","etag":null,"topics":["convert","ghostscript","image","imagick","pdf","php"],"latest_commit_sha":null,"homepage":"https://murze.be/2015/07/convert-a-pdf-to-an-image-using-php/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":"spatie","custom":"https://spatie.be/open-source/support-us"}},"created_at":"2015-07-02T08:08:18.000Z","updated_at":"2025-05-08T18:47:25.000Z","dependencies_parsed_at":"2024-03-03T02:24:49.657Z","dependency_job_id":"8345c24b-cbea-451a-af8f-5b5bdcaeac5a","html_url":"https://github.com/spatie/pdf-to-image","commit_stats":{"total_commits":154,"total_committers":31,"mean_commits":4.967741935483871,"dds":0.3116883116883117,"last_synced_commit":"0fa3039df3236576b25e121fec0fbd30b8f8dd39"},"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fpdf-to-image","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fpdf-to-image/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fpdf-to-image/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fpdf-to-image/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/pdf-to-image/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253766117,"owners_count":21960847,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["convert","ghostscript","image","imagick","pdf","php"],"created_at":"2024-08-01T16:00:15.078Z","updated_at":"2025-10-20T05:07:13.461Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://github.com/sponsors/spatie","https://spatie.be/open-source/support-us"],"categories":["PHP"],"sub_categories":[],"readme":"\u003cdiv align=\"left\"\u003e\n    \u003ca href=\"https://spatie.be/open-source?utm_source=github\u0026utm_medium=banner\u0026utm_campaign=pdf-to-image\"\u003e\n      \u003cpicture\u003e\n        \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"https://spatie.be/packages/header/pdf-to-image/html/dark.webp\"\u003e\n        \u003cimg alt=\"Logo for pdf-to-image\" src=\"https://spatie.be/packages/header/pdf-to-image/html/light.webp\"\u003e\n      \u003c/picture\u003e\n    \u003c/a\u003e\n\n\u003ch1\u003eConvert a PDF to an image\u003c/h1\u003e\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/pdf-to-image.svg?style=flat-square)](https://packagist.org/packages/spatie/pdf-to-image)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n[![Quality Score](https://img.shields.io/scrutinizer/g/spatie/pdf-to-image.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/pdf-to-image)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/pdf-to-image.svg?style=flat-square)](https://packagist.org/packages/spatie/pdf-to-image)\n    \n\u003c/div\u003e\n\nThis package provides an easy-to-work-with class to convert a PDF to one or more image.\n\n## Requirements\n\nYou should have [Imagick](http://php.net/manual/en/imagick.setresolution.php) and [Ghostscript](http://www.ghostscript.com/) installed. \nSee [issues regarding Ghostscript](#issues-regarding-ghostscript) and [Imagick Issues](#imagick-issues) for more information.\n\n## Installation\n\nThe package can be installed via composer and requires PHP 8.2+:\n\n```bash\ncomposer require spatie/pdf-to-image\n```\n\n\u003e If you are using PHP \u003c 8.2, use version 2.0 of this package.\n\n## Usage\n\nConverting a PDF to an image is easy.\n\n```php\n$pdf = new \\Spatie\\PdfToImage\\Pdf($pathToPdf);\n$pdf-\u003esave($pathToWhereImageShouldBeStored);\n```\n\nIf the filename you pass to `saveImage` has the extensions `jpg`, `jpeg`, `png`, or `webp` the image will be saved in that format; otherwise the output format will be `jpg`.\n\nThe `save()` method returns an array with the filenames of the saved images if multiple images are saved, otherwise returns a string with the path to the saved image.\n\n## Other methods\n\nGet the total number of pages in the pdf:\n\n```php\n/** @var int $numberOfPages */\n$numberOfPages = $pdf-\u003epageCount();\n```\n\nCheck if a file type is a supported output format:\n\n```php\n/** @var bool $isSupported */\n$isSupported = $pdf-\u003eisValidOutputFormat('jpg');\n```\n\nBy default, only the first page of the PDF will be rendered. To render another page, call the `selectPage()` method:\n\n```php\n$pdf-\u003eselectPage(2)\n    -\u003esave($pathToWhereImageShouldBeStored); //saves the second page\n```\n\nOr, select multiple pages with the `selectPages()` method:\n\n```php\n$pdf-\u003eselectPages(2, 4, 5)\n    -\u003esave($directoryToWhereImageShouldBeStored); //saves the 2nd, 4th and 5th pages\n```\n\nChange the output format:\n\n```php\n$pdf-\u003eformat(\\Spatie\\PdfToImage\\Enums\\OutputFormat::Webp)\n    -\u003esave($pathToWhereImageShouldBeStored); //the saved image will be in webp format\n```\n\nSet the output quality _(the compression quality)_ from 0 to 100:\n\n```php\n$pdf-\u003equality(90) // set an output quality of 90%\n    -\u003esave($pathToWhereImageShouldBeStored);\n```\n\nSet the output resolution DPI:\n\n```php\n$pdf-\u003eresolution(300) // resolution of 300 dpi\n    -\u003esave($pathToWhereImageShouldBeStored);\n```\n\nSpecify the thumbnail size of the output image:\n\n```php\n$pdf\n   -\u003ethumbnailSize(400) // set thumbnail width to 400px; height is calculated automatically\n   -\u003esave($pathToWhereImageShouldBeStored);\n\n// or:\n$pdf\n   -\u003ethumbnailSize(400, 300) // set thumbnail width to 400px and the height to 300px\n   -\u003esave($pathToWhereImageShouldBeStored);\n```\n\nSet the output image width:\n\n```php\n$pdf-\u003esize(400) // set the width to 400px; height is calculated automatically\n    -\u003esave($pathToWhereImageShouldBeStored);\n```\n\nSet the output image width and height:\n\n```php\n$pdf-\u003esize(400, 300) // set the width to 400px and the height to 300px\n    -\u003esave($pathToWhereImageShouldBeStored);\n```\n\nGet the dimensions of the PDF. This can be used to determine if the PDF is extremely high-resolution.\n\n```php\n/** @var \\Spatie\\PdfToImage\\DTOs\\PageSize $size */\n$size = $pdf-\u003egetSize();\n\n$width = $size-\u003ewidth;\n$height = $size-\u003eheight;\n```\n\n\u003e [!NOTE]\n\u003e $directoryToWhereImagesShouldBeStored must be an existing directory\n\nSave all pages to images:\n\n```php\n$pdf-\u003esaveAllPages($directoryToWhereImagesShouldBeStored);\n```\n\nSet the Merge Layer Method for Imagick:\n\n```php\n$pdf-\u003elayerMethod(\\Spatie\\PdfToImage\\Enums\\LayerMethod::Merge);\n\n// or disable layer merging:\n$pdf-\u003elayerMethod(\\Spatie\\PdfToImage\\Enums\\LayerMethod::None);\n```\n\nSet the background color of the output image:\n\n```php\n$pdf-\u003ebackgroundColor('white') // simple text for 'white' color\n    -\u003esave($pathToWhereImageShouldBeStored);\n\n$pdf-\u003ebackgroundColor('#fff') // code for 'white' color\n    -\u003esave($pathToWhereImageShouldBeStored);\n\n$pdf-\u003ebackgroundColor('rgb(255,255,255)') // rgb for 'white' color\n    -\u003esave($pathToWhereImageShouldBeStored);\n```\n\n## Issues regarding Ghostscript\n\nThis package uses Ghostscript through Imagick. For this to work Ghostscripts `gs` command should be accessible from the PHP process. For the PHP CLI process (e.g. Laravel's asynchronous jobs, commands, etc...) this is usually already the case. \n\nHowever for PHP on FPM (e.g. when running this package \"in the browser\") you might run into the following problem:\n\n```\nUncaught ImagickException: FailedToExecuteCommand 'gs'\n```\n\nThis can be fixed by adding the following line at the end of your `php-fpm.conf` file and restarting PHP FPM. If you're unsure where the `php-fpm.conf` file is located you can check `phpinfo()`. If you are using Laravel Valet the `php-fpm.conf` file will be located in the `/usr/local/etc/php/YOUR-PHP-VERSION` directory.\n\n```\nenv[PATH] = /usr/local/bin:/usr/bin:/bin\n```\n\nThis will instruct PHP FPM to look for the `gs` binary in the right places.\n\n## Imagick Issues\n\nIf you receive an error with the message `attempt to perform an operation not allowed by the security policy 'PDF'`, you may need to add the following line to your `policy.xml` file. This file is usually located in `/etc/ImageMagick-[VERSION]/policy.xml`, such as `/etc/ImageMagick-7/policy.xml`.\n\n```xml\n\u003cpolicy domain=\"coder\" rights=\"read | write\" pattern=\"PDF\" /\u003e\n```\n\n## Testing\n\n`spatie/pdf-to-image` uses the PEST framework for unit tests. They can be run with the following command:\n\n``` bash\n./vendor/bin/pest\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Freek Van der Herten](https://github.com/freekmurze)\n- [Patrick Organ](https://github.com/patinthehat)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fpdf-to-image","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fpdf-to-image","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fpdf-to-image/lists"}