How to load the UniPDF license key?

Loading the UniPDF license code can be done in one of two ways.

1. Loading via code.

2. Loading via an environment variable.

In both cases, the customer name and license code need to be provided.

Loading the UniPDF license code in code

Make sure you are loading the correct version of the license package. License package can be loaded via  github.com/unidoc/unipdf/v3/common/license where V is for UniPDF version 1.x.xx, V2 is for UniPDF version 2.x.xx , and V3 is for UniPDF version 3.x.xx). The following play illustrates the license code loading.

Make sure the key is separated the same way as mentioned in the below example

package main
		
import (
  "fmt"

  "github.com/unidoc/unipdf/v3/common/license"
)

// Example of an offline perpetual license key.
const offlineLicenseKey = `
-----BEGIN UNIDOC LICENSE KEY-----
contents here.
-----END UNIDOC LICENSE KEY-----
`

func init() {
  // The customer name needs to match the entry that is embedded in the signed key.
  customerName := `My Company`

  // Good to load the license key in `init`. Needs to be done prior to using the library, otherwise operations
  // will result in an error.
  err := license.SetLicenseKey(offlineLicenseKey, customerName)
  if err != nil {
    panic(err)
  }
}

func main() {
  lk := license.GetLicenseKey()
  if lk == nil {
    fmt.Printf("Failed retrieving license key")
    return
  }
  fmt.Printf("License: %s\n", lk.ToString())
}
	

Loading the UniPDF license via environment variables

Alternatively, the UniPDF license can be loaded via environment variables. This involves setting the following environment variables

  • UNIPDF_CUSTOMER_NAME  Needs to be set to the customer name.
  • UNIPDF_LICENSE_PATH Needs to point to a file location containing the license key.

If those environment variables are set, UniPDF will automatically load the license key.

For example on Linux:

export UNIPDF_CUSTOMER_NAME=UniDoc
export UNIPDF_LICENSE_PATH=/path/to/licenses/UniDoc.txt

On Windows:

set UNIPDF_CUSTOMER_NAME=UniDoc
set UNIPDF_LICENSE_PATH="C:\path\to\license.txt"

How to load the UniOffice license key?