html2image

package module
v0.0.0-...-80cdd9a Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2024 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
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.

cp \
  './ultralight-sdk/bin/libAppCore.so' \
  './ultralight-sdk/bin/libUltralightCore.so' \
  './ultralight-sdk/bin/libUltralight.so' \
  './ultralight-sdk/bin/libWebCore.so' \
  './ultralight-sdk/resources/cacert.pem' \
  './ultralight-sdk/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
renderer := html2image.NewRenderer(800, 600, nil)
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

This section is empty.

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 int, height int, logger Logger) *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
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳