Skip to content

Commit

Permalink
Adding a taggedPdf option
Browse files Browse the repository at this point in the history
  • Loading branch information
ntaylor-86 authored and freekmurze committed Dec 21, 2023
1 parent ddb46ac commit dc2fab7
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/usage/creating-pdfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@ Browsershot::html($someHtml)
->save('example.pdf');
```

## Tagged (accessible) pdf

Call `taggedPdf` if you want the resulting pdf to be tagged (accessible).

```php
Browsershot::html($someHtml)
->taggedPdf()
->save('example.pdf');
```

## Landscape orientation

Call `landscape` if you want to resulting pdf to be landscape oriented.
Expand Down
12 changes: 12 additions & 0 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class Browsershot
protected $scale = null;
protected $screenshotType = 'png';
protected $screenshotQuality = null;
protected $taggedPdf = false;
protected $temporaryHtmlDirectory;
protected $timeout = 60;
protected $transparentBackground = false;
Expand Down Expand Up @@ -378,6 +379,13 @@ public function transparentBackground()
return $this;
}

public function taggedPdf()
{
$this->taggedPdf = true;

return $this;
}

public function setScreenshotType(string $type, int $quality = null)
{
$this->screenshotType = $type;
Expand Down Expand Up @@ -833,6 +841,10 @@ public function createPdfCommand($targetPath = null): array
$command['options']['omitBackground'] = true;
}

if ($this->taggedPdf) {
$command['options']['tagged'] = true;
}

if ($this->scale) {
$command['options']['scale'] = $this->scale;
}
Expand Down
46 changes: 46 additions & 0 deletions tests/BrowsershotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,19 @@
expect(mime_content_type($targetPath))->toEqual('application/pdf');
});

it('can save a pdf with the taggedPdf option', function () {
$targetPath = __DIR__.'/temp/customPdf.pdf';

Browsershot::url('https://example.com')
->taggedPdf()
->pages('1')
->savePdf($targetPath);

expect($targetPath)->toBeFile();

expect(mime_content_type($targetPath))->toEqual('application/pdf');
});

it('can return a pdf as base 64', function () {
$base64 = Browsershot::url('https://example.com')
->base64pdf();
Expand Down Expand Up @@ -323,6 +336,39 @@
], $command);
});

it('can create a command to generate a pdf with tags', function() {
$command = Browsershot::url('https://example.com')
->showBackground()
->transparentBackground()
->taggedPdf()
->landscape()
->margins(10, 20, 30, 40)
->pages('1-3')
->paperSize(210, 148)
->createPdfCommand('screenshot.pdf');

$this->assertEquals([
'url' => 'https://example.com',
'action' => 'pdf',
'options' => [
'path' => 'screenshot.pdf',
'printBackground' => true,
'omitBackground' => true,
'tagged' => true,
'landscape' => true,
'margin' => ['top' => '10mm', 'right' => '20mm', 'bottom' => '30mm', 'left' => '40mm'],
'pageRanges' => '1-3',
'width' => '210mm',
'height' => '148mm',
'viewport' => [
'width' => 800,
'height' => 600,
],
'args' => [],
],
], $command);
});

it('can create a command to generate a pdf with a custom header', function () {
$command = Browsershot::url('https://example.com')
->showBackground()
Expand Down

0 comments on commit dc2fab7

Please sign in to comment.