Skip to Content
NexPDF

License

Activate a NexPDF license key in Rust, check which modules it unlocks, and handle license errors.

English

Call nexpdf_sdk::license::activate once per process, before other NexPDF calls. It accepts the license XML file's text or the bare NEX2. key. See Licensing for how keys are issued and bound.

Activate

use nexpdf_sdk::license::{self, Activation};

fn main() -> nexpdf_sdk::Result<()> {
    let xml = std::fs::read_to_string("nexpdf_license.xml")?;
    match license::activate(&xml, "server", "com.acme.invoicing")? {
        Activation::Trial => println!("trial"),
        Activation::Commercial => println!("licensed"),
    }
    Ok(())
}
ParameterValue
key_or_xmlThe license XML or the bare key
platformOne of the platforms on the key: server, linux, macos, windows, ios, android or web (license::PLATFORMS lists them)
app_idThe application id the key was issued for

It returns Activation::Commercial for a commercial key and Activation::Trial for a trial key.

Reading the key from the environment

On servers, keep the key out of the image and read it from a secret:

let key = std::env::var("NEXPDF_LICENSE_KEY").expect("NEXPDF_LICENSE_KEY is not set");
let app_id = std::env::var("NEXPDF_APP_ID").unwrap_or_else(|_| "com.acme.invoicing".to_owned());
nexpdf_sdk::license::activate(&key, "server", &app_id)?;

Check the status

use nexpdf_sdk::license;

let status = nexpdf_sdk::license_status_json_value()?;   // `serde` feature; or license::status_json()
println!("{} {} {}", status["state"], status["tier"], status["days_left"]);

if license::is_licensed("signatures") {
    println!("signing is available");
}

license::status_json() returns the JSON as a String; with the serde feature, nexpdf_sdk::license_status_json_value() parses it. It has state (unlicensed, trial, licensed), licensee, id, tier, modules, expires_at, days_left and watermark. license::is_licensed(module) returns a bool and needs no feature.

Handle errors

A rejected key is Error::License with the negative code in code and the engine's message in message:

use nexpdf_sdk::{license, Error};

match license::activate(&key, "server", "com.acme.invoicing") {
    Ok(_) => {}
    Err(Error::License { code: -4, .. }) => {
        eprintln!("NexPDF license expired: renew it in the dashboard");
    }
    Err(Error::License { code: code @ (-6 | -7), message }) => {
        eprintln!("NexPDF license is for another platform or app id ({code}): {message}");
    }
    Err(e) => eprintln!("NexPDF license rejected: {e}"),
}

A call that needs a module your key does not include returns Error::Engine:

if let Err(e) = doc.encrypt_aes256("user", "owner") {
    eprintln!("{e}"); // nexpdf: the active license does not include the 'security' module
}
CodeMeaning
-1Malformed key
-2Bad signature
-3Unsupported key version
-4Expired
-5Not yet valid (check the clock)
-6Wrong platform
-7Wrong application id

Without a license, or after a rejected key, the SDK stays in viewer mode: it opens, renders, extracts text and searches, and rendered pages carry the "NexPDF • Unlicensed" watermark.

On this page