htmltoimage-server

command module
v0.0.0-...-558f2c6 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2022 License: ISC Imports: 1 Imported by: 0

README

HTML to Image Server

Self-hosted HTML to image service powered by Golang RPC server for your web-applications.

Installation

Create the docker-compose.yml file your directory.

version: '3.8'
services:
  app:
    container_name: htmltoimage_server_app
    image: bartmika/htmltoimage-server:latest
    stdin_open: true
    environment:
        HTMLTOIMAGE_SERVER_IP: 0.0.0.0
        HTMLTOIMAGE_SERVER_PORT: 8002
        HTMLTOIMAGE_SERVER_CHROME_HEADLESS_WS_URL: ws://browserless:9090
    restart: unless-stopped
    ports:
      - "8002:8002"
    depends_on:
      - browserless
    links:
      - browserless

  browserless:
    image: browserless/chrome:latest
    environment:
      - DEBUG=browserless:*
      - MAX_CONCURRENT_SESSIONS=10
      - CONNECTION_TIMEOUT=60000
      - MAX_QUEUE_LENGTH=20
      - PREBOOT_CHROME=true
      - DEMO_MODE=false
      - HOST=0.0.0.0
      - ENABLE_DEBUGGER=false
      - PORT=9090
      - WORKSPACE_DELETE_EXPIRED=true
    container_name: "htmltoimage_server_browserless"
    restart: unless-stopped

Start the service.

$ docker compose up -d

Usage

Here is a sample program of how to utilize the service.

package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"time"

	"github.com/bartmika/htmltoimage-server/pkg/rpc"
)

// NOTE:
// Before you begin running this code, make sure the HTML to Image server is
// running before you execute this program.

func main() {
	// Generate the HTML to Image server based on your `docker-compose.yml`.
	serverIP := "127.0.0.1"
	serverPort := "8002"
	applicationAddress := fmt.Sprintf("%s:%s", serverIP, serverPort)

	// Connect to a running client.
	client, err := rpc.NewClient(applicationAddress, 3, 15*time.Second)
	if err != nil {
		log.Fatal(err)
	}

    // Execute the remote call.
    imgBin, err := client.Screenshot("https://bartlomiejmika.com")
    if err != nil {
        log.Fatal("Sample command failed generating image with error:", err)
    }

    // Save our file.
    if err := ioutil.WriteFile("data/screenshot.png", imgBin, 0o644); err != nil {
        log.Fatal("Sample command failed writing file with error:", err)
    }
    log.Println("Saved file: screenshot.png")
}

Finally if you look in your data folder, you'll see the screenshot.png image downloaded!

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
internal
app
pkg
rpc

Jump to

Keyboard shortcuts

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