RenderPDFs/Tutorials/Go
📘 Quickstart · 5 minutes

Generate PDFs in Go

Generate PDFs from HTML, Markdown, URLs or templates in Go — using net/http and the RenderPDFs REST API.

1. Install

RenderPDFs uses a plain REST API — no SDK required. For Go, install the dependency below and grab your API key from renderpdfs.com/signup (free, 100 PDFs/month, no credit card).

Installbash
# net/http and encoding/json are in the Go stdlib — no deps needed
go mod init my-pdf-app

2. Convert HTML to PDF

The simplest case: send an HTML string, get back a PDF binary. Anything Chromium renders works — Flexbox, Grid, web fonts, SVG, JavaScript.

HTML → PDFgo
package main

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

func main() {
	body, _ := json.Marshal(map[string]any{
		"html": "<h1>Hello PDF</h1><p>Generated from Go</p>",
	})

	req, _ := http.NewRequest("POST", "https://api.renderpdfs.com/v1/generate", bytes.NewReader(body))
	req.Header.Set("X-API-Key", os.Getenv("RENDERPDFS_KEY"))
	req.Header.Set("Content-Type", "application/json")

	res, err := http.DefaultClient.Do(req)
	if err != nil { panic(err) }
	defer res.Body.Close()

	f, _ := os.Create("hello.pdf")
	defer f.Close()
	io.Copy(f, res.Body)
}

3. Convert a URL to PDF

Pass url instead of html and RenderPDFs fetches the page, waits for JS, and snapshots it. Useful for archiving dashboards, public pages, or invoices served from your app.

URL → PDFgo
body, _ := json.Marshal(map[string]any{
	"url": "https://example.com",
	"options": map[string]any{
		"format":          "A4",
		"printBackground": true,
	},
})

4. Use a built-in template

Skip the design work. RenderPDFs ships six battle-tested templates — invoice, receipt, report, contract, certificate, offer_letter. Send JSON, get a styled PDF.

Built-in invoice templatego
body, _ := json.Marshal(map[string]any{
	"template": "invoice",
	"data": map[string]any{
		"company":        "Acme Inc.",
		"invoice_number": "INV-001",
		"items": []map[string]any{
			{"description": "Consulting", "quantity": 10, "unit_price": 120},
		},
	},
})

5. Custom paper, margins, headers

Control page format, orientation, margins, and running headers/footers via the options object. All standard Chromium PDF settings are supported.

Custom paper size & marginsgo
body, _ := json.Marshal(map[string]any{
	"html": "<h1>Report</h1>",
	"options": map[string]any{
		"format":    "Letter",
		"landscape": false,
		"margin": map[string]string{
			"top": "20mm", "bottom": "20mm", "left": "15mm", "right": "15mm",
		},
		"printBackground": true,
	},
})

6. Store the PDF and share a link

For emailable links or webhook payloads, set store: true in the body. The response becomes { url, expires_in } — the PDF is hosted on our CDN for 24 hours by default.

Store the PDF and get a URLgo
body, _ := json.Marshal(map[string]any{
	"html":  "<h1>Shared report</h1>",
	"store": true,
})
// ... send request ...
var result struct {
	URL       string `json:"url"`
	ExpiresIn int    `json:"expires_in"`
}
json.NewDecoder(res.Body).Decode(&result)
fmt.Printf("Download at %s (expires in %ds)\n", result.URL, result.ExpiresIn)

7. Notes & gotchas

Authentication

Every request needs an X-API-Key header. Grab a free key at renderpdfs.com/signup — 100 PDFs/month, no credit card. Treat the key like a password: keep it server-side, never expose it in browser code.

Response format

By default the endpoint streams back the raw PDF binary (Content-Type: application/pdf). Set `store: true` in the body and the response becomes { url, expires_in } — useful for emailing links or attaching to webhooks.

Handling errors

Non-2xx responses return JSON: { error: string }. The most common cases are 401 (bad API key), 402 (over plan quota), and 422 (invalid HTML or URL). Always parse the error body before retrying.

Other languages

Generate your first PDF in 60 seconds

100 free PDFs per month. No credit card. HTML, Markdown, URLs, templates, MCP — all included.