PDF Generation in Go (Golang): Complete Overview of Libraries and Tools
Creating PDF documents is a common task in web development, report automation, and electronic document management. The Go language (Golang) offers several mature solutions for this task. In this article, we will thoroughly review the main libraries, their capabilities, usage examples, and help you choose the right tool for your project.
Why Go for PDF Generation?
Go combines the high performance of a compiled language with development simplicity. Thanks to built-in concurrency support (goroutines) and efficient memory management, PDF generation even on large volumes of data is fast. Additionally, static typing and a rich standard library make code reliable and predictable.
Main Libraries for PDF Generation in Go
1. gofpdf — Simple and Lightweight Library
gofpdf is a port of the well-known PHP library FPDF. It provides a basic API for creating PDFs from scratch: adding pages, fonts, images, tables, and simple graphic primitives.
Installation:
go get github.com/jung-kurt/gofpdfExample: Creating a simple PDF with text and an image
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, PDF World!") pdf.ImageOptions("logo.png", 10, 20, 30, 0, false, gofpdf.ImageOptions{ImageType: "PNG"}, 0, "") err := pdf.OutputFileAndClose("output.pdf") if err != nil { panic(err) }}Pros: minimalistic API, small size, no external dependencies, Unicode (UTF-8) support.
Cons: limited support for complex layouts, no built-in HTML rendering.
2. maroto — High-Level Library for Reports
Maroto is built on top of gofpdf and adds a "grid" concept for creating complex layouts: tables, columns, signatures, and logos. Ideal for invoices, waybills, and reports.
Installation:
go get github.com/johnfercher/maroto/v2Example: Creating an invoice with a table
package main
import ( "github.com/johnfercher/maroto/v2" "github.com/johnfercher/maroto/v2/pkg/components/col" "github.com/johnfercher/maroto/v2/pkg/components/row" "github.com/johnfercher/maroto/v2/pkg/components/text" "github.com/johnfercher/maroto/v2/pkg/core")
func main() { m := maroto.New() m.AddRow(20, text.NewCol(12, "Invoice No. 123")) m.AddRow(10, text.NewCol(12, "Date: 2024-01-15")) // Table headers := []string{"Product", "Qty", "Price", "Total"} rows := [][]string{ {"Laptop", "2", "50000", "100000"}, {"Mouse", "5", "1500", "7500"}, } m.AddRow(10, col.New(12).Add(text.New("Product Table"))) // For the table, use the table component // m.AddTable(headers, rows) doc, _ := m.Build() doc.Save("invoice.pdf")}Pros: declarative style, ready-made components (tables, QR codes, charts), automatic alignment.
Cons: more dependencies than gofpdf, some performance degradation on complex layouts.
3. unipdf — Commercial Library with Advanced Capabilities
UniPDF (from UniDoc) is a professional solution for working with PDF: creation, editing, data extraction, signing, encryption. Supports HTML-to-PDF conversion (via WebKit).
Installation:
go get github.com/unidoc/unipdf/v3Pr