Generating PDF Reports in Go: Library Overview and Code 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): A Complete Guide



Creating PDF documents is a common task in web development, especially when building enterprise systems, financial applications, or educational platforms. The Go language (Golang) offers several powerful libraries for PDF generation, allowing you to create both simple text reports and complex multi-page documents with tables, charts, and images. In this article, we will look at three popular approaches: gofpdf, maroto, and integration with wkhtmltopdf.



1. gofpdf Library — Low-Level PDF Control



gofpdf is one of the oldest and most flexible libraries for PDF generation in Go. It provides full control over every page element but requires more code for complex layouts.



Installation

go get github.com/jung-kurt/gofpdf


Example: Simple Report with Header and Table

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, "Sales Report") pdf.Ln(12)

// Table headers pdf.SetFont("Arial", "B", 12) headers := []string{"Product", "Quantity", "Price"} for _, h := range headers { pdf.CellFormat(40, 10, h, "1", 0, "C", false, 0, "") } pdf.Ln(-1)

// Data pdf.SetFont("Arial", "", 12) data := [][]string{{"Laptop", "5", "45000"}, {"Mouse", "12", "1500"}} for _, row := range data { for _, cell := range row { pdf.CellFormat(40, 10, cell, "1", 0, "C", false, 0, "") } pdf.Ln(-1) }

err := pdf.OutputFileAndClose("report.pdf") if err != nil { panic(err) }}


Pros: minimal dependencies, full control, Unicode support (TrueType fonts).
Cons: no ready-made components (tables, charts), need to manually calculate positions.



2. maroto Library — High-Level Generation with Grid



maroto is built on top of gofpdf and offers a component-based approach with a grid, simplifying the creation of complex layouts. It is ideal for standard reports: invoices, waybills, price lists.



Installation

go get github.com/johnfercher/maroto/v2


Example: Report with Logo and Table

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.NewMaroto(maroto.WithMaxGridSize(12)) m.RegisterHeader(func() { m.Row(20, func() { m.Col(3, func() { m.FileImage("logo.png", props.Rect{ Percent: 80, Center: true, }) }) m.Col(9, func() { m.Text("TechnoService LLC", props.Text{ Style: props.Bold, Size: 16, }) }) }) })

m.Row(10, func() { m.Col(12, func() { m.Table([]string{"Name", "Qty", "Amount"}, [][]string{ {"24\\" Monitor", "2", "40000"}, {"Keyboard", "5", "7500"}, }, props.Table{ Content: props.TableContent{ GridSizes: []uint{6, 3, 3}, }, }) }) })

document, _ := m.Generate() _ = document.Save("invoice.pdf")}


Pros: rapid development, automatic alignment, built-in support for tables, QR codes, and barcodes.
Cons: more

Recommendations