Generating PDF Reports in Go (Golang): Libraries and Examples

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

Generating PDF Reports in Go (Golang): Library Overview and Code Examples



Go (Golang) is a modern compiled language increasingly chosen for building high-load server applications and microservices. One typical task in such systems is generating PDF reports (invoices, acts, statistical dashboards). In this article, we will review three popular libraries for PDF generation in Go, their strengths, and provide ready-made code examples.



1. gofpdf — The Classic for Creating PDFs from Scratch



The gofpdf library (now developed as jung-kurt/gofpdf or the fork go-pdf/fpdf) is a port of the well-known PHP library FPDF. It allows you to create PDF documents without external dependencies, using only Go's standard tools.



Installation

go get github.com/jung-kurt/gofpdf


Example: Simple Invoice

package main

import ( "github.com/jung-kurt/gofpdf")

func main() { pdf := gofpdf.New("P", "mm", "A4", "") pdf.SetFont("Arial", "B", 16) pdf.AddPage() pdf.CellFormat(190, 10, "Invoice No. 123 dated 01/01/2025", "0", 1, "C", false, 0, "") pdf.SetFont("Arial", "", 12) pdf.Ln(10) pdf.CellFormat(190, 10, "Product: Laptop", "1", 1, "L", false, 0, "") pdf.CellFormat(190, 10, "Price: 120,000 RUB", "1", 1, "L", false, 0, "") pdf.CellFormat(190, 10, "Total: 120,000 RUB", "1", 1, "L", false, 0, "") err := pdf.OutputFileAndClose("invoice.pdf") if err != nil { panic(err) }}


Pros: minimal dependencies, full control over every element, Unicode support (via AddUTF8Font).
Cons: low-level API — for complex reports, you'll need to write a lot of manual code.



2. maroto — High-Level Generation with Tables and Styles



Maroto is built on top of gofpdf but offers a more convenient API: tables, grids, automatic alignment, and built-in font support. Ideal for standard reports: invoices, lists, price lists.



Installation

go get github.com/johnfercher/maroto/v2


Example: Table with Data

package main

import ( "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.SetPageMargins(10, 10, 10) // Header m.Row(10, func() { m.Col(12, func() { m.Text("Sales Report", props.Text{ Style: props.Bold, Size: 16, Align: props.Center, }) }) }) // Table header := []string{"Product", "Quantity", "Price"} data := [][]string{ {"Monitor", "2", "30,000"}, {"Keyboard", "5", "7,500"}, } m.TableList(header, data, props.TableList{ HeaderProp: props.TableListContent{ Style: props.Bold, Size: 10, }, ContentProp: props.TableListContent{ Size: 9, }, }) document, _ := m.Generate() document.Save("report.pdf")}


Pros: rapid development, built-in styles, automatic page breaks.
Cons: less flexibility for non-standard layouts.



3. wkhtmltopdf — Converting HTML to PDF via a Go Wrapper



If you already have HTML templates (e.g., using Bootstrap), the easiest approach is to use wkhtmltopdf. It is not a Go library but a command-line utility; however, there is a Go wrapper called go-wkhtmltopdf. It accepts an HTML string and returns a PDF.



Installation

# First install wkhtmltopdf: https://wkhtmltopdf.org/downloads.htmlgo get github.com/SebastiaanKlippert/go-wkhtmltopdf

Recommendations