PDF Generation in Go (Golang): Overview of Libraries and Tools
Creating PDF documents is a common task in web development. In the Go (Golang) ecosystem, there are several mature libraries that allow you to generate PDFs for both simple reports and complex multi-page documents with tables, graphics, and fonts. In this article, we will review the main tools, their capabilities, and provide practical code examples.
Why Go for PDF Generation?
Go is a compiled language with excellent performance and a simple concurrency model. For PDF generation tasks, this means:
- Fast processing of large volumes of data
- Easy integration with web servers (net/http)
- The ability to parallelize the generation of multiple documents
- No external dependencies (the binary includes everything needed)
Main Libraries for Working with PDF in Go
1. gofpdf — Simple and Popular Library
gofpdf is a port of the well-known PHP library FPDF to Go. It provides a simple API for creating PDFs with basic elements: text, lines, rectangles, images, and tables. The library requires no external dependencies and supports Unicode through font embedding.
Example of creating a simple PDF with gofpdf:
package main
import ( "github.com/jung-kurt/gofpdf")
func main() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.AddPage() pdf.SetFont("Arial", "B", 16) pdf.Cell(40, 10, "Hello, World!") pdf.OutputFileAndClose("hello.pdf")}Main features of gofpdf:
- UTF-8 support (requires embedding TrueType fonts)
- Automatic page breaks
- Vector graphics (lines, curves, fills)
- Image embedding (JPEG, PNG, GIF)
- Tables with dynamic data
2. UniPDF — Powerful Commercial Library
UniPDF (formerly FoxyUtils) is a professional solution for working with PDFs in Go. The library has an open source code, but a license is required for commercial use. UniPDF supports not only creation but also editing, conversion, and data extraction from PDFs.
Example of creating a PDF with UniPDF:
package main
import ( "os" "github.com/unidoc/unipdf/v3/creator")
func main() { c := creator.New() c.NewPage() // Adding text text := c.NewStyledParagraph() text.Append("UniPDF document example") text.SetFontSize(18) c.Draw(text) // Saving c.WriteToFile("unipdf_example.pdf")}Unique features of UniPDF:
- Full parsing and editing of existing PDFs
- Support for forms, annotations, and bookmarks
- HTML to PDF conversion
- Working with digital signatures
- Text and image extraction
3. Maroto — PDF Generator Based on Visual Components
Maroto is built on top of gofpdf and offers a higher-level approach using the concept of a "grid". This is convenient for creating reports, invoices, and other structured documents.
Example with Maroto:
package main
import ( "fmt" "github.com/johnfercher/maroto/v2/pkg/consts/align" "github.com/johnfercher/maroto/v2/pkg/consts/fontstyle" "github.com/johnfercher/maroto/v2/pkg/core" "github.com/johnfercher/maroto/v2/pkg/props" "github.com/johnfercher/maroto/v2")
func main() { m := maroto.New() m.Row(20, func() { m.Col(12, func() { m.Text("Invoice #123", props.Text{ Style: fontstyle.Bold, Align: align.Center, }) }) }) document, err := m.Generate() if err != nil {