Stack · Go
Generate PDFs in Go
Go's PDF story is either a drawing library like gofpdf — where you position every element by hand — or chromedp, which means running and supervising a Chrome process next to your service. Neither is appealing when what you have is already HTML.
Render your html/template output, POST it with the standard library, and stream the response to a file or straight to the client. One static binary, no cgo, no browser to babysit.
01Call it from Go
package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
)
func renderPDF(html string, out io.Writer) error {
body, err := json.Marshal(map[string]any{
"html": html,
"options": map[string]any{"format": "A4", "pageNumbers": true},
})
if err != nil {
return err
}
req, err := http.NewRequest("POST", "https://rendral.com/api/v1/pdf", bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+os.Getenv("RENDRAL_API_KEY"))
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
_, err = io.Copy(out, res.Body)
return err
}Frequently asked
Why not just use chromedp?
chromedp is powerful, but you own the Chrome lifecycle: installing it, capping concurrency, reaping zombie processes, and containing the memory. That operational surface is what this replaces.
Does this work with html/template?
Yes — execute your template into a bytes.Buffer and send the resulting string. Nothing about your templating changes.
How do I handle concurrency?
The API applies its own per-key rate limit and returns standard X-RateLimit headers, so a worker pool can back off on 429 rather than you sizing a browser pool.
Can I stream the PDF to S3?
Yes. The response body is the PDF, so io.Copy it into an S3 uploader without buffering the whole document in memory.
Related
Packing slips PDF API
Generate packing slips and pick lists as PDFs from HTML.
Compare vs jsPDF
How jsPDF and Rendral differ on pricing, features, and setup.
PDFs in Ruby on Rails
A step-by-step guide to generating PDFs from Ruby on Rails.
A free tier, five endpoints, and SDKs for Node and Python.