Skip to Content
NexPDF

Error handling

The exceptions NexPDF for Flutter throws, the engine's error messages, license errors, errors from dart:ffi calls and the native reader, and patterns for untrusted files.

English

The Dart API either returns its result or throws. The SDK reads the engine's per-thread error right after a failed call and throws NexPdfException with that message. For C functions you call yourself through dart:ffi, the nexpdfCall helper from Calling the C API does the same.

Exception types

ExceptionThrown when
NexPdfExceptionAn engine call failed. e.message is the engine's message. Thrown by open, openFile, renderPagePng, save, ask and activateLicense, and by nexpdfCall.
StateErrorA NexPdfDocument was used after dispose() (document was disposed).
ArgumentErrordart:ffi could not load the engine library or find a function in it (NexPdfFlutter(), DynamicLibrary.open, lookupFunction).
FileSystemExceptionopenFile could not read the file.
PlatformExceptionNexPdfReader.open failed on the native side; code is NEXPDF.
MissingPluginExceptionNexPdfReader was called where the plugin has no native side (macOS, Windows, Linux), or before a full rebuild after adding the plugin.
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:nexpdf_flutter/nexpdf_flutter.dart';

final sdk = NexPdfFlutter();

Uint8List? stampUpload(Uint8List data) {
  NexPdfDocument? doc;
  try {
    doc = sdk.open(data);
    // ... engine calls through nexpdfCall ...
    return doc.save();
  } on NexPdfException catch (e) {
    debugPrint('NexPDF could not process the upload: ${e.message}');
    return null;
  } finally {
    doc?.dispose();
  }
}

Calls that do not throw

A few methods return an empty result instead of throwing:

MethodOn failure
search(query)[] (also for an empty query)
toMarkdown()''
pageView(i)A plain view built from pageSize(i)

Call sdk.lastError() straight after them, on the same isolate, if you need to know why.

Common engine messages

MessageCauseWhat to do
password requiredThe file is encrypted and no password was givenAsk for the password and pass it to sdk.open(bytes, password:)
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 at start-up; see Running without a key
the active license does not include the '<module>' moduleYour plan does not include the moduleCheck the license status modules, or upgrade the plan
internal error: ...The engine caught an unexpected failure on this inputThe app keeps running; report the file to support
empty documentopen was given no bytesCheck the file was read

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 errors

activateLicense returns 2 for a commercial key and 1 for a trial key. A rejected key throws NexPdfException with the engine's message; the numeric code is not part of the exception. The messages map to the codes like this:

MessageCodeMeaning
malformed license key-1The text is not a NexPDF key
license key signature is not valid-2The key was changed, or it is not a NexPDF key
license key format version is not supported by this SDK-3Update the SDK
license expired at <time> (Unix time)-4Renew the key
license is not valid yet (issued at <time>); check the device clock-5The device clock is behind
license is not valid on platform '<platform>'-6The key does not list NexPdfFlutter.platform
license is not valid for application '<id>'-7The app id does not match the key

To get the code itself, call nexpdf_license_activate through dart:ffi as shown in License. A rejected key leaves the previous license state as it was, so an app that had no key stays in view-only mode.

The last error

sdk.lastError() returns the engine's last error message for the current thread, or null. Errors are kept per thread, so read it on the isolate that made the failing call, straight after the call. The SDK and nexpdfCall already do this for you.

The native reader

NexPdfReader.open throws PlatformException with code NEXPDF when the reader cannot be shown, for example no view controller to present the reader from on iOS, or when the document cannot be written to the cache on Android. Errors inside the reader (a wrong password, a missing module) are shown to the user by the reader itself.

Untrusted input

The engine is written in Rust and fuzzed; a malformed PDF produces a NexPdfException, not a crash. When you open files the user picks or downloads:

  • catch NexPdfException per file, so one bad file does not stop a batch;
  • check the file size before you open it, since documents are read into memory;
  • dispose documents in a finally block, so native memory is released on failure too;
  • open large or unknown files in a background isolate, so a slow file does not freeze the UI.
int? pageCountOrNull(Uint8List data) {
  NexPdfDocument? doc;
  try {
    doc = sdk.open(data);
    return doc.pageCount;
  } on NexPdfException {
    return null;
  } finally {
    doc?.dispose();
  }
}

On this page