Skip to content

Commit

Permalink
Merge pull request #12 from tjodalv/configure-driver
Browse files Browse the repository at this point in the history
Added ability to configure Browsershot driver instance
  • Loading branch information
pxlrbt authored Feb 13, 2024
2 parents 7b62382 + b9c85c5 commit deaea0b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ Currently two drivers are supported:

### Browsershot Driver

This is the driver and requires [spatie/browsershot](https://github.com/spatie/browsershot). Please follow the installation instructions for that package.
This is the default driver and requires [spatie/browsershot](https://github.com/spatie/browsershot). Please follow the installation instructions for that package.

You can configure the Browsershot driver via `BrowsershotDriver::configureUsing()` in your `AppServiceProvider`:

```php
BrowsershotDriver::configureUsing(
fn (Browsershot $browser) => $browser->setCustomTempPath(storage_path('tmp'))
);
```

### Wkhtmltopdf Driver

Expand Down
20 changes: 16 additions & 4 deletions src/Drivers/BrowsershotDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,33 @@

namespace pxlrbt\LaravelPdfable\Drivers;

use Closure;
use pxlrbt\LaravelPdfable\Pdfable;
use Spatie\Browsershot\Browsershot;

class BrowsershotDriver implements Driver
{
protected static ?Closure $configureUsing = null;

public static function configureUsing(Closure $callback): void {
static::$configureUsing = $callback;
}

public function getData(Pdfable $pdf): ?string
{
$html = $pdf->getView()->render();
$page = $pdf->page();

$browser = Browsershot::html($html)
->paperSize($page->getWidth(), $page->getHeight())
->margins(...$page->getMargins());

if (self::$configureUsing !== null) {
$browser = call_user_func(static::$configureUsing, $browser);
}

return base64_decode(
Browsershot::html($html)
->paperSize($page->getWidth(), $page->getHeight())
->margins(...$page->getMargins())
->base64pdf()
$browser->base64pdf()
);
}
}

0 comments on commit deaea0b

Please sign in to comment.