How to load the UniOffice license key?
Load it before you touch anything else in the library. UniOffice checks the
license on the first operation, so a key loaded partway through main is loaded
too late and every call before it fails. An init function is the conventional
place.
Which call you use depends on which kind of license you have.
| License | Call | You supply |
|---|---|---|
| Metered (API key) | license.SetMeteredKey(apiKey) | The key string from UniCloud. |
| Offline | license.SetLicenseKey(content, customerName) | The signed key content and the customer name embedded in it. |
Both live in github.com/unidoc/unioffice/v2/common/license.
Metered API key
This is what the free tier gives you and what every example in the guides uses. Read the key from an environment variable rather than committing it:
package main
import (
"os"
"github.com/unidoc/unioffice/v2/common/license"
)
func init() {
if err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)); err != nil {
panic(err)
}
}The library does not read UNIDOC_LICENSE_API_KEY on its own. Your program reads
it and passes the value in, which is why the os.Getenv call is in every
example. Setting the variable without the SetMeteredKey call does nothing.
Offline license
An offline license is verified from its own signature and makes no outbound connection, which suits OEM distribution and networks that block egress. The customer name has to match the one embedded in the signed key.
const offlineLicenseKey = `
-----BEGIN UNIDOC LICENSE KEY-----
contents here.
-----END UNIDOC LICENSE KEY-----
`
func init() {
if err := license.SetLicenseKey(offlineLicenseKey, `My Company`); err != nil {
panic(err)
}
}Keep the key content exactly as issued, including the BEGIN and END lines and
the line breaks between them. Reflowing it breaks the signature.
Letting the environment load an offline license
An offline license can be loaded without any code at all. If both of these are set, UniOffice reads the file at startup and applies it:
| Variable | Meaning |
|---|---|
UNIOFFICE_LICENSE_PATH | Path to a file containing the signed license key. |
UNIOFFICE_CUSTOMER_NAME | The customer name embedded in that key. |
Both must be set; if either is missing the loader returns without doing anything. Failures to read or apply the file are logged rather than returned, so a wrong path leaves the program unlicensed with only a log line to say why.
This applies to offline licenses only. There is no equivalent for metered keys,
which always go through SetMeteredKey.
A third variable, UNIDOC_LICENSE_DIR, sets where metered license information is
cached. It defaults to your home directory, and matters mainly in containers or
under service accounts that have no writable home.
Checking what was loaded
GetLicenseKey returns the active key, or nil if none was loaded:
lk := license.GetLicenseKey()
if lk == nil {
fmt.Println("no license loaded")
return
}
fmt.Printf("License: %s\n", lk.ToString())If you are converting documents to PDF, one key is not enough: UniPDF keeps its own license state and needs the same key loaded separately. See Why does my license key fail when converting to PDF?