html2image

package module
v0.0.0-...-4de954c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 9, 2025 License: MIT Imports: 6 Imported by: 0

README

html2image

html2image is a simple Go package that renders the given HTML string into an Image.

It uses Ultralight under the hood to achieve this.

How to use

Note: Ultralight isn't distributed with this library. Please make sure you're familiar with the limitations of their Free License before you proceed.

Dependencies

See Ultralight's prerequisites for more information. If you're on Ubuntu, the following should be enough to get you started.

apt-get install \
  build-essential \
  clang \
  clang-format \
  clang-tidy \
  clang-tools \
  clangd \
  cmake \
  libc++-dev \
  libc++1 \
  libc++abi-dev \
  libc++abi1 \
  libclang-dev \
  libclang1 \
  libglu1-mesa-dev \
  liblldb-dev \
  libllvm-ocaml-dev \
  libomp-dev \
  libomp5 \
  libstdc++-12-dev \
  libx11-dev \
  lld \
  lldb \
  llvm \
  llvm-dev \
  llvm-runtime \
  python3-clang \
  xorg-dev
Downloading the SDK

This library expects the Ultralight SDK to be in the ultralight-sdk folder in this repo. You can find the latest SDK here.

Assuming you have curl and 7-zip installed, the following should work.

curl -o /tmp/sdk.7z -L 'https://ultralight-sdk-dev.sfo2.cdn.digitaloceanspaces.com/ultralight-sdk-min-latest-linux-x64.7z'
7z x /tmp/sdk.7z -o./ultralight-sdk
export ULTRALIGHT_SDK_DIR="$(realpath ./ultralight-sdk)"

https://ul-sdk.nyc3.digitaloceanspaces.com/1.4.0/linux/x64/release/ultralight-free-sdk-1.4.0-linux-x64.7z?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=DO801ZCVN6ZXPWYZ4HTE%2F20250809%2Fnyc3%2Fs3%2Faws4_request&X-Amz-Date=20250809T151335Z&X-Amz-Expires=300&X-Amz-Signature=24f685c5ab0384d3f62fb5a41cddd7e28483027a08025fa41bbfd26de1c910af&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject

Building

If you have github.com/fstanis/html2image as a dependency, you must provide the path to the Ultralight SDK downloaded in the previous steps via the CGO_CXXFLAGS and CGO_LDFLAGS environment variables.

This examples assumes you set ULTRALIGHT_SDK_DIR in the previous step.

cd /path/to/your/project
go mod tidy

CGO_CXXFLAGS="-I$ULTRALIGHT_SDK_DIR/include" CGO_LDFLAGS="-L$ULTRALIGHT_SDK_DIR/bin" go build .
Required runtime files

The following files are required at runtime:

  • cacert.pem
  • icudt67l.dat
  • libAppCore.so
  • libUltralightCore.so
  • libUltralight.so
  • libWebCore.so

These files should be in the subdirectory named ultralight relatitve to your binary. They can be found in the Ultralight SDK.

The following command copies them from their respective locations.

cd /path/to/your/project
mkdir ultralight

cp \
  "$ULTRALIGHT_SDK_DIR/bin/libAppCore.so" \
  "$ULTRALIGHT_SDK_DIR/bin/libUltralightCore.so" \
  "$ULTRALIGHT_SDK_DIR/bin/libUltralight.so" \
  "$ULTRALIGHT_SDK_DIR/bin/libWebCore.so" \
  "$ULTRALIGHT_SDK_DIR/resources/cacert.pem" \
  "$ULTRALIGHT_SDK_DIR/resources/icudt67l.dat" \
  "./ultralight"

Usage

Simple example that renders HTML into a PNG file.

// create a new instance of a renderer with a viewport of 800x600 and scale 2.0
renderer := html2image.NewRenderer(800, 600, 2)
defer renderer.Free()

// render an image
img := renderer.Render("<div style='background-color:red'>Hello world</div>")

// write PNG file
f, _ := os.Create("image.png")
png.Encode(f, img)
f.Close()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetLogger

func SetLogger(logger Logger)

Types

type LogLevel

type LogLevel byte

LogLevel, as reported by the Ultralight library.

const (
	LevelError LogLevel = iota
	LevelWarning
	LevelInfo
)

type Logger

type Logger interface {
	Log(level LogLevel, message string)
}

Logger is an interface that receives log messages sent by the Ultralight library, primarily for debugging during development.

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer represents an HTML engine that can process requests to render HTML to images.

func NewRenderer

func NewRenderer(width, height int, scale float64) *Renderer

NewRenderer creates a new instance of a Renderer with the given viewport width and height and an optional Logger.

func (*Renderer) Free

func (r *Renderer) Free()

Free releases the resources used by the Renderer.

func (*Renderer) Render

func (r *Renderer) Render(html string) image.Image

Render renders the given HTML string to an image.

Directories

Path Synopsis
cmd
html2image command
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL