License
Activate a NexPDF license key in Go, check which modules it unlocks, and handle license errors.
Call nexpdf.ActivateLicense 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
key, err := os.ReadFile("nexpdf_license.xml")
if err != nil {
log.Fatal(err)
}
result, err := nexpdf.ActivateLicense(string(key), "server", "com.acme.invoicing")
if err != nil {
log.Fatal(err)
}
if result == 1 {
fmt.Println("trial")
} else {
fmt.Println("licensed")
}| Parameter | Value |
|---|---|
keyOrXML | The license XML or the bare key |
platform | server, linux, macos or windows: one of the platforms on the key |
appID | The application id the key was issued for |
It returns 2 for a commercial key and 1 for a trial key. nexpdf.Platforms lists every
platform name a key can be bound to.
Reading the key from the environment
On servers, keep the key out of the image and read it from a secret:
appID := os.Getenv("NEXPDF_APP_ID")
if appID == "" {
appID = "com.acme.invoicing"
}
if _, err := nexpdf.ActivateLicense(os.Getenv("NEXPDF_LICENSE_KEY"), "server", appID); err != nil {
log.Fatal(err)
}Check the status
import "encoding/json"
type LicenseStatus struct {
State string `json:"state"`
Licensee string `json:"licensee"`
ID string `json:"id"`
Tier string `json:"tier"`
Modules []string `json:"modules"`
ExpiresAt int64 `json:"expires_at"` // Unix seconds, 0 when it does not expire
DaysLeft *int `json:"days_left"` // null when it does not expire
Watermark bool `json:"watermark"`
}
s, err := nexpdf.LicenseStatusJSON()
if err != nil {
log.Fatal(err)
}
var status LicenseStatus
if err := json.Unmarshal([]byte(s), &status); err != nil {
log.Fatal(err)
}
fmt.Println(status.State, status.Tier, status.Modules)
if nexpdf.IsLicensed("signatures") {
fmt.Println("signing is available")
}LicenseStatusJSON() returns state (unlicensed, trial, licensed), licensee, id,
tier, modules, expires_at, days_left and watermark. Without a license only state,
modules (["viewer"]) and watermark are present.
Handle errors
A rejected key returns a *nexpdf.Error with the negative code in Code:
import "errors"
if _, err := nexpdf.ActivateLicense(key, "server", "com.acme.invoicing"); err != nil {
var e *nexpdf.Error
if errors.As(err, &e) {
switch e.Code {
case -4:
log.Print("NexPDF license expired: renew it in the dashboard")
case -6, -7:
log.Printf("NexPDF license is for another platform or app id: %s", e.Message)
default:
log.Printf("NexPDF license rejected (%d): %s", e.Code, e.Message)
}
}
}A call that needs a module your key does not include returns a *nexpdf.Error with Code 0:
if _, err := doc.EncryptAES256("user", "owner"); err != nil {
fmt.Println(err) // 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.