Error handling

Catch them all! attempts to render your document even when errors occur, such as incorrect use of CSS properties or unsupported features. These errors are caught and stored within the result object for further handling, e.g. logging.

Similar, the standard UriResolver also catches any exceptions, such as file not found or access denied, and stores them in the UriResolver instance.

However, some errors may still prevent the document from rendering, in which case a try-catch block can be utilized. The example provided demonstrates how to dump all errors as additional headers.

try {
    $html = <<<HTML
        <p>This is an simple example.</p>
    $resolveUri = \Typesetsh\UriResolver::all();
    $result = \Typesetsh\createPdf($html, $resolveUri);

    $data = $result->asString();
    header('Content-Type: application/pdf');
    header('Content-Length: ' . strlen($data));
    /* Merge PDF errors and resolver errors */
    foreach ([...$result->issues, ...$resolveUri->errors] as $issue) {
        header("X-PDF-Warning: ".$issue->getMessage());

    echo $data;

} catch (Exception $exception) {
    // Snap!
    echo "Error!";

Last updated