PHPackages                             andrewdyer/pdf-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. andrewdyer/pdf-generator

ActiveLibrary[Templating &amp; Views](/categories/templating)

andrewdyer/pdf-generator
========================

A framework-agnostic library for generating PDFs from Twig templates, with support for a swappable driver interface

0.1.2(1mo ago)02MITPHPPHP ^8.3CI passing

Since May 28Pushed 1mo agoCompare

[ Source](https://github.com/andrewdyer/pdf-generator)[ Packagist](https://packagist.org/packages/andrewdyer/pdf-generator)[ Docs](https://github.com/andrewdyer/pdf-generator)[ RSS](/packages/andrewdyer-pdf-generator/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (3)Dependencies (10)Versions (5)Used By (0)

PDF Generator
=============

[](#pdf-generator)

A framework-agnostic library for generating PDFs from Twig templates, with support for a swappable driver interface.

[![Latest Stable Version](https://camo.githubusercontent.com/90593608eb5d22387d9a9eaa9fb30d7a966205003f2388d0931470e880f669e8/687474703a2f2f706f7365722e707567782e6f72672f616e64726577647965722f7064662d67656e657261746f722f763f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewdyer/pdf-generator)[![Total Downloads](https://camo.githubusercontent.com/466c052b7394e5a3acd3ab6dd9847c35c6a17ee0fa930b44dc1c111a2517065d/687474703a2f2f706f7365722e707567782e6f72672f616e64726577647965722f7064662d67656e657261746f722f646f776e6c6f6164733f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewdyer/pdf-generator)[![License](https://camo.githubusercontent.com/be79912ceb96862ff7c716da9f3211f8ce1eea3bd80cdf4e89c394f27bac6758/687474703a2f2f706f7365722e707567782e6f72672f616e64726577647965722f7064662d67656e657261746f722f6c6963656e73653f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewdyer/pdf-generator)[![PHP Version Require](https://camo.githubusercontent.com/d12402c451e269260b2cbec31b3f93f4d1e4a8d418c7fa916da1e7d997b22e20/687474703a2f2f706f7365722e707567782e6f72672f616e64726577647965722f7064662d67656e657261746f722f726571756972652f7068703f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewdyer/pdf-generator)

Introduction
------------

[](#introduction)

This library converts Twig templates into PDF documents through an extensible, swappable driver interface. It includes [Dompdf](https://dompdf.github.io/) and [Browsershot](https://spatie.be/docs/browsershot) driver implementations, with each driver enabled by installing its corresponding dependency. Custom drivers can be introduced by implementing a simple contract, making it straightforward to adapt the generation pipeline to different rendering backends or deployment environments.

Prerequisites
-------------

[](#prerequisites)

- **[PHP](https://www.php.net/)**: Version 8.3 or higher is required.
- **[Composer](https://getcomposer.org/)**: Dependency management tool for PHP.
- **[Twig](https://twig.symfony.com/)**: Version ^3.27 is required.

Installation
------------

[](#installation)

```
composer require andrewdyer/pdf-generator
```

Getting Started
---------------

[](#getting-started)

### 1. Create a Twig template

[](#1-create-a-twig-template)

Create an HTML template in the application's templates directory:

```
>

      body {
        font-family: Arial, sans-serif;
        font-size: 14px;
        color: #333;
        padding: 40px;
      }
      h1 {
        font-size: 28px;
        color: #1a1a2e;
      }

    Invoice #{{ order.id }}
    {{ order.customer.name }}

```

### 2. Create a PDF document

[](#2-create-a-pdf-document)

Extend `PdfDocument` and implement `options()` to configure the output and `content()` to define the template and data:

```
use AndrewDyer\PdfGenerator\PdfDocument;
use AndrewDyer\PdfGenerator\Values\Content;
use AndrewDyer\PdfGenerator\Values\Options;

class InvoicePdf extends PdfDocument
{
    public function __construct(private readonly Order $order) {}

    public function options(): Options
    {
        return new Options(
            filename: sprintf('invoice-%s.pdf', $this->order->id),
        );
    }

    public function content(): Content
    {
        return new Content(
            view: 'pdfs/invoice.html.twig',
            data: ['order' => $this->order],
        );
    }
}
```

### 3. Set up the PDF generator

[](#3-set-up-the-pdf-generator)

Instantiate `PdfGenerator` with a `Twig\Environment` and a driver:

```
use AndrewDyer\PdfGenerator\PdfGenerator;
use AndrewDyer\PdfGenerator\Drivers\DompdfDriver;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

$twig = new Environment(new FilesystemLoader('/path/to/templates'));

$generator = new PdfGenerator(
    twig:   $twig,
    driver: new DompdfDriver(),
);
```

Usage
-----

[](#usage)

### Saving to disk

[](#saving-to-disk)

Returns the full path of the written file:

```
$path = $generator->save(new InvoicePdf($order), '/storage/invoices');
```

### Streaming inline

[](#streaming-inline)

Streams the PDF directly in the browser for preview:

```
$generator->inline(new InvoicePdf($order));
```

### Forcing a download

[](#forcing-a-download)

Sends the PDF as a browser download:

```
$generator->download(new InvoicePdf($order));
```

### Retrieving raw bytes

[](#retrieving-raw-bytes)

Returns the raw PDF bytes for further processing, such as attaching to an email:

```
$bytes = $generator->generate(new InvoicePdf($order));
```

Drivers
-------

[](#drivers)

### Dompdf

[](#dompdf)

A pure PHP driver requiring no external services or binaries.

```
composer require dompdf/dompdf
```

Instantiate the driver directly, or pass a pre-configured [`Options`](https://github.com/dompdf/dompdf) instance:

```
use AndrewDyer\PdfGenerator\Drivers\DompdfDriver;
use Dompdf\Options;

$options = new Options();
$options->set('isRemoteEnabled', true);

$driver = new DompdfDriver($options);
```

### Browsershot

[](#browsershot)

A driver backed by headless Chromium via [Puppeteer](https://pptr.dev/), producing pixel-perfect output. Best suited for complex layouts, modern CSS, and web fonts.

Requires [Node.js](https://nodejs.org/) and Puppeteer:

```
npm install puppeteer
```

Then install the driver via Composer:

```
composer require spatie/browsershot
```

Pass optional binary paths if they are not on the system path:

```
use AndrewDyer\PdfGenerator\Drivers\BrowsershotDriver;

$driver = new BrowsershotDriver(
    nodeBinary:   '/usr/local/bin/node',
    npmBinary:    '/usr/local/bin/npm',
    chromiumPath: '/usr/bin/chromium',
);
```

### Custom drivers

[](#custom-drivers)

Any class implementing `DriverInterface` can be used as a driver, accepting rendered HTML and returning raw PDF bytes:

```
use AndrewDyer\PdfGenerator\Contracts\DriverInterface;
use AndrewDyer\PdfGenerator\Values\Options;

class CustomDriver implements DriverInterface
{
    public function generate(string $html, Options $options): string
    {
        // Convert $html to PDF bytes and return them...
    }
}
```

Options
-------

[](#options)

A value object passed to the driver alongside the rendered HTML. All properties have sensible defaults:

```
use AndrewDyer\PdfGenerator\Values\Options;
use AndrewDyer\PdfGenerator\Enums\Orientation;
use AndrewDyer\PdfGenerator\Enums\PaperSize;

new Options(
    filename:    'document.pdf',
    paperSize:   PaperSize::A4,
    orientation: Orientation::Portrait,
);
```

License
-------

[](#license)

Licensed under the [MIT licence](https://opensource.org/licenses/MIT) and is free for private or commercial projects.

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance92

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~9 days

Total

3

Last Release

38d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/666597ea6e46748a89fe8764d1a45b4d0da97daf1bb1e9770ea34ae41f706d08?d=identicon)[andrewdyer](/maintainers/andrewdyer)

---

Top Contributors

[![andrewdyer](https://avatars.githubusercontent.com/u/8114523?v=4)](https://github.com/andrewdyer "andrewdyer (10 commits)")

---

Tags

framework-agnostichtml-to-pdfpdfpdf-generatorphptwigphppdftwigPDF GENERATORframework agnostichtml-to-pdf

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/andrewdyer-pdf-generator/health.svg)

```
[![Health](https://phpackages.com/badges/andrewdyer-pdf-generator/health.svg)](https://phpackages.com/packages/andrewdyer-pdf-generator)
```

###  Alternatives

[symfony/ux-twig-component

Twig components for Symfony

22018.6M379](/packages/symfony-ux-twig-component)[symfony/ux-live-component

Live components for Symfony

1647.0M136](/packages/symfony-ux-live-component)[symfony/ux-toolkit

A tool to easily create a design system in your Symfony app with customizable, well-crafted Twig components

16126.1k1](/packages/symfony-ux-toolkit)[mati365/ckeditor5-symfony

CKEditor 5 integration for Symfony

262.6k](/packages/mati365-ckeditor5-symfony)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
