Generating PDF Reports in Go (Golang): Library Overview and Practical Examples
Go (Golang) is a modern compiled language that is ideal for creating high-load server applications, microservices, and command-line utilities. One common task in web development is generating reports in PDF format: invoices, waybills, analytical dashboards, or certificates. In this article, we will look at three popular libraries for working with PDF in Go, their capabilities, and provide working code examples.
Why Go for PDF Generation?
Go offers several advantages: high performance, ease of deployment (a single binary), built-in concurrency support, and a rich standard library. Generating PDF does not require installing bulky runtimes (like Java or .NET), making Go solutions compact and fast. However, unlike Python, which has powerful libraries like ReportLab, the Go ecosystem for PDF is more diverse, and the choice of tool depends on the complexity of the report.
1. gofpdf — Basic PDF Generation
gofpdf is a port of the well-known PHP library FPDF. It allows you to create PDF documents "from scratch": adding text, images, lines, tables, and fonts. The library does not support HTML templates, so all layout is defined programmatically.
Installation:
go get github.com/jung-kurt/gofpdfExample: Creating a Simple Invoice
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, "Invoice No. 123") pdf.Ln(12) pdf.SetFont("Arial", "", 12) pdf.Cell(40, 10, "Date: 2024-01-15") pdf.Ln(10) pdf.Cell(40, 10, "Product: Laptop") pdf.Ln(10) pdf.Cell(40, 10, "Amount: 50,000 RUB") err := pdf.OutputFileAndClose("invoice.pdf") if err != nil { panic(err) }}Pros: lightweight, full control over positioning, Unicode support via TTF fonts.
Cons: no built-in HTML support, complexity when creating complex layouts.
2. maroto — Simplified Report Creation
Maroto is built on top of gofpdf but provides a high-level API for creating reports. You work with concepts like "grid", "rows", "columns", "tables". This resembles working with CSS frameworks.
Installation:
go get github.com/johnfercher/maroto/v2Example: 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.NewMaroto(maroto.WithPageSize("A4")) m.RegisterHeader(func() { m.Row(20, func() { m.Col(12, func() { m.Text("Sales Report", props.Text{Bold: true, Size: 16}) }) }) })
headers := []string{"Product", "Quantity", "Price"} contents := [][]string{ {"Laptop", "2", "100000"}, {"Mouse", "5", "2500"}, }
m.Table(headers, contents, props.Table{}) document, _ := m.Generate() _ = document.Save("report.pdf")}Pros: declarative style, automatic alignment, built-in support for tables and charts.
Cons: less flexibility for custom layouts compared to gofpdf.
3. wkhtmltopdf — HTML to PDF
If you need to convert an HTML template to PDF (e.g., with CSS styles), use wkhtmltopdf — a wrapper around the wkhtmltopdf utility (WebKit). This is not a pure Go library, but it allows you to leverage the full power of HTML/CSS for report design.
Also in library
OAuth Client for JavaScript (Node.js): Overview and Usage Examples
Working with Apache Kafka in Go (Golang): Libraries and Examples
Swagger/OpenAPI Generation for PHP: Best Tools and Libraries
Authorization and Authentication in Rust: A Complete Guide
Logging for JavaScript (Node.js): Best Solutions and Libraries