Go has no built-in PDF or Office support, so document generation uses the standard library for text, HTML and CSV, and third-party packages for PDF and spreadsheets. Document management, meaning storing, versioning and serving files, is done with os, io/fs, net/http and an object store such as S3. For most reports the cleanest path is to render HTML with html/template and, when you need a PDF, convert it with a headless browser or use a PDF library.
Whatever format you pick, stream the output instead of building huge files in memory, and treat every user-supplied value as untrusted.
| Format | Approach | Notes |
|---|---|---|
| Plain text, Markdown, HTML | text/template, html/template | Standard library, HTML is auto-escaped |
| CSV | encoding/csv | Streams row by row |
| JSON, XML | encoding/json, encoding/xml | Standard library |
go-pdf/fpdf (maintained fork of gofpdf), or HTML to PDF via headless Chromium | The original jung-kurt/gofpdf is archived | |
| Excel (.xlsx) | github.com/xuri/excelize/v2 | Supports streaming writes |
| Word (.docx) | Fill a template with a docx library, or generate HTML or PDF instead | Library support is thinner than for PDF |
Check each library's licence and maintenance status before adopting it. Some PDF libraries, for example UniPDF, have commercial licences.
html/template escapes values for their context, which prevents cross-site scripting when documents include user data.
The customer name Acme <Ltd> is output as Acme <Ltd>. Parse templates once at startup, or embed them with //go:embed so the binary carries its own templates.
For simple, fixed layouts, a drawing library works well. The following uses github.com/go-pdf/fpdf, the maintained continuation of gofpdf, which keeps the same API.
For rich layouts (tables that paginate, CSS, web fonts), render HTML first and convert it with headless Chromium (for example through chromedp) or a service such as Gotenberg. This keeps design in HTML and CSS rather than in drawing code. Core PDF fonts do not cover all Unicode, so register a TrueType font when you need non-Latin text.
encoding/csv writes one record at a time, so memory use stays flat even for millions of rows. Always check Flush errors.
For .xlsx, excelize offers a StreamWriter for large sheets. If cell values come from users, prefix values that start with =, +, - or @ with a single quote or a tab to avoid spreadsheet formula injection.
Go gives you the pieces for a document store.
os.CreateTemp and write, then os.Rename into place so readers never see a half-written file. Use filepath.Join and validate user-supplied names to prevent path traversal; os.Root (Go 1.24+) confines file operations to a directory.Content-Type and Content-Disposition headers, and stream with io.Copy or http.ServeContent, which supports range requests and conditional requests.io.Writer (the HTTP response, a file, an S3 upload) instead of building a full buffer.http.MaxBytesReader, detect type with http.DetectContentType, and scan if users share files.log/slog and wrap errors with %w so failures can be traced.For programmatic layouts use go-pdf/fpdf. For complex, styled documents, render HTML and convert it with headless Chromium or Gotenberg. Check licences before choosing a commercial library.
Use html/template for HTML so values are escaped by context, and sanitise cell values that begin with formula characters in CSV and Excel exports.
Yes. The standard library parses CSV, JSON and XML, and libraries such as excelize read .xlsx. For PDF text extraction, use a dedicated library or an external tool.
Write them to object storage under generated keys, keep metadata in a database, and serve them through short-lived signed URLs or an authenticated handler.
Use templates for text and HTML, encoding/csv and excelize for tabular data, and a PDF library or an HTML-to-PDF converter for PDFs. Stream output, escape untrusted data, store files safely with versioned metadata, and move heavy generation into background jobs.