HTML to PDF in Go with the Rendlet API

Last updated: . Every code sample on this page was run against the live API with the sandbox key on that date.

Short answer: http.Post (or http.NewRequest) to https://rendlet.com/v1/pdf with JSON {"html": "..."} and an Authorization: Bearer header; the response body is the PDF. The standard library is enough. Rendlet runs Chromium for you, so your Go binary stays small and your containers don't need a browser.

1. Try it with curl

The public sandbox key needs no signup (5 renders a day per network, 1 page, up to 50 KB, faint watermark).

# Works as-is with the public sandbox key. With your own key: export RENDLET_API_KEY=YOUR_API_KEY
curl -X POST https://rendlet.com/v1/pdf \
  -H "Authorization: Bearer ${RENDLET_API_KEY:-sandbox}" \
  -H "Content-Type: application/json" \
  -d '{"html":"<h1>Hello</h1><p>Rendered by Rendlet.</p>","options":{"format":"A4","margin":"15mm"}}' \
  -o hello.pdf

2. Go: an htmlToPDF() helper

Save as render.go and run go run render.go. Output when we ran the same HTML via the live sandbox: saved order.pdf (1 page, 12823 bytes); 4 renders left (byte size varies slightly with Chromium).

// Go 1.21+. Save as render.go and run: go run render.go
// Uses the public sandbox key unless you set RENDLET_API_KEY=YOUR_API_KEY
package main

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

type options struct {
	Format string `json:"format"`
	Margin string `json:"margin"`
}

type requestBody struct {
	HTML    string  `json:"html"`
	Options options `json:"options"`
}

func htmlToPDF(html string) (pdf []byte, pages, remaining string, err error) {
	key := os.Getenv("RENDLET_API_KEY")
	if key == "" {
		key = "sandbox"
	}
	body, err := json.Marshal(requestBody{
		HTML:    html,
		Options: options{Format: "A4", Margin: "15mm"},
	})
	if err != nil {
		return nil, "", "", err
	}
	req, err := http.NewRequest(http.MethodPost, "https://rendlet.com/v1/pdf", bytes.NewReader(body))
	if err != nil {
		return nil, "", "", err
	}
	req.Header.Set("Authorization", "Bearer "+key)
	req.Header.Set("Content-Type", "application/json")
	client := &http.Client{Timeout: 60 * time.Second}
	res, err := client.Do(req)
	if err != nil {
		return nil, "", "", err
	}
	defer res.Body.Close()
	pdf, err = io.ReadAll(res.Body)
	if err != nil {
		return nil, "", "", err
	}
	if res.StatusCode < 200 || res.StatusCode >= 300 {
		// Errors are JSON, e.g. {"error":"quota_exceeded", ...}
		return nil, "", "", fmt.Errorf("Rendlet %d: %s", res.StatusCode, pdf)
	}
	return pdf, res.Header.Get("X-Pages"), res.Header.Get("X-Quota-Remaining"), nil
}

func main() {
	html := `<!doctype html>
<html><body style="font-family:sans-serif;padding:24px">
  <h1 style="color:#1f4e79">Order A-1042</h1>
  <p>Customer: Globex Corp</p>
  <p><b>Total: $2,035.00</b></p>
</body></html>`
	pdf, pages, remaining, err := htmlToPDF(html)
	if err != nil {
		panic(err)
	}
	if err := os.WriteFile("order.pdf", pdf, 0o644); err != nil {
		panic(err)
	}
	fmt.Printf("saved order.pdf (%s page, %d bytes); %s renders left\n", pages, len(pdf), remaining)
}

Escape or template user-supplied values before putting them into HTML (e.g. html/template), then pass the string to htmlToPDF(). Keep the API key in the process environment on the server.

Serving the PDF from a Go HTTP handler

Write the bytes with Content-Type: application/pdf, or put them in object storage. For serverless or agent callers that prefer JSON, set "response":"base64" in the request body and decode the base64 field.

3. Options

FieldValues
options.formatA4, Letter, Legal, A3, A5
options.landscapetrue / false
options.marginCSS length, e.g. "15mm" or "0"
options.printBackgroundtrue / false
filenameName used in the Content-Disposition header
response"binary" (default) or "base64": JSON {filename, pages, bytes, content_type, base64}

For HTML input, JavaScript in the page is not run, and only data: URIs and public https:// assets (images, fonts, CSS) load. Inline your CSS or link it over https. Simplified Chinese works out of the box (Noto Sans SC is bundled).

Errors and response headers

Successful renders return X-Pages, X-Plan, X-Quota-Limit and X-Quota-Remaining. Errors are JSON like {"error":"...","message":"..."}: 400 bad request, 401 missing key, 403 invalid key, 413 HTML too large or too many pages, 429 quota exceeded, 500 render failed.

Limits to know

SandboxFreeStarterProScale
Price$0$0$7/mo$19/mo$59/mo
Documents5 / day / network100 / month1,000 / month5,000 / month25,000 / month
Requests per minute3103060120
Pages per document11050200500
HTML size50 KB1 MB2 MB2 MB2 MB

Every plan has a hard monthly cap: at the cap the API returns 429 with an upgrade_url, and you are never billed for overage. Failed renders don't count. Source: docs.

Try it on your own HTML. The sandbox renders a real PDF with no signup (5 a day per network, 1 page, watermarked). A free key gives 100 documents a month, up to 10 pages each, with no watermark.

Open the sandbox →   Get a free key · Pricing

Related