Skip to content

htmlcapture – A Go package to capture high-quality screenshots from URLs, HTML files, or raw HTML strings. Supports dynamic HTML, CSS selectors, and Instagram-optimized sizes.

License

Notifications You must be signed in to change notification settings

rohankarn35/htmlcapture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

htmlcapture

📸 Overview

htmlcapture is a Go package that captures high-quality screenshots from URLs, HTML files, or raw HTML strings. It is optimized for Instagram post sizes and supports dynamic HTML rendering.


GoDoc Go Report Card License: MIT

🚀 Installation

To install htmlcapture, use:

go get github.com/rohankarn35/htmlcapture@latest

📌 Features

  • ✅ Capture from URL, HTML file, or raw HTML string
  • ✅ Supports dynamic HTML rendering with external variables
  • ✅ Capture specific elements using CSS selectors
  • ✅ Default Instagram-optimized screenshot sizes
  • ✅ Uses headless Chrome (chromedp) for rendering

🛠 Usage

1️⃣ Basic Usage: Capture a Raw HTML String

package main

import (
    "fmt"
    "os"
    "github.com/rohankarn35/htmlcapture"
)

func main() {
    opts := htmlcapture.CaptureOptions{
        Input: `<html><body><h1>Hello, World!</h1></body></html>`,
    }
    img, err := htmlcapture.Capture(opts)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    os.WriteFile("screenshot.png", img, 0644)
}

2️⃣ Capture an HTML File (With Dynamic Variables)

Example: template.html

<html>
  <body>
    <h1>Welcome {{.User}}, to {{.Website}}!</h1>
  </body>
</html>

Go Code:

package main

import (
    "github.com/rohankarn35/htmlcapture"
)

func main() {
    opts := htmlcapture.CaptureOptions{
        Input: "template.html",
        Variables: map[string]string{
            "User": "Alice",
            "Website": "Wonderland",
        },
    }
    img, err := htmlcapture.Capture(opts)
    if err != nil {
        log.Fatalf("Error capturing screenshot: %v", err)
    }
    os.WriteFile("screenshot.png", img, 0644)
}

3️⃣ Capture a Website URL

package main

import (
    "github.com/rohankarn35/htmlcapture"
)

func main() {
    opts := htmlcapture.CaptureOptions{
        Input: "https://example.com",
        ViewportW: 1920,
        ViewportH: 1080,
    }
    img, err := htmlcapture.Capture(opts)
    if err != nil {
        log.Fatalf("Error capturing screenshot: %v", err)
    }
    os.WriteFile("screenshot.png", img, 0644)
}

4️⃣ Capture a Specific Element (CSS Selector)

package main

import (
    "github.com/rohankarn35/htmlcapture"
)

func main() {
    opts := htmlcapture.CaptureOptions{
        Input: "https://example.com",
        Selector: "#main-content",
    }
    img, err := htmlcapture.Capture(opts)
    if err != nil {
        log.Fatalf("Error capturing screenshot: %v", err)
    }
    os.WriteFile("screenshot.png", img, 0644)
}

📝 Configuration Options

Option Type Default Description
Input string Required URL, HTML string, or file path
ViewportW int 1080 Viewport width
ViewportH int 1350 Viewport height
Selector string "" CSS selector for capturing a specific element
Variables map[string]string {} Key-value pairs for dynamic HTML

📖 License

MIT License


💡 Future Enhancements

  • ✅ Add PDF output support 📄
  • ✅ Improve performance optimizations 🚀
  • ✅ Additional image quality controls 🎨

Contributions are welcome! Feel free to open issues or submit pull requests. 😃

About

htmlcapture – A Go package to capture high-quality screenshots from URLs, HTML files, or raw HTML strings. Supports dynamic HTML, CSS selectors, and Instagram-optimized sizes.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages