Skip to content

Commit b145cf4

Browse files
authored
Merge pull request #230 from spatie/v3
Major version: v3.0
2 parents 61ea9a6 + 7a68ada commit b145cf4

30 files changed

+756
-316
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
github: spatie
2+
custom: https://spatie.be/open-source/support-us

.github/workflows/dependabot-auto-merge.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313

1414
- name: Dependabot metadata
1515
id: metadata
16-
uses: dependabot/fetch-metadata@v2.1.0
16+
uses: dependabot/fetch-metadata@v2
1717
with:
1818
github-token: "${{ secrets.GITHUB_TOKEN }}"
1919
compat-lookup: true

.github/workflows/php-cs-fixer.yml renamed to .github/workflows/pint.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ jobs:
1212
with:
1313
ref: ${{ github.head_ref }}
1414

15-
- name: Run PHP CS Fixer
16-
uses: docker://oskarstark/php-cs-fixer-ga
17-
with:
18-
args: --config=.php-cs-fixer.dist.php --allow-risky=yes
15+
- name: Fix styling issues
16+
uses: aglipanci/laravel-pint-action@v2
1917

2018
- name: Commit changes
21-
uses: stefanzweifel/git-auto-commit-action@v4
19+
uses: stefanzweifel/git-auto-commit-action@v5
2220
with:
2321
commit_message: Fix styling

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
strategy:
99
fail-fast: true
1010
matrix:
11-
php: [8.3, 8.2, 8.1, 8.0, 7.4]
11+
php: [8.3, 8.2]
1212
dependency-version: [prefer-lowest, prefer-stable]
1313
os: [ubuntu-latest]
1414

.php-cs-fixer.dist.php

Lines changed: 0 additions & 43 deletions
This file was deleted.
File renamed without changes.

README.md

Lines changed: 118 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,141 @@
1-
# Convert a pdf to an image
1+
# Convert a PDF to an image
22

33
[![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)
4-
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](.github/LICENSE.md)
4+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
55
[![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)
6-
[![StyleCI](https://styleci.io/repos/38419604/shield?branch=master)](https://styleci.io/repos/38419604)
76
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/pdf-to-image.svg?style=flat-square)](https://packagist.org/packages/spatie/pdf-to-image)
87

9-
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).
12-
13-
## Support us
14-
15-
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/pdf-to-image.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/pdf-to-image)
16-
17-
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.
209

2110
## Requirements
2211

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.
2414

2515
## Installation
2616

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
2920
composer require spatie/pdf-to-image
3021
```
3122

23+
> If you are using PHP < 8.2, use version 2.0 of this package.
24+
3225
## Usage
3326

34-
Converting a pdf to an image is easy.
27+
Converting a PDF to an image is easy.
3528

3629
```php
37-
$pdf = new Spatie\PdfToImage\Pdf($pathToPdf);
38-
$pdf->saveImage($pathToWhereImageShouldBeStored);
30+
$pdf = new \Spatie\PdfToImage\Pdf($pathToPdf);
31+
$pdf->save($pathToWhereImageShouldBeStored);
3932
```
4033

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.
4337

4438
## Other methods
4539

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+
4756
```php
48-
$pdf->getNumberOfPages(); //returns an int
57+
$pdf->selectPage(2)
58+
->save($pathToWhereImageShouldBeStored); //saves the second page
4959
```
5060

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+
5263
```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
5566
```
5667

57-
You can override the output format:
68+
Change the output format:
69+
5870
```php
59-
$pdf->setOutputFormat('png')
60-
->saveImage($pathToWhereImageShouldBeStored); //the output wil be a png, no matter what
71+
$pdf->format(\Spatie\PdfToImage\Enums\OutputFormat::Webp)
72+
->save($pathToWhereImageShouldBeStored); //the saved image will be in webp format
6173
```
6274

63-
You can set the quality of compression from 0 to 100:
75+
Set the output quality _(the compression quality)_ from 0 to 100:
76+
6477
```php
65-
$pdf->setCompressionQuality(100); // sets the compression quality to maximum
78+
$pdf->quality(90) // set an output quality of 90%
79+
->save($pathToWhereImageShouldBeStored);
6680
```
6781

68-
You can specify the width of the resulting image:
82+
Set the output resolution DPI:
83+
84+
```php
85+
$pdf->resolution(300) // resolution of 300 dpi
86+
->save($pathToWhereImageShouldBeStored);
87+
```
88+
89+
Specify the thumbnail size of the output image:
90+
6991
```php
7092
$pdf
71-
->width(400)
72-
->saveImage($pathToWhereImageShouldBeStored);
93+
->thumbnailSize(400) // set thumbnail width to 400px; height is calculated automatically
94+
->save($pathToWhereImageShouldBeStored);
95+
96+
// or:
97+
$pdf
98+
->thumbnailSize(400, 300) // set thumbnail width to 400px and the height to 300px
99+
->save($pathToWhereImageShouldBeStored);
100+
```
101+
102+
Set the output image width:
103+
104+
```php
105+
$pdf->size(400) // set the width to 400px; height is calculated automatically
106+
->save($pathToWhereImageShouldBeStored);
107+
```
108+
109+
Set the output image width and height:
110+
111+
```php
112+
$pdf->size(400, 300) // set the width to 400px and the height to 300px
113+
->save($pathToWhereImageShouldBeStored);
114+
```
115+
116+
Get the dimensions of the PDF. This can be used to determine if the PDF is extremely high-resolution.
117+
118+
```php
119+
/** @var \Spatie\PdfToImage\DTOs\PageSize $size */
120+
$size = $pdf->getSize();
121+
122+
$width = $size->width;
123+
$height = $size->height;
124+
```
125+
126+
Save all pages to images:
127+
128+
```php
129+
$pdf->saveAllPages($directoryToWhereImagesShouldBeStored);
130+
```
131+
132+
Set the Merge Layer Method for Imagick:
133+
134+
```php
135+
$pdf->layerMethod(\Spatie\PdfToImage\Enums\LayerMethod::Merge);
136+
137+
// or disable layer merging:
138+
$pdf->layerMethod(\Spatie\PdfToImage\Enums\LayerMethod::None);
73139
```
74140

75141
## Issues regarding Ghostscript
@@ -90,37 +156,40 @@ env[PATH] = /usr/local/bin:/usr/bin:/bin
90156

91157
This will instruct PHP FPM to look for the `gs` binary in the right places.
92158

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`.
162+
163+
```xml
164+
<policy domain="coder" rights="read | write" pattern="PDF" />
165+
```
166+
93167
## Testing
94168

169+
`spatie/pdf-to-image` uses the PEST framework for unit tests. They can be run with the following command:
170+
95171
``` bash
96-
composer test
172+
./vendor/bin/pest
97173
```
98174

99175
## Changelog
100176

101-
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
177+
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
102178

103179
## Contributing
104180

105181
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
106182

107-
## Security
108-
109-
If you've found a bug regarding security please mail [[email protected]](mailto:[email protected]) instead of using the issue tracker.
110-
111-
## Postcardware
112-
113-
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
116184

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.
118186

119187
## Credits
120188

121-
- [Freek Van der Herten](https://github.com/spatie)
189+
- [Freek Van der Herten](https://github.com/freekmurze)
190+
- [Patrick Organ](https://github.com/patinthehat)
122191
- [All Contributors](../../contributors)
123192

124193
## License
125194

126-
The MIT License (MIT). Please see [License File](.github/LICENSE.md) for more information.
195+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,20 @@
1616
"email": "[email protected]",
1717
"homepage": "https://spatie.be",
1818
"role": "Developer"
19+
},
20+
{
21+
"name": "Patrick Organ",
22+
"email": "[email protected]",
23+
"homepage": "https://permafrost.dev",
24+
"role": "Developer"
1925
}
2026
],
2127
"require": {
22-
"php" : "^7.2|^8.0",
28+
"php" : "^8.2",
2329
"ext-imagick" : "*"
2430
},
2531
"require-dev": {
26-
"pestphp/pest": "^1.21"
32+
"pestphp/pest": "^2.34"
2733
},
2834
"autoload": {
2935
"psr-4": {

phpunit.xml.dist

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
3-
backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
verbose="true"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
10-
processIsolation="false"
11-
stopOnFailure="false">
12-
<testsuites>
13-
<testsuite name="League Test Suite">
14-
<directory>tests</directory>
15-
</testsuite>
16-
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory suffix=".php">src/</directory>
20-
</whitelist>
21-
</filter>
22-
<logging>
23-
<log type="tap" target="build/report.tap"/>
24-
<log type="junit" target="build/report.junit.xml"/>
25-
<log type="coverage-html" target="build/coverage"/>
26-
<log type="coverage-text" target="build/coverage.txt"/>
27-
<log type="coverage-clover" target="build/logs/clover.xml"/>
28-
</logging>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
3+
<coverage>
4+
<report>
5+
<clover outputFile="build/logs/clover.xml"/>
6+
<html outputDirectory="build/coverage"/>
7+
<text outputFile="build/coverage.txt"/>
8+
</report>
9+
</coverage>
10+
<testsuites>
11+
<testsuite name="League Test Suite">
12+
<directory>tests</directory>
13+
</testsuite>
14+
</testsuites>
15+
<logging>
16+
<junit outputFile="build/report.junit.xml"/>
17+
</logging>
18+
<source>
19+
<include>
20+
<directory suffix=".php">src/</directory>
21+
</include>
22+
</source>
2923
</phpunit>

0 commit comments

Comments
 (0)