Skip to content

Commit

Permalink
Allow to pass format as a custom array (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
BilelJellouli authored Nov 14, 2024
1 parent abf450a commit 255dacd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,23 @@ $headlessBrowser = new \AirLST\HeadlessBrowserClient\AirlstHeadlessBrowser('api-

### Generate PDF from HTML

#### Using standard format
```php
$headlessBrowser->pdf(
'<p>html</p>', // html content
[10, 10, 10, 10] // margins
'A4', // page size
);
```

#### Using custom size
```php
$headlessBrowser->pdf(
'<p>html</p>', // 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
);
```

Expand Down
8 changes: 6 additions & 2 deletions src/AirlstHeadlessBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/HeadlessBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
21 changes: 19 additions & 2 deletions tests/AirlstHeadlessBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function testRequestsPdfContents(): void
return false;
}

return $request->getBody()->getContents() === '{"html":"<p>html<\/p>","format":"A3","margins":[5,5,5,5]}';
return $request->getBody()->getContents() === '{"html":"<p>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('<p>html</p>', 'A3', [5, 5, 5, 5]);
$pdf = (new AirlstHeadlessBrowser('api-key', $client))->pdf('<p>html</p>', [5, 5, 5, 5], 'A4');

$this->assertSame('http://example.com/pdf', $pdf->temporaryUrl());
}
Expand Down Expand Up @@ -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('<p>html</p>', width: 'customWidth', height: 'customHeight', margins: [5, 5, 5, 5]);

$this->assertSame('http://example.com/pdf', $pdf->temporaryUrl());
}

public function testImplementsHeadlessBrowser(): void
{
$this->assertTrue(
Expand Down

0 comments on commit 255dacd

Please sign in to comment.