Skip to content

Building Extensions

TCPDF-Next's architecture is open by design. The three official extensions (Artisan, Laravel, Pro) use the same interfaces and hook points available to any third-party developer.

Interface Contracts

Your extension should implement one or more of these interfaces:

PdfDocumentInterface

The core document contract. Implement this if you're building an alternative document engine.

php
namespace Yeeefang\TcpdfNext\Contracts;

interface PdfDocumentInterface
{
    public function addPage(?PageSize $size = null, Orientation $orientation = Orientation::Portrait): static;
    public function setMargins(Margin $margin): static;
    public function setFont(string $family, string $style = '', float $size = 12.0): static;
    public function cell(float $width, float $height, string $text = '', ...): static;
    public function multiCell(float $width, float $height, string $text, ...): static;
    public function writeHtml(string $html): static;
    public function image(string $file, ...): static;
    public function output(?string $filename = null, OutputDestination $dest = OutputDestination::Inline): string;
    public function save(string $path): void;
}

SignerInterface

Implement this for custom signing backends (cloud HSMs, remote signing services, etc.).

php
namespace Yeeefang\TcpdfNext\Contracts;

interface SignerInterface
{
    public function sign(string $data): SignatureResult;
    public function timestamp(string $signatureValue): string;
    public function supportsLtv(): bool;
}

HsmSignerInterface

For hardware security module integration:

php
namespace Yeeefang\TcpdfNext\Contracts;

interface HsmSignerInterface
{
    public function sign(string $data, string $algorithm = 'sha256WithRSAEncryption'): string;
    public function getCertificateDer(): string;
    public function getCertificateChainDer(): array;
    public function getPublicKeyAlgorithm(): string;
}

FontManagerInterface

For custom font loading strategies:

php
namespace Yeeefang\TcpdfNext\Contracts;

interface FontManagerInterface
{
    public function registerFont(string $fontFile, string $alias = '', int $fontIndex = 0): FontInfo;
    public function getFont(string $family, string $style = ''): ?FontInfo;
    public function subset(FontInfo $font, string $text): string;
    public function getRegisteredFonts(): array;
    public function addFontDirectory(string $directory): void;
}

Extension Skeleton

Here's a minimal third-party extension:

composer.json

json
{
    "name": "your-vendor/tcpdf-next-watermark",
    "description": "Advanced watermark extension for TCPDF-Next",
    "type": "library",
    "require": {
        "php": "^8.5",
        "yeeefang/tcpdf-next": "^1.7"
    },
    "autoload": {
        "psr-4": {
            "YourVendor\\TcpdfNextWatermark\\": "src/"
        }
    }
}

Extension Class

php
namespace YourVendor\TcpdfNextWatermark;

use Yeeefang\TcpdfNext\Core\Document;
use Yeeefang\TcpdfNext\Graphics\Color;

final class WatermarkExtension
{
    public function apply(
        Document $document,
        string $text,
        float $angle = 45.0,
        float $opacity = 0.15,
    ): Document {
        return $document
            ->startTransform()
            ->setAlpha($opacity)
            ->rotate($angle, $document->getPageWidth() / 2, $document->getPageHeight() / 2)
            ->setFontSize(60)
            ->setTextColor(200, 200, 200)
            ->text(
                $document->getPageWidth() / 4,
                $document->getPageHeight() / 2,
                $text,
            )
            ->stopTransform();
    }
}

How Official Extensions Connect

Artisan → Core

The Chrome renderer is injected via setChromeRendererConfig() on the Document class. Core stores the renderer as ?object — no type dependency on Artisan.

Laravel → Core

The ServiceProvider creates a factory binding for PdfDocumentInterface that returns a configured Document instance. The Facade proxies static calls to the container-resolved instance.

Pro → Core

Pro classes like LtvManager and PdfAManager operate on BinaryBuffer and ObjectRegistry — the same internal APIs Core uses. The PadesOrchestrator accepts optional Pro-only parameters (CertificateChainValidator, OcspResponseVerifier).

Namespace Conventions

Follow the ecosystem pattern:

YourVendor\TcpdfNext{ExtensionName}\
├── YourMainClass.php
├── Config\
├── Exception\
└── ...

Use Yeeefang\TcpdfNext\Contracts\ interfaces, never Core\ internal classes, for your public API boundaries.

Released under the LGPL-3.0-or-later License.