Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a taggedPdf option #804

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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