https://github.com/rohankarn35/htmlcapture
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.
https://github.com/rohankarn35/htmlcapture
chromedp golang html2image htmlcapture package
Last synced: 6 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/rohankarn35/htmlcapture
- Owner: rohankarn35
- License: mit
- Created: 2025-01-30T21:09:12.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-07T20:00:00.000Z (over 1 year ago)
- Last Synced: 2025-02-07T21:18:20.647Z (over 1 year ago)
- Topics: chromedp, golang, html2image, htmlcapture, package
- Language: Go
- Homepage:
- Size: 46.9 KB
- Stars: 14
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.
---
[](https://pkg.go.dev/github.com/rohankarn35/htmlcapture)
[](https://goreportcard.com/report/github.com/rohankarn35/htmlcapture)
[](https://opensource.org/licenses/MIT)
## 🚀 Installation
To install **htmlcapture**, use:
```sh
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**
```go
package main
import (
"fmt"
"os"
"github.com/rohankarn35/htmlcapture"
)
func main() {
opts := htmlcapture.CaptureOptions{
Input: `
Hello, World!
`,
}
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
Welcome {{.User}}, to {{.Website}}!
```
**Go Code:**
```go
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**
```go
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)**
```go
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.** 😃