PHPackages                             tobento/service-pdf - 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. [PDF &amp; Document Generation](/categories/documents)
4. /
5. tobento/service-pdf

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

tobento/service-pdf
===================

A flexible and extensible PDF generation service built around clean interfaces and parameter objects.

2.0.3(3mo ago)072MITPHPPHP &gt;=8.4

Since Jan 6Pushed 3mo agoCompare

[ Source](https://github.com/tobento-ch/service-pdf)[ Packagist](https://packagist.org/packages/tobento/service-pdf)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-service-pdf/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (4)Dependencies (11)Versions (5)Used By (2)

Pdf Service
===========

[](#pdf-service)

The PDF service offers a set of interfaces for creating, streaming, and downloading PDF documents. It includes a default implementation powered by the [mPDF library](https://github.com/mpdf/mpdf).

Table of Contents
-----------------

[](#table-of-contents)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Highlights](#highlights)
- [Documentation](#documentation)
    - [Basic Usage](#basic-usage)
        - [Generating Pdf](#generating-pdf)
        - [Streaming Pdf](#streaming-pdf)
        - [Downloading Pdf](#downloading-pdf)
    - [Pdf](#pdf)
        - [Name](#name)
        - [Contents](#contents)
        - [Headers and Footers](#headers-and-footers)
        - [Page Setup](#page-setup)
        - [Page Break](#page-break)
        - [Paging](#paging)
        - [Document Info](#document-info)
        - [Compression](#compression)
        - [Security](#security)
    - [Custom Pdf](#custom-pdf)
    - [Pdf Generator](#pdf-generator)
        - [Mpdf Pdf Generator](#mpdf-pdf-generator)
        - [Null Pdf Generator](#null-pdf-generator)
    - [Pdf Generators](#pdf-generators)
        - [Default Pdf Generators](#default-pdf-generators)
        - [Lazy Pdf Generators](#lazy-pdf-generators)
    - [Mpdf](#mpdf)
        - [Pdf Generator - Mpdf](#pdf-generator---mpdf)
        - [Pdf Generator Factory - Mpdf](#pdf-generator-factory---mpdf)
    - [Learn More](#learn-more)
        - [Queueing PDF](#queueing-pdf)
        - [Prerendering Templates](#prerendering-templates)
- [Credits](#credits)

---

Getting started
===============

[](#getting-started)

Add the latest version of the Pdf service project running this command.

```
composer require tobento/service-pdf

```

Requirements
------------

[](#requirements)

- PHP 8.4 or above

Highlights
----------

[](#highlights)

- Framework-agnostic, will work with any project
- Decoupled design

Documentation
=============

[](#documentation)

Basic Usage
-----------

[](#basic-usage)

### Generating Pdf

[](#generating-pdf)

To generate a PDF, create a `Pdf` instance with your desired content and pass it to a PDF generator.
The generator processes the configuration and returns the final PDF as a binary string, ready to store, stream, or return in a response.

```
use Tobento\Service\Pdf\Pdf;
use Tobento\Service\Pdf\PdfGeneratorInterface;

class SomeService
{
    public function generate(PdfGeneratorInterface $pdfGenerator): void
    {
        $pdf = new Pdf()
            ->html('Lorem Ipsum');

        $binaryString = $pdfGenerator->generate(pdf: $pdf);
    }
}
```

Check out the [Pdf](#pdf) to learn more about building PDF content.

Check out the available [Pdf Generators](#pdf-generator) to explore the different generator implementations.

### Streaming Pdf

[](#streaming-pdf)

Stream the generated PDF directly as a PSR-7 `StreamInterface`.
This is useful when returning the PDF from a controller or middleware.

```
use Psr\Http\Message\StreamInterface;
use Tobento\Service\Pdf\Pdf;
use Tobento\Service\Pdf\PdfGeneratorInterface;

class SomeService
{
    public function stream(PdfGeneratorInterface $pdfGenerator): void
    {
        $pdf = new Pdf()
            ->html('Lorem Ipsum');

        $stream = $pdfGenerator->stream(pdf: $pdf);

        var_dump($stream instanceof StreamInterface);
        // bool(true)
    }
}
```

Check out the [Pdf](#pdf) to learn more about building PDF content.

Check out the available [Pdf Generators](#pdf-generator) to explore the different generator implementations.

### Downloading Pdf

[](#downloading-pdf)

Create a downloadable PDF response using the `PdfResponseFactoryInterface`.
The factory generates the PDF using the configured generator and returns a PSR-7 response with the correct download headers.

```
use Tobento\Service\Pdf\Pdf;
use Tobento\Service\Pdf\PdfResponseFactoryInterface;

class SomeService
{
    public function download(PdfResponseFactoryInterface $pdfResponseFactory): void
    {
        $pdf = new Pdf()
            ->html('Lorem Ipsum');

        // Create a PSR-7 download response:
        $response = $pdfResponseFactory->download(
            pdf: $pdf,
            filename: 'document.pdf',
        );

        // Return or emit the response depending on your framework.
    }
}
```

Check out the [Pdf](#pdf) to learn more about building PDF content.

Check out the available [Pdf Generators](#pdf-generator) to explore the different generator implementations.

Pdf
---

[](#pdf)

The `Pdf` object provides a fluent API for building PDF documents.
You can add content, configure layout, and then generate the final PDF using a [PDF Generator](#pdf-generator).

### Name

[](#name)

You may assign a logical name to a PDF.
This name identifies the PDF within your application domain (e.g. "invoice", "report", "contract") and is useful for event handling, conditional logic, routing, or analytics.

By default, if no name is set, the class name is returned:

```
use Tobento\Service\Pdf\Pdf;

$pdf = new Pdf();

echo $pdf->getName(); // Tobento\Service\Pdf\Pdf

$pdf->name('invoice');

echo $pdf->getName(); // invoice
```

### Contents

[](#contents)

You can add HTML, plain text, or templates to the document:

```
use Tobento\Service\Pdf\Pdf;

$pdf = new Pdf()
    // Add HTML content:
    ->html('Lorem Ipsum')

    // Add plain text:
    ->text('Lorem ipsum')

    // Add a template:
    ->template(name: 'shop/invoice', data: []);
```

#### Templates

[](#templates)

Use the `template` method to render a named template with the renderer provided by the PDF generator, allowing you to generate structured and reusable PDF content.

```
use Tobento\Service\Pdf\Pdf;

$pdf = new Pdf()->template(
    name: 'shop/invoice',
    data: [
        'title' => 'Title',
        'items' => $items,
    ]
);
```

**Example Template**

```
DOCTYPE html>
