Generate PDFs with Go

Complete Go code examples for the PixDoc API

Install

Terminal
# Uses Go's standard library — no dependencies

Generate a PDF

Send HTML to the /api/v1/pdf endpoint and save the binary response.

Go
package main

import (
    "bytes"
    "encoding/json"
    "io"
    "net/http"
    "os"
)

func main() {
    body, _ := json.Marshal(map[string]string{"html": "<h1>Hello World</h1>"})
    req, _ := http.NewRequest("POST", "https://pixdoc.dev/api/v1/pdf", bytes.NewBuffer(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()

    out, _ := os.Create("output.pdf")
    io.Copy(out, resp.Body)
}

Take a Screenshot

The /api/v1/screenshot endpoint returns a PNG image.

Go
package main

import (
    "bytes"
    "encoding/json"
    "io"
    "net/http"
    "os"
)

func main() {
    body, _ := json.Marshal(map[string]string{"html": "<h1>Hello World</h1>"})
    req, _ := http.NewRequest("POST", "https://pixdoc.dev/api/v1/screenshot", bytes.NewBuffer(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()

    out, _ := os.Create("screenshot.png")
    io.Copy(out, resp.Body)
}

Use a Template

Pass a template_id and variables instead of raw HTML.

Go
package main

import (
    "bytes"
    "encoding/json"
    "io"
    "net/http"
    "os"
)

func main() {
    payload := map[string]interface{}{
        "template_id": "invoice",
        "variables": map[string]string{
            "company": "Acme Corp",
            "total":   "$1,250.00",
        },
    }
    body, _ := json.Marshal(payload)
    req, _ := http.NewRequest("POST", "https://pixdoc.dev/api/v1/pdf", bytes.NewBuffer(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()

    out, _ := os.Create("invoice.pdf")
    io.Copy(out, resp.Body)
}

Next Steps