Skip to content

Laravel Integration

The package ships with a built-in Laravel integration that provides automatic service container binding, publishable configuration, a facade, and a class-based PDF generation API.

Setup

After installing the package, the service provider is auto-discovered by Laravel. No additional registration is needed.

Publishing Configuration

To customise the default configuration, publish the config file:

shell
php artisan vendor:publish --tag=weasyprint.config

This creates a config/weasyprint.php file in your application. All options can be set via environment variables - see the Configuration reference for available options.

Using the Factory

The service provider binds WeasyPrintFactory to the WeasyPrint contract as a scoped singleton, meaning a single instance is reused throughout a request lifecycle (including with Laravel Octane).

Dependency Injection

As covered in the Factory docs, inject the contract into your classes:

php
use WeasyPrint\Contracts\WeasyPrint;

class InvoiceController
{
  public function download(WeasyPrint $weasyprint)
  {
    return $weasyprint
      ->prepareSource(view('invoices.template', ['total' => 100.0]))
      ->build()
      ->download('invoice.pdf');
  }
}

Facade

The WeasyPrint facade provides static access to the factory:

php
use WeasyPrint\Integration\Laravel\WeasyPrint;

$output = WeasyPrint::prepareSource('<h1>Invoice</h1>')
  ->build();

return $output->download('invoice.pdf');

Class-based PDFs

For a more structured approach, the Laravel integration also provides an abstract PDF class that lets you encapsulate source, filename, configuration, and headers in a single, reusable class.

Learn about class-based PDFs

Released under the ISC License.