You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://packagist.org/packages/spatie/pdf-to-image)
This package provides an easy to work with class to convert PDF's to images.
10
-
11
-
Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
18
-
19
-
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
8
+
This package provides an easy-to-work-with class to convert a PDF to one or more image.
20
9
21
10
## Requirements
22
11
23
-
You should have [Imagick](http://php.net/manual/en/imagick.setresolution.php) and [Ghostscript](http://www.ghostscript.com/) installed. See [issues regarding Ghostscript](#issues-regarding-ghostscript).
12
+
You should have [Imagick](http://php.net/manual/en/imagick.setresolution.php) and [Ghostscript](http://www.ghostscript.com/) installed.
13
+
See [issues regarding Ghostscript](#issues-regarding-ghostscript) and [Imagick Issues](#imagick-issues) for more information.
24
14
25
15
## Installation
26
16
27
-
The package can be installed via composer:
28
-
```bash
17
+
The package can be installed via composer and requires PHP 8.2+:
18
+
19
+
```bash
29
20
composer require spatie/pdf-to-image
30
21
```
31
22
23
+
> If you are using PHP < 8.2, use version 2.0 of this package.
24
+
32
25
## Usage
33
26
34
-
Converting a pdf to an image is easy.
27
+
Converting a PDF to an image is easy.
35
28
36
29
```php
37
-
$pdf = new Spatie\PdfToImage\Pdf($pathToPdf);
38
-
$pdf->saveImage($pathToWhereImageShouldBeStored);
30
+
$pdf = new \Spatie\PdfToImage\Pdf($pathToPdf);
31
+
$pdf->save($pathToWhereImageShouldBeStored);
39
32
```
40
33
41
-
If the path you pass to `saveImage` has the extensions `jpg`, `jpeg`, or `png` the image will be saved in that format.
42
-
Otherwise the output will be a jpg.
34
+
If 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`.
35
+
36
+
The `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.
43
37
44
38
## Other methods
45
39
46
-
You can get the total number of pages in the pdf:
40
+
Get the total number of pages in the pdf:
41
+
42
+
```php
43
+
/** @var int $numberOfPages */
44
+
$numberOfPages = $pdf->pageCount();
45
+
```
46
+
47
+
Check if a file type is a supported output format:
48
+
49
+
```php
50
+
/** @var bool $isSupported */
51
+
$isSupported = $pdf->isValidOutputFormat('jpg');
52
+
```
53
+
54
+
By default, only the first page of the PDF will be rendered. To render another page, call the `selectPage()` method:
55
+
47
56
```php
48
-
$pdf->getNumberOfPages(); //returns an int
57
+
$pdf->selectPage(2)
58
+
->save($pathToWhereImageShouldBeStored); //saves the second page
49
59
```
50
60
51
-
By default the first page of the pdf will be rendered. If you want to render another page you can do so:
61
+
Or, select multiple pages with the `selectPages()` method:
62
+
52
63
```php
53
-
$pdf->setPage(2)
54
-
->saveImage($pathToWhereImageShouldBeStored); //saves the second page
64
+
$pdf->selectPages(2, 4, 5)
65
+
->save($directoryToWhereImageShouldBeStored); //saves the 2nd, 4th and 5th pages
55
66
```
56
67
57
-
You can override the output format:
68
+
Change the output format:
69
+
58
70
```php
59
-
$pdf->setOutputFormat('png')
60
-
->saveImage($pathToWhereImageShouldBeStored); //the output wil be a png, no matter what
This will instruct PHP FPM to look for the `gs` binary in the right places.
92
158
159
+
## Imagick Issues
160
+
161
+
If 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`.
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
114
-
115
-
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
183
+
## Security Vulnerabilities
116
184
117
-
We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).
185
+
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
118
186
119
187
## Credits
120
188
121
-
-[Freek Van der Herten](https://github.com/spatie)
189
+
-[Freek Van der Herten](https://github.com/freekmurze)
0 commit comments