Document to PDF using custom fonts

This guide will teach you how to change the font style of the document by converting it to PDF.

Before you begin

You should get your API key from your UniCloud account.

If this is your first time using UniOffice SDK, follow this guide to set up a local development environment.

Clone the project repository

In your terminal, clone the examples repository. It contains the Go code we will be using for this guide.

git clone https://github.com/unidoc/unioffice-examples

Navigate to the path document/doc-to-pdf-fonts folder in the unioffice-examples directory.

cd unioffice-examples/document/doc-to-pdf-fonts/

You can also find the “fonts.docx” file in the initial directory, which will be used in the code of the guide.

Normal page

How it works

/*
* This example showcases PDF generation from docx document with UniOffice package.
*/
package main
import (
"fmt"
"log"
"os"
unipdflicense "github.com/unidoc/unipdf/v4/common/license"
"github.com/unidoc/unipdf/v4/model"
"github.com/unidoc/unioffice/v2/common/license"
"github.com/unidoc/unioffice/v2/document"
"github.com/unidoc/unioffice/v2/document/convert"
)
func init() {
// Make sure to load your metered License API key prior to using the library.
// If you need a key, you can sign up and create a free one at https://cloud.unidoc.io
err := unipdflicense.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
fmt.Printf("ERROR: Failed to set metered key: %v\n", err)
fmt.Printf("Make sure to get a valid key from https://cloud.unidoc.io\n")
fmt.Printf("If you don't have one - Grab one in the Free Tier at https://cloud.unidoc.io\n")
panic(err)
}
// This example requires both for unioffice and unipdf.
err = license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
if err != nil {
fmt.Printf("ERROR: Failed to set metered key: %v\n", err)
fmt.Printf("Make sure to get a valid key from https://cloud.unidoc.io\n")
fmt.Printf("If you don't have one - Grab one in the Free Tier at https://cloud.unidoc.io\n")
panic(err)
}
}
func main() {
// register all fonts from the folder
err := convert.RegisterFontsFromDirectory("fonts/PTSans")
if err != nil {
log.Fatalf("Error registering fonts from the folder: %s\n", err)
}
// register fonts in more precise way
zcoolRegular, err := model.NewCompositePdfFontFromTTFFile("fonts/ZCOOL/ZCOOLXiaoWei-Regular.ttf")
if err != nil {
log.Fatalf("error opening font: %s\n", err)
}
convert.RegisterFont("SimHei", convert.FontStyle_Regular, zcoolRegular) // we can use one font instead of other
doc, err := document.Open("fonts.docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
defer doc.Close()
c := convert.ConvertToPdf(doc)
err = c.WriteToFile("fonts.pdf")
if err != nil {
log.Fatalf("error converting document: %s", err)
}
}

Lines 7-18 import the UniOffice packages and other required dependencies.

The init function in lines 20-29 authenticates your request with your UNIDOC_LICENSE_API_KEY.

The main function spans from line 41 to 67, demonstrating two methods for registering new external fonts. The first method, found at line 44, registers all the fonts from a specified directory using the RegisterFontsFromDirectory function.

Another more specific method involves obtaining the font file using the NewCompositePdfFontFromTTFFile method, as seen in line 50, and then, as shown in line 54, registering it using the RegisterFont method.

Subsequently, the document is opened in line 56, and at line 61, it is converted to PDF using the defined font with the ConvertToPdf method. The result is written as a PDF in line 63.

Run the code

Execute the following command to convert the Word document into a PDF document with a custom font style.

go run main.go

Sample output

Page with custom paragraph style

Got any Questions?

We're here to help you.