Custom drivers | laravel-pdf | Spatie

 SPATIE

  Laravel PDF
==============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-pdf](https://spatie.be/docs/laravel-pdf/v2)  Drivers  Custom drivers

 Version   v2   v1

 Other versions for crawler [v2](https://spatie.be/docs/laravel-pdf/v2) [v1](https://spatie.be/docs/laravel-pdf/v1)

- [ Introduction ](https://spatie.be/docs/laravel-pdf/v2/introduction)
- [ Support us ](https://spatie.be/docs/laravel-pdf/v2/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-pdf/v2/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-pdf/v2/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-pdf/v2/questions-issues)
- [ Alternatives ](https://spatie.be/docs/laravel-pdf/v2/alternatives)
- [ Changelog ](https://spatie.be/docs/laravel-pdf/v2/changelog)
- [ About us ](https://spatie.be/docs/laravel-pdf/v2/about-us)

Basic usage
-----------

- [ Creating PDFs ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/creating-pdfs)
- [ Responding with PDFs ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/responding-with-pdfs)
- [ Formatting PDFs ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/formatting-pdfs)
- [ Saving PDFs to disks ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/saving-pdfs-to-disks)
- [ Queued PDF generation ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/queued-pdf-generation)
- [ Testing PDFs ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/testing-pdfs)
- [ Setting defaults ](https://spatie.be/docs/laravel-pdf/v2/basic-usage/setting-defaults)

Drivers
-------

- [ Configuration ](https://spatie.be/docs/laravel-pdf/v2/drivers/configuration)
- [ Customizing Browsershot ](https://spatie.be/docs/laravel-pdf/v2/drivers/customizing-browsershot)
- [ Using the Cloudflare driver ](https://spatie.be/docs/laravel-pdf/v2/drivers/using-the-cloudflare-driver)
- [ Using the DOMPDF driver ](https://spatie.be/docs/laravel-pdf/v2/drivers/using-the-dompdf-driver)
- [ Generating PDFs on AWS Lambda ](https://spatie.be/docs/laravel-pdf/v2/drivers/generating-pdfs-on-aws-lambda)
- [ Using the Gotenberg driver ](https://spatie.be/docs/laravel-pdf/v2/drivers/using-the-gotenberg-driver)
- [ Using the WeasyPrint driver ](https://spatie.be/docs/laravel-pdf/v2/drivers/using-the-weasyprint-driver)
- [ Custom drivers ](https://spatie.be/docs/laravel-pdf/v2/drivers/custom-drivers)

Advanced usage
--------------

- [ Creating PDFs with multiple pages ](https://spatie.be/docs/laravel-pdf/v2/advanced-usage/creating-pdfs-with-multiple-pages)
- [ Using Tailwind ](https://spatie.be/docs/laravel-pdf/v2/advanced-usage/using-tailwind)
- [ Extending with Macros ](https://spatie.be/docs/laravel-pdf/v2/advanced-usage/extending-with-macros)

 Custom drivers
==============

###  On this page

1. [ Creating a driver ](#content-creating-a-driver)
2. [ Registering a driver ](#content-registering-a-driver)
3. [ Using the driver ](#content-using-the-driver)

You can create your own PDF generation driver by implementing the `PdfDriver` interface. This allows you to integrate any PDF generation service or library.

Creating a driver
-----------------------------------------------------------------------------------------------------------

A driver must implement the `Spatie\LaravelPdf\Drivers\PdfDriver` interface:

```
namespace App\Pdf\Drivers;

use Spatie\LaravelPdf\Drivers\PdfDriver;
use Spatie\LaravelPdf\PdfOptions;

class WeasyPrintDriver implements PdfDriver
{
    public function __construct(protected array $config = [])
    {
    }

    public function generatePdf(
        string $html,
        ?string $headerHtml,
        ?string $footerHtml,
        PdfOptions $options,
    ): string {
        // Generate and return the PDF content as a string
    }

    public function savePdf(
        string $html,
        ?string $headerHtml,
        ?string $footerHtml,
        PdfOptions $options,
        string $path,
    ): void {
        // Generate the PDF and save it to the given path
    }
}
```

The `PdfOptions` object provides access to the formatting options set on the builder:

- `$options->format` — paper format like `A4`, `Letter`, etc. (or `null`)
- `$options->paperSize` — array with `width`, `height`, and `unit` keys (or `null`)
- `$options->margins` — array with `top`, `right`, `bottom`, `left`, and `unit` keys (or `null`)
- `$options->orientation` — `landscape` or `portrait` (or `null`)

Registering a driver
--------------------------------------------------------------------------------------------------------------------

Register your driver as a singleton in a service provider:

```
namespace App\Providers;

use App\Pdf\Drivers\WeasyPrintDriver;
use Illuminate\Support\ServiceProvider;

class PdfServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->singleton('laravel-pdf.driver.weasyprint', function () {
            return new WeasyPrintDriver(config('laravel-pdf.weasyprint', []));
        });
    }
}
```

Using the driver
--------------------------------------------------------------------------------------------------------

Once registered, you can use the driver on a per-PDF basis:

```
use Spatie\LaravelPdf\Facades\Pdf;

Pdf::view('pdfs.invoice', ['invoice' => $invoice])
    ->driver('weasyprint')
    ->save('invoice.pdf');
```

To make it the default driver, bind it to the `PdfDriver` interface in your service provider:

```
use App\Pdf\Drivers\WeasyPrintDriver;
use Spatie\LaravelPdf\Drivers\PdfDriver;

$this->app->singleton(PdfDriver::class, function () {
    return new WeasyPrintDriver(config('laravel-pdf.weasyprint', []));
});
```
