License
Activate a NexPDF license key in Rust, check which modules it unlocks, and handle license errors.
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(())
}| Parameter | Value |
|---|---|
key_or_xml | The license XML or the bare key |
platform | One of the platforms on the key: server, linux, macos, windows, ios, android or web (license::PLATFORMS lists them) |
app_id | The 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
}| Code | Meaning |
|---|---|
| -1 | Malformed key |
| -2 | Bad signature |
| -3 | Unsupported key version |
| -4 | Expired |
| -5 | Not yet valid (check the clock) |
| -6 | Wrong platform |
| -7 | Wrong 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.