Error handling
The exceptions NexPDF for PHP throws, the engine's error messages, license error codes, and patterns for handling failures and untrusted uploads.
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
| Exception | Thrown when |
|---|---|
Nexdevit\NexPdf\NexPdfException | An engine call failed. getMessage() is the engine's message. Subclass of RuntimeException. |
NexPdfException with getCode() < 0 | activateLicense rejected the key. The code is the negative status (see below); it is 0 for every other failure. |
NexPdfException | Document::open could not read the file (cannot read <path>) or save could not write it (cannot write <path>). |
LogicException | A method was called on a closed document (document is closed), or a document was cloned; use copy() instead. |
FFI\Exception | The native library could not be loaded, or FFI is restricted by the ffi.enable setting. See Troubleshooting. |
Error | Class "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
| Message | Cause | What to do |
|---|---|---|
password required | The file is encrypted and no password was given | Ask for the password and pass it to Document::open |
incorrect password | The password matches neither the user nor the owner password | Ask again |
no license activated: '<module>' requires a NexPDF license key | The call needs a module and no key is active | Call activateLicense in your bootstrap; see Running without a key |
the active license does not include the '<module>' module | Your plan does not include the module | Check NexPdf::isLicensed($module), or upgrade the plan |
internal error: ... | The engine caught an unexpected failure on this input | The 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:
| Code | Meaning |
|---|---|
| -1 | Malformed key |
| -2 | Bad signature (the key was changed, or it is not a NexPDF key) |
| -3 | Unsupported key version (update the SDK) |
| -4 | Expired |
| -5 | Not yet valid (check the system clock) |
| -6 | Wrong platform |
| -7 | Wrong 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
NexPdfExceptionper 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 afinallyblock 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;
}
}