diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b632f5..38cb91f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `headless-browser-client-php` will be documented in this file +## 3.0.0 - 2024-11-14 + - PDF format can be set to null + - Add width and height properties for PDF + ## 2.0.0 - 2024-07-25 - Drop support for PHP 8.2 diff --git a/README.md b/README.md index 3a24319..2fa03b7 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,23 @@ $headlessBrowser = new \AirLST\HeadlessBrowserClient\AirlstHeadlessBrowser('api- ### Generate PDF from HTML +#### Using standard format ```php $headlessBrowser->pdf( '
html
', // html content + [10, 10, 10, 10] // margins 'A4', // page size +); +``` + +#### Using custom size +```php +$headlessBrowser->pdf( + 'html
', // html content [10, 10, 10, 10] // margins + null, // Page size must be null otherwise it will override the custom width and height + '80mm', // width + '60mm', // height ); ``` diff --git a/src/AirlstHeadlessBrowser.php b/src/AirlstHeadlessBrowser.php index 6be15f5..519293d 100644 --- a/src/AirlstHeadlessBrowser.php +++ b/src/AirlstHeadlessBrowser.php @@ -22,13 +22,17 @@ public function __construct( #[Override] public function pdf( string $html, - string $format = 'A4', - array $margins = [10, 10, 10, 10] + array $margins = [10, 10, 10, 10], + ?string $format = null, + ?string $width = null, + ?string $height = null, ): Response { $request = $this->prepareRequest('/pdf', [ 'html' => $html, 'format' => $format, 'margins' => $margins, + 'width' => $width, + 'height' => $height, ]); $response = $this->client->sendRequest($request); diff --git a/src/HeadlessBrowser.php b/src/HeadlessBrowser.php index 92b7f2e..cd6b188 100644 --- a/src/HeadlessBrowser.php +++ b/src/HeadlessBrowser.php @@ -6,7 +6,7 @@ interface HeadlessBrowser { - public function pdf(string $html, string $format = 'A4', array $margins = [10, 10, 10, 10]): Response; + public function pdf(string $html, array $margins = [10, 10, 10, 10], ?string $format = null, ?string $width = null, ?string $height = null): Response; public function jpeg(string $html, int $quality = 75): Response; } diff --git a/tests/AirlstHeadlessBrowserTest.php b/tests/AirlstHeadlessBrowserTest.php index 6f5f700..bb5cd4f 100644 --- a/tests/AirlstHeadlessBrowserTest.php +++ b/tests/AirlstHeadlessBrowserTest.php @@ -43,11 +43,11 @@ public function testRequestsPdfContents(): void return false; } - return $request->getBody()->getContents() === '{"html":"html<\/p>","format":"A3","margins":[5,5,5,5]}'; + return $request->getBody()->getContents() === '{"html":"
html<\/p>","format":"A4","margins":[5,5,5,5],"width":null,"height":null}'; }) ->andReturn(new Response(200, [], json_encode(['temporary_url' => 'http://example.com/pdf']))); - $pdf = (new AirlstHeadlessBrowser('api-key', $client))->pdf('
html
', 'A3', [5, 5, 5, 5]); + $pdf = (new AirlstHeadlessBrowser('api-key', $client))->pdf('html
', [5, 5, 5, 5], 'A4'); $this->assertSame('http://example.com/pdf', $pdf->temporaryUrl()); } @@ -87,6 +87,23 @@ public function testRequestsJpegContents(): void $this->assertSame('http://example.com/jpeg', $jpeg->temporaryUrl()); } + public function testAcceptsCustomSize(): void + { + $client = Mockery::mock(ClientInterface::class); + $client->shouldReceive('sendRequest') + ->once() + ->withArgs(function (RequestInterface $request): bool { + $inputs = json_decode($request->getBody()->getContents(), true); + + return $inputs['width'] === 'customWidth' && $inputs['height'] === 'customHeight'; + }) + ->andReturn(new Response(200, [], json_encode(['temporary_url' => 'http://example.com/pdf']))); + + $pdf = (new AirlstHeadlessBrowser('api-key', $client))->pdf('html
', width: 'customWidth', height: 'customHeight', margins: [5, 5, 5, 5]); + + $this->assertSame('http://example.com/pdf', $pdf->temporaryUrl()); + } + public function testImplementsHeadlessBrowser(): void { $this->assertTrue(