Skip to content

Commit dc551ae

Browse files
committed
♻️ use enums for endpoints and wrap it all up
1 parent dc6e37e commit dc551ae

8 files changed

Lines changed: 71 additions & 60 deletions

File tree

.github/workflows/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ jobs:
1010
matrix:
1111
php: [8.1, 8.2, 8.3]
1212
symfony_process: [4, 5, 6, 7]
13-
# exclude:
14-
# - php: 8.1
15-
# symfony_process: 7
13+
exclude:
14+
- php: 8.1
15+
symfony_process: 7
1616

1717
steps:
1818
- uses: actions/checkout@v4

README.md

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
_For pre-V1 documentation [click here](https://github.com/SynergiTech/chrome-pdf-php/blob/v0/README.md)_
55

6+
_For pre-V3 documentation [click here](https://github.com/SynergiTech/chrome-pdf-php/blob/v2/README.md)_
7+
68
This is a library for creating PDFs from HTML rendered with the SkPDF backend via Chrome. In order to do this, you can opt to use one of the supported drivers:
79
* [SynergiTech/chrome-pdf](https://github.com/SynergiTech/chrome-pdf)
8-
* [browserless](https://www.browserless.io/)
9-
10-
Currently supports PHP ^8
10+
* [Browserless](https://www.browserless.io/)
1111

1212
## Installation
1313
```
@@ -16,8 +16,8 @@ composer require synergitech/chrome-pdf-php
1616
### chrome-pdf
1717
If you are planning to use the [`chrome-pdf`](https://github.com/SynergiTech/chrome-pdf) driver to render PDFs locally, you should also make sure to install this from npm.
1818

19-
### browserless
20-
If you are planning to use the [browserless](https://www.browserless.io/) driver to render PDFs remotely or take screenshots, you should register for an API key. Remember that local assets cannot be rendered by browserless.
19+
### Browserless
20+
If you are planning to use the [Browserless](https://www.browserless.io/) driver to render PDFs remotely or take screenshots, you should register for an API key. Remember that local assets cannot be rendered by Browserless.
2121

2222
## Usage
2323
A common interface is provided via AbstractPDF. The options presented via this class will be available from all drivers.
@@ -26,17 +26,32 @@ You should instantiate one of the available drivers, potentially passing options
2626
```php
2727
use SynergiTech\ChromePDF\Chrome;
2828
use SynergiTech\ChromePDF\Browserless;
29-
use SynergiTech\ChromePDF\Browserless\Screenshot;
3029

3130
$pdf = new Chrome('path-to-chrome-pdf');
3231
$pdf->renderContent('<h1>test</h1>');
3332

3433
$pdf = new Browserless('your-api-key');
3534
$pdf->renderContent('<h1>test</h1>');
35+
```
36+
37+
### Advanced Browserless Usage
38+
39+
You can optionally use specific endpoints when you create a client.
40+
41+
```php
42+
use SynergiTech\ChromePDF\Browserless;
43+
44+
$pdf = new Browserless('your-api-key', Browserless\EndpointsEnum::London);
45+
```
46+
47+
As this library essentially functions as an API client for Browserless, we have also implemented the screenshot API.
48+
49+
```php
50+
use SynergiTech\ChromePDF\Browserless;
3651

3752
// For information on options, see https://www.browserless.io/docs/screenshot#custom-options.
3853
// `render()` defaults to using jpeg with a quality of 75 and fullPage set to false.
39-
$file = new Screenshot('your-api-key');
54+
$file = new Browserless\Screenshot('your-api-key');
4055
$file->render('https://example.com');
4156
$file->render('https://example.com', [
4257
'fullPage' => true,

src/Browserless.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public function getFormattedOptions(): array
182182
*/
183183
private function render(array $options)
184184
{
185-
return $this->request($this->pdfEndpoint, $options);
185+
return $this->request($this->pdfEndpoint, $options);
186186
}
187187

188188
/**

src/Browserless/Client.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,17 @@ trait Client
1717
private $apiKey;
1818

1919
/**
20-
* @var string
21-
*/
22-
private $apiUrl = 'https://chrome.browserless.io';
23-
24-
// public const EUROPE_REGION_URL = 'production-lon.browserless.io';
25-
26-
// public const US_REGION_URL = 'production-sfo.browserless.io';
27-
28-
/**
29-
* @param string $apiKey api key from browserless.io
3020
* @param \GuzzleHttp\Client $client custom Guzzle client
3121
*/
32-
public function __construct(string $apiKey = null, $client = null)
33-
{
22+
public function __construct(
23+
string $apiKey = null,
24+
EndpointsEnum|string $endpoint = EndpointsEnum::Default,
25+
$client = null
26+
) {
3427
if ($client === null) {
3528
// @codeCoverageIgnoreStart
3629
$client = new \GuzzleHttp\Client([
37-
'base_uri' => $this->apiUrl,
30+
'base_uri' => ($endpoint instanceof EndpointsEnum) ? $endpoint->value : $endpoint,
3831
]);
3932
// @codeCoverageIgnoreEnd
4033
}
@@ -90,9 +83,9 @@ protected function request(string $endpoint, array $json)
9083
}
9184
}
9285

93-
throw new APIException("Failed to render PDF: {$message}", $e->getCode(), $e);
86+
throw new APIException("Failed to render from Browserless: {$message}", $e->getCode(), $e);
9487
} catch (\Exception $e) {
95-
throw new APIException("Failed to render PDF: {$e->getMessage()}", $e->getCode(), $e);
88+
throw new APIException("Failed to render from Browserless: {$e->getMessage()}", $e->getCode(), $e);
9689
}
9790

9891
return StreamWrapper::getResource($response->getBody());

src/Browserless/EndpointsEnum.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace SynergiTech\ChromePDF\Browserless;
4+
5+
enum EndpointsEnum: string
6+
{
7+
case Default = 'https://chrome.browserless.io';
8+
case London = 'https://production-lon.browserless.io';
9+
case SanFrancisco = 'https://production-sfo.browserless.io';
10+
}

src/Browserless/Screenshot.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ class Screenshot
77
use Client;
88

99
/**
10-
* Defaults to the following options:
11-
* - quality: 75
12-
* - type: jpeg
13-
* - fullPage: true
14-
*
15-
* @param array<string, mixed> $options see https://www.browserless.io/docs/screenshot#custom-options
10+
* @param array<string,mixed> $options see https://www.browserless.io/docs/screenshot#custom-options
1611
*
1712
* @return resource
1813
*/
@@ -22,6 +17,7 @@ public function render(string $url, array $options = [])
2217
'type' => 'jpeg',
2318
'fullPage' => false,
2419
], $options);
20+
2521
if ($options['type'] === 'jpeg' && ! isset($options['quality'])) {
2622
$options['quality'] = 75;
2723
}
@@ -31,7 +27,7 @@ public function render(string $url, array $options = [])
3127
json: [
3228
'url' => $url,
3329
'options' => $options,
34-
]
30+
],
3531
);
3632
}
3733
}

test/Browserless/ScreenshotTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
namespace SynergiTech\ChromePDF\Test\Browserless;
44

55
use GuzzleHttp\Psr7\Response;
6-
7-
use SynergiTech\ChromePDF\Browserless;
86
use SynergiTech\ChromePDF\Browserless\Screenshot;
9-
use SynergiTech\ChromePDF\Chrome;
107
use SynergiTech\ChromePDF\Test\TestCase;
118

129
class ScreenshotTest extends TestCase
@@ -20,7 +17,7 @@ public function test_render()
2017
->with($this->anything(), $this->hasKeyValue(['json', 'url'], $this->identicalTo('test')))
2118
->willReturn(new Response(200, [], 'screenshot'));
2219

23-
$bl = new Screenshot('', $client);
20+
$bl = new Screenshot(client: $client);
2421
$stream = $bl->render('test');
2522

2623
$this->assertIsResource($stream);

test/BrowserlessTest.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function test_rotation()
2929
)
3030
->willReturn(new Response());
3131

32-
$bl = new Browserless('', $client);
32+
$bl = new Browserless(client: $client);
3333

3434
$this->assertNull($bl->getRotation());
3535

@@ -54,7 +54,7 @@ public function test_safeMode()
5454
)
5555
->willReturn(new Response());
5656

57-
$bl = new Browserless('', $client);
57+
$bl = new Browserless(client: $client);
5858

5959
$this->assertFalse($bl->getSafeMode());
6060

@@ -74,7 +74,7 @@ public function test_timeout()
7474
)
7575
->willReturn(new Response());
7676

77-
$bl = new Browserless('', $client);
77+
$bl = new Browserless(client: $client);
7878

7979
$this->assertNull($bl->getTimeout());
8080

@@ -117,7 +117,7 @@ public function test_displayHeaderFooter()
117117
)
118118
->willReturn(new Response());
119119

120-
$bl = new Browserless('', $client);
120+
$bl = new Browserless(client: $client);
121121
$bl->renderContent('test');
122122

123123
$bl->setDisplayHeaderFooter(true);
@@ -155,7 +155,7 @@ public function test_header()
155155
)
156156
->willReturn(new Response());
157157

158-
$bl = new Browserless('', $client);
158+
$bl = new Browserless(client: $client);
159159
$bl->renderContent('test');
160160

161161
$bl->setHeader('header-test');
@@ -180,7 +180,7 @@ public function test_footer()
180180
)
181181
->willReturn(new Response());
182182

183-
$bl = new Browserless('', $client);
183+
$bl = new Browserless(client: $client);
184184
$bl->renderContent('test');
185185

186186
$bl->setFooter('footer-test');
@@ -205,7 +205,7 @@ public function test_format()
205205
)
206206
->willReturn(new Response());
207207

208-
$bl = new Browserless('', $client);
208+
$bl = new Browserless(client: $client);
209209
$bl->renderContent('test');
210210

211211
$bl->setFormat('Letter');
@@ -234,7 +234,7 @@ public function test_width()
234234
)
235235
->willReturn(new Response());
236236

237-
$bl = new Browserless('', $client);
237+
$bl = new Browserless(client: $client);
238238
$bl->renderContent('test');
239239

240240
$bl->setWidth('100px');
@@ -266,7 +266,7 @@ public function test_height()
266266
)
267267
->willReturn(new Response());
268268

269-
$bl = new Browserless('', $client);
269+
$bl = new Browserless(client: $client);
270270
$bl->renderContent('test');
271271

272272
$bl->setHeight('100px');
@@ -298,7 +298,7 @@ public function test_landscape()
298298
)
299299
->willReturn(new Response());
300300

301-
$bl = new Browserless('', $client);
301+
$bl = new Browserless(client: $client);
302302
$bl->renderContent('test');
303303

304304
$bl->setLandscape(true);
@@ -374,7 +374,7 @@ public function test_margin()
374374
)
375375
->willReturn(new Response());
376376

377-
$bl = new Browserless('', $client);
377+
$bl = new Browserless(client: $client);
378378
$bl->renderContent('test');
379379

380380
$bl->setMargin('20px');
@@ -415,7 +415,7 @@ public function test_pageRanges()
415415
)
416416
->willReturn(new Response());
417417

418-
$bl = new Browserless('', $client);
418+
$bl = new Browserless(client: $client);
419419
$bl->renderContent('test');
420420

421421
$bl->setPageRanges('2,5-7');
@@ -451,7 +451,7 @@ public function test_preferCSSPageSize()
451451
)
452452
->willReturn(new Response());
453453

454-
$bl = new Browserless('', $client);
454+
$bl = new Browserless(client: $client);
455455
$bl->renderContent('test');
456456

457457
$bl->setPreferCSSPageSize(true);
@@ -486,7 +486,7 @@ public function test_printBackground()
486486
)
487487
->willReturn(new Response());
488488

489-
$bl = new Browserless('', $client);
489+
$bl = new Browserless(client: $client);
490490
$bl->renderContent('test');
491491

492492
$bl->setPrintBackground(true);
@@ -518,7 +518,7 @@ public function test_scale()
518518
)
519519
->willReturn(new Response());
520520

521-
$bl = new Browserless('', $client);
521+
$bl = new Browserless(client: $client);
522522
$bl->renderContent('test');
523523

524524
$bl->setScale(2);
@@ -550,7 +550,7 @@ public function test_waitUntil()
550550
)
551551
->willReturn(new Response());
552552

553-
$bl = new Browserless('', $client);
553+
$bl = new Browserless(client: $client);
554554
$bl->renderContent('test');
555555

556556
$bl->setWaitUntil('domcontentloaded');
@@ -582,7 +582,7 @@ public function test_mediaEmulation()
582582
)
583583
->willReturn(new Response());
584584

585-
$bl = new Browserless('', $client);
585+
$bl = new Browserless(client: $client);
586586
$bl->renderContent('test');
587587

588588
$bl->setMediaEmulation('screen');
@@ -601,7 +601,7 @@ public function test_renderContent()
601601
->with($this->anything(), $this->hasKeyValue(['json', 'html'], $this->identicalTo('test')))
602602
->willReturn(new Response(200, [], 'rendered-pdf'));
603603

604-
$bl = new Browserless('', $client);
604+
$bl = new Browserless(client: $client);
605605
$stream = $bl->renderContent('test');
606606

607607
$this->assertIsResource($stream);
@@ -617,7 +617,7 @@ public function test_renderURL()
617617
->with($this->anything(), $this->hasKeyValue(['json', 'url'], $this->identicalTo('https://bbc.co.uk')))
618618
->willReturn(new Response(200, [], 'rendered-pdf'));
619619

620-
$bl = new Browserless('', $client);
620+
$bl = new Browserless(client: $client);
621621
$stream = $bl->renderURL('https://bbc.co.uk');
622622

623623
$this->assertIsResource($stream);
@@ -637,7 +637,7 @@ public function test_renderFile()
637637
fwrite($tmpfile, 'test file');
638638
$path = stream_get_meta_data($tmpfile)['uri'];
639639

640-
$bl = new Browserless('', $client);
640+
$bl = new Browserless(client: $client);
641641
$stream = $bl->renderFile($path);
642642

643643
$this->assertIsResource($stream);
@@ -658,7 +658,7 @@ public function test_plainError()
658658
$handler = HandlerStack::create($mock);
659659
$client = new Client(['handler' => $handler]);
660660

661-
$bl = new Browserless('', $client);
661+
$bl = new Browserless(client: $client);
662662
try {
663663
$stream = $bl->renderContent('');
664664
} catch (APIException $e) {
@@ -681,7 +681,7 @@ public function test_jsonError()
681681
$handler = HandlerStack::create($mock);
682682
$client = new Client(['handler' => $handler]);
683683

684-
$bl = new Browserless('', $client);
684+
$bl = new Browserless(client: $client);
685685
try {
686686
$stream = $bl->renderContent('');
687687
} catch (APIException $e) {
@@ -705,7 +705,7 @@ public function test_miscError()
705705
$handler = HandlerStack::create($mock);
706706
$client = new Client(['handler' => $handler]);
707707

708-
$bl = new Browserless('', $client);
708+
$bl = new Browserless(client: $client);
709709
try {
710710
$stream = $bl->renderContent('');
711711
} catch (APIException $e) {

0 commit comments

Comments
 (0)