Why does my license key fail when converting to PDF?
Because converting to PDF uses two libraries, and each keeps its own license state. Loading the key into UniOffice does nothing for UniPDF, so a program that sets only one gets a license error the moment conversion starts, even though the key is valid and the rest of the program works.
This is the single most common support question about UniOffice licensing. The usual symptom is that reading, editing and saving a document all work, and the failure appears only on the call that produces the PDF.
The fix
Load the same key into both, before anything else runs. Both packages are called
license, so one of the imports needs an alias:
import (
"os"
"github.com/unidoc/unioffice/v2/common/license"
unipdflicense "github.com/unidoc/unipdf/v5/common/license"
)
func init() {
if err := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)); err != nil {
panic(err)
}
if err := unipdflicense.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`)); err != nil {
panic(err)
}
}It is one key, not two. The same UNIDOC_LICENSE_API_KEY satisfies both
libraries, and using it in both places costs nothing extra. There is no second
key to buy and no separate UniPDF account to create.
Which operations need both
| Operation | UniOffice key | UniPDF key |
|---|---|---|
| Creating, reading or editing DOCX, XLSX and PPTX | Yes | No |
document/convert.ConvertToPdf | Yes | Yes |
spreadsheet/convert.ConvertToPdf | Yes | Yes |
presentation/convert.ConvertToPdf | Yes | Yes |
| Exporting to PDF through Word using OLE | Yes | No |
The OLE path is the exception because Word does the conversion, not UniPDF. Every native converter goes through UniPDF and needs both.
The reason is visible in the return type: ConvertToPdf hands back a UniPDF
*creator.Creator rather than bytes. Your program is holding a UniPDF object and
calling UniPDF methods on it, which is what trips the check.
Your go.mod needs both modules for the same reason:
require (
github.com/unidoc/unioffice/v2 v2.13.0
github.com/unidoc/unipdf/v5 v5.0.0
)If it still fails
Check that the failing call is the one you think it is. Both libraries report a license problem the same way, so the message alone does not say which one is unlicensed. Setting one key and then deliberately omitting the other is a quick way to confirm which half is missing.
Also check that init runs before the conversion. A key loaded partway through
main, after a Creator already exists, is loaded too late.
For loading the key itself, and the difference between metered and offline licenses, see How to load the UniOffice license key? and the getting started guide.