diff --git a/docs/usage/creating-pdfs.md b/docs/usage/creating-pdfs.md index 534b6fdf..1070e73e 100644 --- a/docs/usage/creating-pdfs.md +++ b/docs/usage/creating-pdfs.md @@ -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. diff --git a/src/Browsershot.php b/src/Browsershot.php index 936a596e..d9d76e00 100644 --- a/src/Browsershot.php +++ b/src/Browsershot.php @@ -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; @@ -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; @@ -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; } diff --git a/tests/BrowsershotTest.php b/tests/BrowsershotTest.php index 6878ef45..d7429fc8 100644 --- a/tests/BrowsershotTest.php +++ b/tests/BrowsershotTest.php @@ -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(); @@ -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()