Go

Generate PDFs in Go

Render a PDF from a Go program with the standard library — no cgo and no wkhtmltopdf.

PDF options in Go often mean cgo bindings or shelling out to wkhtmltopdf. PDFgen keeps it pure Go: build a JSON body, POST it with net/http and copy the response to a file or HTTP writer.

The example below writes an invoice to disk; swap os.Create for your http.ResponseWriter to stream it to a browser.

Generate a PDF in Go

POST your HTML and data to /api/v1/generate with your API key, then save the PDF that comes back. Swap in your own HTML or a saved template_id and loop over records to render at scale.

package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
)
func main() {
payload, _ := json.Marshal(map[string]any{
"html": "<h1>Invoice {{number}}</h1><p>Billed to {{client}} — total {{total}}</p>",
"engine": "handlebars",
"format": "A4",
"data": map[string]string{
"number": "INV-001", "client": "Acme Corp", "total": "$2,400.00",
},
})
req, _ := http.NewRequest("POST", "https://pdfgen.com/api/v1/generate", bytes.NewReader(payload))
req.Header.Set("Authorization", "Bearer pdfg_live_xxx")
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
out, _ := os.Create("invoice.pdf")
defer out.Close()
io.Copy(out, res.Body)
}

Convert existing HTML to PDF in Go

Already have finished HTML? Skip templating — read an .html file and POST it with net/http and send it with engine: "legacy", which tells the API your markup is final.

package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
)
func main() {
// Your existing, already-styled HTML file.
html, _ := os.ReadFile("invoice.html")
payload, _ := json.Marshal(map[string]any{
"html": string(html),
"engine": "legacy",
"format": "A4",
})
req, _ := http.NewRequest("POST", "https://pdfgen.com/api/v1/generate", bytes.NewReader(payload))
req.Header.Set("Authorization", "Bearer pdfg_live_xxx")
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
out, _ := os.Create("invoice.pdf")
defer out.Close()
io.Copy(out, res.Body)
}

How it works

  1. 1

    Send your HTML + data

    POST an HTML template (with optional Handlebars tokens) and a data object to the API.

  2. 2

    We render the PDF

    PDFgen renders it server-side — no headless browser for you to run or scale.

  3. 3

    Get the PDF back

    Receive the PDF binary (or a hosted link with export_type:"url") and serve it from Go.

Good to know

  • Only the standard library — no cgo and no third-party PDF dependency.
  • Stream straight to a browser by copying res.Body into your http.ResponseWriter instead of a file.
  • In real code, check each error return; they’re elided here for brevity.

Frequently asked questions

What’s the best way to generate a PDF in Go?
POST your HTML and data to /api/v1/generate with net/http and copy the response body to a file or writer — no cgo or external binary.
Can I stream the PDF to an HTTP response?
Yes — io.Copy from res.Body into your http.ResponseWriter after setting a Content-Type of application/pdf.
Do I need a cgo-based library?
No. The call is plain HTTP, so your build stays pure Go and easy to cross-compile.

Generate PDFs in other languages

Your first PDF from Go in five minutes

Grab an API key, paste the snippet above, and render a PDF — no headless browsers to babysit. Build a reusable template with AI when you're ready.