Skip to Content
NexPDF

License

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

English

Call nexpdf.activate_license 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

import nexpdf

with open("nexpdf_license.xml", encoding="utf-8") as f:
    result = nexpdf.activate_license(f.read(), "server", "com.acme.invoicing")

print("trial" if result == 1 else "licensed")
ParameterValue
key_or_xmlThe license XML or the bare key
platformserver (default), linux, macos or windows: one of the platforms on the key
app_idThe application id the key was issued for

It returns 2 for a commercial key and 1 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:

import os
import nexpdf

nexpdf.activate_license(os.environ["NEXPDF_LICENSE_KEY"], "server", os.environ.get("NEXPDF_APP_ID", "com.acme.invoicing"))

Check the status

status = nexpdf.license_status()
print(status["state"], status.get("tier"), status.get("days_left"))

if nexpdf.is_licensed("signatures"):
    print("signing is available")

license_status() returns the parsed JSON: state (unlicensed, trial, licensed), licensee, id, tier, modules, expires_at, days_left and watermark.

Handle errors

A rejected key raises nexpdf.LicenseError, a subclass of nexpdf.NexPdfError, with the negative code in code:

try:
    nexpdf.activate_license(key, "server", "com.acme.invoicing")
except nexpdf.LicenseError as e:
    if e.code == -4:
        log.error("NexPDF license expired: renew it in the dashboard")
    elif e.code in (-6, -7):
        log.error("NexPDF license is for another platform or app id: %s", e)
    else:
        log.error("NexPDF license rejected (%d): %s", e.code, e)

A call that needs a module your key does not include raises nexpdf.NexPdfError:

try:
    doc.encrypt_aes256("user", "owner")
except nexpdf.NexPdfError as e:
    print(e)  # 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