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 GotenbergDriver implements PdfDriver
{
public function __construct(protected array $config = [])
{
}
public function generatePdf(
string $html,
?string $headerHtml,
?string $footerHtml,
PdfOptions $options,
): string {
}
public function savePdf(
string $html,
?string $headerHtml,
?string $footerHtml,
PdfOptions $options,
string $path,
): void {
}
}
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\GotenbergDriver;
use Illuminate\Support\ServiceProvider;
class PdfServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton('laravel-pdf.driver.gotenberg', function () {
return new GotenbergDriver(config('laravel-pdf.gotenberg', []));
});
}
}
##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('gotenberg')
->save('invoice.pdf');
To make it the default driver, bind it to the PdfDriver interface in your service provider:
use App\Pdf\Drivers\GotenbergDriver;
use Spatie\LaravelPdf\Drivers\PdfDriver;
$this->app->singleton(PdfDriver::class, function () {
return new GotenbergDriver(config('laravel-pdf.gotenberg', []));
});