PricingDocumentationCase StudiesBlogLoginGet started →

Guides

HTML to PDF in Go

Generate PDFs from HTML templates in Go with standard library HTTP clients. No CGO dependencies.

Why Go developers love a PDF API

Go's ecosystem for PDF generation is limited. Libraries like gofpdf lack modern CSS support, and binding Chromium via CGO introduces complexity and larger binaries. A REST API keeps your Go application lightweight, statically linked, and free of heavy dependencies.

Rendering a PDF with net/http

render.go
package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "net/http"
)

func main() {
    payload := map[string]any{
        "html": "<h1>Hello {{name}}</h1>",
        "data": map[string]string{"name": "World"},
    }
    body, _ := json.Marshal(payload)

    req, _ := http.NewRequest("POST", "https://api.pdfapi.dev/v1/render", bytes.NewReader(body))
    req.Header.Set("Authorization", "Bearer YOUR_API_KEY")
    req.Header.Set("Content-Type", "application/json")

    resp, _ := http.DefaultClient.Do(req)
    defer resp.Body.Close()

    var result map[string]any
    json.NewDecoder(resp.Body).Decode(&result)
    fmt.Println("PDF URL:", result["url"])
}

Production tips for Go

Use http.Client with timeouts instead of http.DefaultClient. Consider retry logic with exponential backoff for transient failures. For high-throughput services, reuse HTTP connections and batch template renders when possible.

Next steps