PDF Generation in Go: Library Overview and Practical Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

PDF Generation in Go (Golang): A Complete Overview of Tools and Libraries



Modern web applications and microservices often require PDF document generation: reports, invoices, waybills, certificates, or simply data export. The Go language 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 on pythonlib.ru.



Why Use Go for PDF Generation?



Go (Golang) is a compiled, statically typed language known for its performance and simplicity. When generating PDFs, this provides the following advantages:

  • High speed — Go binaries run fast and use memory efficiently.
  • Ease of deployment — a single executable file with no external dependencies (except for cases with wkhtmltopdf).
  • Built-in concurrency support — you can process multiple PDF generation requests simultaneously.
  • Cross-platform — build once and run on Linux, Windows, macOS.


Main Libraries for Working with PDF in Go



Let's look at three popular approaches: a pure Go implementation (gofpdf), a professional commercial library (uniPDF), and HTML to PDF conversion (wkhtmltopdf).



1. gofpdf — Simple and Lightweight Library



gofpdf (now github.com/jung-kurt/gofpdf) is one of the first and most common libraries for creating PDFs in pure Go. It requires no external dependencies and allows you to programmatically draw text, lines, images, and tables.



Example of 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, world!") // Add an image (logo) pdf.ImageOptions("logo.png", 10, 20, 50, 0, false, gofpdf.ImageOptions{ImageType: "PNG", ReadDpi: true}, 0, "") // Save to file err := pdf.OutputFileAndClose("output.pdf") if err != nil { panic(err) }}


Pros:

  • Fully in Go — no external programs.
  • Lightweight and easy to install.
  • Unicode support (including Cyrillic) via built-in fonts.

Cons:

  • Limited support for complex layouts (e.g., no ready-made solutions for complex tables).
  • Manual element positioning — suitable for simple documents.


2. UniPDF — Professional Solution for Complex Tasks



UniPDF (from github.com/unidoc/unipdf/v3) is a powerful commercial library that provides almost complete control over PDF. It supports creating, editing, converting, and extracting data from PDF. A free version with limitations is available for educational and non-commercial projects.



Example of creating a report with a table:



package main

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

func main() { // Specify the license (free for testing) license.SetMeteredKey("your_key") c := creator.New() c.NewPage() // Title chapter := c.NewChapter("Monthly Report") chapter.Add(c.NewParagraph("Sales Data")) // Table table := c.NewTable(3) table.SetColumnWidths(100, 100, 100) headers := []string{"Product", "Quantity", "Amount"} for _, h := range headers { cell := table.NewCell() cell.SetBackgroundColor(creator.Col

Recommendations