Skip to content

The Factory ​

The WeasyPrintFactory is the core class for generating PDFs. It serves as the main entry point for all PDF generation operations.

Instantiating a Factory ​

Create a new instance by directly instantiating the WeasyPrintFactory class:

php
use WeasyPrint\WeasyPrintFactory;

$factory = new WeasyPrintFactory();

You can optionally pass configuration using the Config class for type-safety and clear option hints:

php
use WeasyPrint\Objects\Config;

$factory = new WeasyPrintFactory(
  new Config(
    timeout: 60,
    logOutput: false
  )
);

Alternatively, you can pass an array, which is useful when injecting configuration from a repository (like Laravel's config() helper):

php
$factory = new WeasyPrintFactory([
  'timeout' => 60,
  'log_output' => false,
]);

TIP

The Config class is recommended as it provides type-safety and IDE autocomplete for available options.

How to configure the factory

Using in a Class ​

Once you have a factory instance, you can inject it into your services and use it to prepare sources and generate PDFs:

php
<?php

namespace Services;

use WeasyPrint\WeasyPrintFactory;

class InvoiceService
{
  private WeasyPrintFactory $factory;

  public function __construct()
  {
    $this->factory = new WeasyPrintFactory();
  }

  public function generateDocument(string $documentId): void
  {
    // Use the factory for PDF generation
  }
}

Dependency Injection ​

For frameworks that support Dependency Injection or Inversion of Control (IoC) – where the framework automatically manages creating and passing objects to your classes – use the WeasyPrint\Contracts\WeasyPrint interface instead of the concrete class:

php
<?php

namespace Services;

use WeasyPrint\Contracts\WeasyPrint;

class InvoiceService
{
  public function __construct(
    private WeasyPrint $weasyprint
  ) {}

  public function generateDocument(string $documentId): void
  {
    // Use the injected instance for PDF generation
  }
}

This approach allows your code to remain decoupled from the concrete implementation and makes testing easier.

About Dependency Injection

Instead of creating objects yourself inside a class, you declare what you need in your constructor. The framework automatically provides these dependencies. This makes code easier to test and more flexible to change.

For Laravel ​

If you're using Laravel, the package automatically binds the WeasyPrintFactory to the WeasyPrint interface as a scoped singleton in your container. This means a single factory instance will be reused throughout a request lifecycle (even when using Laravel Octane).

Learn more about the Laravel integration

For Other Frameworks ​

For other frameworks that support a service container or dependency injection system, you'll need to manually bind the WeasyPrintFactory to the WeasyPrint interface. The exact syntax depends on your framework's container - here's a conceptual example:

php
// In your container setup/bootstrap file
$container->singleton(
  WeasyPrint::class,
  fn () => new WeasyPrintFactory()
);

Check your framework's documentation for how to register singleton or shared services.

Checking the WeasyPrint Version ​

You can check the installed WeasyPrint binary version using getWeasyPrintVersion():

php
$version = $factory->getWeasyPrintVersion();
// e.g. "68.0"

This can be useful for debugging or verifying your environment is set up correctly.

Released under the ISC License.