Examples

HTML to PNG with Go and Playwright

Heads up before you start

playwright-go's module path moved from github.com/playwright-community/playwright-go to github.com/mxschmitt/playwright-go in v0.6100.0. Older tutorials use the old path, which no longer receives updates — use the new path in go get and your imports.

Install Playwright for Go and Chromium

go get github.com/mxschmitt/playwright-go
go run github.com/mxschmitt/playwright-go/cmd/playwright install chromium

playwright-go is the community-maintained Go client for Playwright. Run the commands inside a Go module (go mod init myproject first if you need one). The install command downloads the Playwright driver and a Chromium build for it automatically.

Convert HTML to PNG and save to file

package main

import (
	"log"

	"github.com/mxschmitt/playwright-go"
)

func main() {
	pw, err := playwright.Run()
	if err != nil {
		log.Fatal(err)
	}
	defer pw.Stop()

	browser, err := pw.Chromium.Launch()
	if err != nil {
		log.Fatal(err)
	}
	defer browser.Close()

	page, err := browser.NewPage()
	if err != nil {
		log.Fatal(err)
	}

	if err := page.SetContent("<html><body><h1>Hello World</h1></body></html>"); err != nil {
		log.Fatal(err)
	}
	if _, err := page.Screenshot(playwright.PageScreenshotOptions{
		Path: playwright.String("html.png"),
	}); err != nil {
		log.Fatal(err)
	}
}

Find out how to change the size of the viewport with Go and Playwright.

That was 35 lines of code — plus a browser to run in production

Keeping headless browsers fed, patched and scaled is a job in itself. We built Urlbox so the same screenshot is one API call:

curl "https://api.urlbox.com/v1/YOUR_API_KEY/png?url=https://example.com"

Try the Urlbox screenshot API — free trial, no browser babysitting.