Skip to Content
NexPDF

Error handling

The exceptions NexPDF for PHP throws, the engine's error messages, license error codes, and patterns for handling failures and untrusted uploads.

English

Every NexPDF call either returns its result or throws an exception. There are no status codes to check by hand: the SDK reads the engine's per-thread error after each call and throws it as a NexPdfException.

Exception types

ExceptionThrown when
Nexdevit\NexPdf\NexPdfExceptionAn engine call failed. getMessage() is the engine's message. Subclass of RuntimeException.
NexPdfException with getCode() < 0activateLicense rejected the key. The code is the negative status (see below); it is 0 for every other failure.
NexPdfExceptionDocument::open could not read the file (cannot read <path>) or save could not write it (cannot write <path>).
LogicExceptionA method was called on a closed document (document is closed), or a document was cloned; use copy() instead.
FFI\ExceptionThe native library could not be loaded, or FFI is restricted by the ffi.enable setting. See Troubleshooting.
ErrorClass "FFI" not found: the FFI extension is not installed.
use Nexdevit\NexPdf\Document;
use Nexdevit\NexPdf\NexPdfException;

try {
    $doc = Document::open('upload.pdf');
    $doc->addWatermark('CONFIDENTIAL');
    $doc->save('out.pdf');
} catch (NexPdfException $e) {
    if ($e->getCode() < 0) {
        error_log(sprintf('license rejected (%d): %s', $e->getCode(), $e->getMessage()));
    } else {
        error_log('NexPDF could not process upload.pdf: ' . $e->getMessage());
    }
} finally {
    if (isset($doc)) {
        $doc->close();
    }
}

pageWidth, pageHeight, trimMemory and memoryUsageBytes do not throw NexPdfException. For a page that does not exist, pageWidth and pageHeight return the A4 size (595 x 842 points), so check the index against pageCount() first.

Common engine messages

MessageCauseWhat to do
password requiredThe file is encrypted and no password was givenAsk for the password and pass it to Document::open
incorrect passwordThe password matches neither the user nor the owner passwordAsk again
no license activated: '<module>' requires a NexPDF license keyThe call needs a module and no key is activeCall activateLicense in your bootstrap; see Running without a key
the active license does not include the '<module>' moduleYour plan does not include the moduleCheck NexPdf::isLicensed($module), or upgrade the plan
internal error: ...The engine caught an unexpected failure on this inputThe process keeps running; report the file to support
try {
    $doc = Document::open('locked.pdf', $password);
} catch (NexPdfException $e) {
    if (in_array($e->getMessage(), ['password required', 'incorrect password'], true)) {
        askForThePasswordAgain();
    } else {
        throw $e;
    }
}

Other messages describe the input: a malformed or truncated PDF, a page index out of range, invalid JSON options. They are meant to be logged as they are.

License error codes

NexPdfException::getCode() after activateLicense is one of:

CodeMeaning
-1Malformed key
-2Bad signature (the key was changed, or it is not a NexPDF key)
-3Unsupported key version (update the SDK)
-4Expired
-5Not yet valid (check the system clock)
-6Wrong platform
-7Wrong application id

A rejected key leaves the previous license state as it was, so a process that had no key stays in view-only mode.

The last error

NexPdf::lastError() returns the engine's last error message for the current thread, or null. You rarely need it, because the SDK throws with that message already, but it is useful when you call NexPdf::invoke directly, which reports failures inside its JSON result ({"ok":false,"error":"..."}) instead of throwing.

Untrusted input

The engine is written in Rust and fuzzed; a malformed PDF produces a NexPdfException, not a crash of the PHP worker, and engine panics are caught and reported as internal error: .... When you process uploads:

  • catch NexPdfException per file, so one bad file does not stop a batch;
  • check the upload size before opening it ($_FILES[...]['size'], upload_max_filesize), since documents are read into memory;
  • call close() in a finally block in long-running workers, so native memory is released on failure too. Documents are also freed when the object is destroyed, at the end of the request at the latest.
function pageCountOrNull(string $data): ?int
{
    try {
        $doc = Document::openBytes($data);
        try {
            return $doc->pageCount();
        } finally {
            $doc->close();
        }
    } catch (NexPdfException) {
        return null;
    }
}

On this page