When it comes to creating visually appealing and professional PDF documents using Golang, custom fonts play a significant role. While default system fonts can serve the purpose, incorporating custom fonts adds a unique touch to your documents and enhances their overall design.

In this blog post, we will delve into the process of implementing custom fonts in Golang PDF documents, exploring why custom fonts matter and how to seamlessly integrate them into your PDF generation workflow.

Understanding the Importance of Custom Fonts in PDF Documents

Fonts play a crucial role in document design, as they influence readability, aesthetics, and the overall impression of the content. While default system fonts are commonly used, they might not always align with your brand’s identity or the document’s intended tone. Custom fonts offer the advantage of uniqueness, allowing you to convey a specific style, professionalism, or creativity.

In the context of PDF documents generated using Golang, implementing custom fonts adds an extra layer of personalization. Whether you’re creating reports, presentations, invoices, or any other type of document, custom fonts enable you to align the visual appearance with your brand’s guidelines and create a consistent experience for your audience.

Introduction to Golang PDF Libraries

Before diving into implementing custom fonts, let’s briefly introduce the concept of Golang PDF libraries. Golang offers several libraries that enable developers to programmatically generate PDF documents. One such library is the popular UniPDF library, which provides a comprehensive set of tools for creating and customizing PDFs using Golang.

With UniPDF and similar libraries, you can define the layout, add text, images, shapes, and more to your PDF documents. Custom fonts integrate seamlessly with these libraries, allowing you to use non-standard fonts to enhance the appearance of your documents.

Choosing the Right Custom Font

Selecting the right custom font for your PDF documents is a crucial decision. The chosen font should align with your brand’s identity, the document’s purpose, and the readability requirements.

Here are a few considerations to keep in mind when choosing a custom font:

  • Readability: Prioritize fonts that are easy to read both on screen and in print. Complex or overly decorative fonts might hinder readability.

  • Brand Identity: If you have brand guidelines that include specific fonts, use those to maintain a consistent visual identity.

  • Document Tone: Consider the tone of your document. For formal documents, elegant and traditional fonts might be appropriate, while creative documents could benefit from more artistic fonts.

  • Licensing: Ensure that the custom font you choose is appropriately licensed for your intended use, especially if you’re creating documents for commercial purposes.

  • Compatibility: Confirm that the chosen font is compatible with the PDF generation library you plan to use in your Golang project.

Implementing Custom Fonts In Golang PDF Documents

Acquiring Custom Fonts

Before integrating custom fonts into your Golang PDF documents, you need to acquire the font files themselves. Custom fonts are typically distributed as TrueType Font (TTF) or OpenType Font (OTF) files. You can obtain custom fonts from various sources, including font marketplaces, online font libraries, or directly from font designers.

Configuring the Golang PDF Library

Once you have the custom font files, the next step is to configure your Golang PDF library to recognize and use these fonts. In the case of the UniPDF library, you need to load the font files and register them within the library. This is usually done by providing the font file path and a font name identifier.

Integrating Custom Fonts

After configuring the library, you can start using custom fonts in your PDF documents. Specify the chosen custom font when adding text to your document, and the library will automatically render the text using the selected font.

Here’s a high-level overview of the process using the UniPDF library:

  • Load the Custom Font: To integrate a custom font into your PDF document, you can use the model.NewCompositePdfFontFromTTFFile function if you’re working with a composite TrueType font. This function will enable you to load a TTF font file with full Unicode support. Alternatively, if your needs are limited to the standard character codes (32-255), you should use the model.NewPdfFontFromTTFFile function to load a standard TTF font file.

  • Add Text with Custom Font: When adding text to the PDF document using the Text function, use SetFont when adding the font into paragraph text to specify the custom font by providing the font family and style. The library will render the text using the custom font.

Styling Text with Custom Fonts

Integrating custom fonts is not only about using them; it’s also about leveraging their features to enhance the visual appeal of your text. Custom fonts often come with various styles (such as regular, bold, italic) and size variations. When working with the UniPDF library, you can use these font styles and sizes to format your text accordingly.

Here’s a basic example of how you can style text using custom fonts with UniPDF:

package main

import (
	"log"
	"os"

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

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 := license.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
	if err != nil {
		panic(err)
	}
}

func main() {
	compositeFont, err := model.NewCompositePdfFontFromTTFFile("./rounded-mplus-1p-regular.ttf")
	if err != nil {
		log.Fatalf("Unable to load font file: %v", err)
	}

	c := creator.New()
	c.SetPageMargins(50, 50, 100, 70)

	// Subset the font.
	// Composite fonts usually quite big and in turn would enlarge the document size if we embed all the runes/glyphs
	// This setting will embed the runes/glyphs that are used in the document
	// and in turn would reduce the document size by a lot.
	//
	// For example, in this case, the PDF file output size is reduced from 1.4 MB to 74 KB
	c.EnableFontSubsetting(compositeFont)

	p := c.NewParagraph("こんにちは世界")
	p.SetFont(compositeFont)
	p.SetFontSize(48)
	p.SetMargins(85, 0, 150, 0)
	p.SetColor(creator.ColorRGBFrom8bit(56, 68, 77))
	err = c.Draw(p)
	if err != nil {
		log.Fatalf("Unable to draw paragraph: %v", err)
	}

	p = c.NewParagraph("UniPDFへようこそ")
	p.SetFont(compositeFont)
	p.SetFontSize(48)
	p.SetMargins(85, 0, 0, 0)
	p.SetColor(creator.ColorRGBFrom8bit(56, 68, 77))
	err = c.Draw(p)
	if err != nil {
		log.Fatalf("Unable to draw paragraph: %v", err)
	}

	p = c.NewParagraph("Welcome To UniPDF")
	p.SetFont(compositeFont)
	p.SetFontSize(30)
	p.SetMargins(85, 0, 0, 0)
	p.SetColor(creator.ColorRGBFrom8bit(45, 148, 215))
	err = c.Draw(p)
	if err != nil {
		log.Fatalf("Unable to draw paragraph: %v", err)
	}

	if err := c.WriteToFile("pdf-load-custom-font.pdf"); err != nil {
		log.Fatalf("Unable to write PDF file: %v", err)
	}
}

Considerations and Best Practices

While integrating custom fonts in PDF documents can greatly enhance visual appeal, it’s important to keep some considerations and best practices in mind:

  • Font File Path: Ensure that you provide the correct file path to the custom font when loading it into the PDF library.

  • Font Styles and Sizes: Utilize the various font styles and sizes available in the custom font to create a hierarchy of text elements and improve readability.

  • Document Size: Be mindful of the font size relative to the document size. Fonts that are too small might lead to readability issues, especially when documents are printed.

  • Font Licensing: Respect font licensing terms and ensure that you have the necessary rights to use the custom font in your documents.

  • Testing: Always test your generated PDF documents on different devices and platforms to ensure consistent rendering of custom fonts.

Conclusion

Implementing custom fonts in Golang PDF documents is a powerful way to elevate the visual quality of your documents and create a more personalized experience for your audience. By selecting the right custom font and integrating it into your document workflows, you can enhance brand identity, maintain a consistent style, and improve overall document aesthetics.

Golang PDF libraries like UniPDF make it relatively straightforward to load, register, and use custom fonts in your PDF documents. Remember to follow best practices, test your documents thoroughly, and respect font licensing terms to create polished and professional PDF documents that effectively convey your intended message.