Save handlers

Manipulate your PDF documents after they have been rendered.

Save handlers are powerful tools that allow you to make additional changes to a PDF document after the layout process is complete, but before it is saved. Save handlers are simple callable functions that can be easily implemented, they require the following signature:

callable(\Typesetsh\Pdf\Document $document): void

To use a save handler, it must be registered at Typesetsh\HtmlToPdf::$saveHandler with an arbitrary key, along with the save handler.

When the document is being saved, each registered handler will be called in the order in which they were registered, passing in the PDF document as a parameter. The following example demonstrates how to initialize an HTML to PDF service and register two save handlers.

$html = 'Hello <strong>World</string>!';

$service = new \Typesetsh\HtmlToPdf();
$service->saveHandler['pdf_a'] = new \Typesetsh\HtmlToPdf\A_1B_Web();
$service->saveHandler['producer'] = function(\Typesetsh\Pdf\Document $document): void {
    $document->Info->Producer = 'My awesome application';

$resolveUri = \Typesetsh\UriResolver::all();

$result = $service->render($html, $resolveUri);
$result->toFile(__DIR__.'/hello.pdf'); comes with a variety of predefined save handlers that offer additional functionality for manipulating your PDF documents. These save handlers are thoroughly described in their corresponding documentation pages, providing detailed explanations on how to use them and their specific capabilities.

Last updated