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

Add support for omitBackground #629

Merged
merged 1 commit into from
Apr 19, 2022
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
8 changes: 8 additions & 0 deletions docs/usage/creating-pdfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ Browsershot::html($someHtml)
->save('example.pdf');
```

If you want a transparent background color instead of the default white, you can call `transparentBackground`:

```php
Browsershot::html($someHtml)
->transparentBackground()
->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 @@ -29,6 +29,7 @@ class Browsershot
protected $screenshotQuality = null;
protected $temporaryHtmlDirectory;
protected $timeout = 60;
protected $transparentBackground = false;
protected $url = '';
protected $postParams = [];
protected $additionalOptions = [];
Expand Down Expand Up @@ -328,6 +329,13 @@ public function hideBackground()
return $this;
}

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

return $this;
}

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

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

if ($this->scale) {
$command['options']['scale'] = $this->scale;
}
Expand Down
2 changes: 2 additions & 0 deletions tests/BrowsershotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@
it('can create a command to generate a pdf', function () {
$command = Browsershot::url('https://example.com')
->showBackground()
->transparentBackground()
->landscape()
->margins(10, 20, 30, 40)
->pages('1-3')
Expand All @@ -265,6 +266,7 @@
'options' => [
'path' => 'screenshot.pdf',
'printBackground' => true,
'omitBackground' => true,
'landscape' => true,
'margin' => ['top' => '10mm', 'right' => '20mm', 'bottom' => '30mm', 'left' => '40mm'],
'pageRanges' => '1-3',
Expand Down