🚀 A modern command-line tool similar to wkhtmltopdf but powered by Chromium headless for superior rendering and web standards support.
- HTML to PDF conversion with full Chromium rendering
- HTML to Image conversion (PNG/JPEG) with screenshot capabilities
- URL and file input support
- Custom page sizes and margins (A4, A3, Letter, Legal, etc.)
- Page orientation (portrait/landscape)
- High-quality output with modern web standards
- CLI tool similar to wkhtmltopdf interface
- Go library for programmatic use
- Cross-platform (Windows, macOS, Linux)
- Go 1.23+
- Chromium/Chrome browser installed
git clone https://github.com/chinmay-sawant/gochromedp.git
cd gochromedp
go mod download
go build -o gochromedp ./cmd/gochromedp# Download from releases page
# Or build and install globally
go install github.com/chinmay-sawant/gochromedp/cmd/gochromedp@latestgochromedp [command]Available commands:
pdf- Convert HTML to PDFimage- Convert HTML to imageversion- Print version information
Use gochromedp [command] --help for more information about a command.
# Basic conversion
gochromedp pdf https://example.com output.pdf
# With custom page size and margins
gochromedp pdf --page-size A4 --margin-top 20mm --margin-bottom 20mm https://example.com document.pdf
# Landscape orientation
gochromedp pdf --orientation landscape --page-size A3 https://example.com landscape.pdfgochromedp pdf input.html output.pdf# PNG screenshot (default)
gochromedp image --width 1920 --height 1080 https://example.com screenshot.png
# JPEG with quality setting
gochromedp image --format jpeg --quality 85 --width 1024 --height 768 https://example.com photo.jpggochromedp image input.html screenshot.png--page-size stringPage size (A4, A3, Letter, Legal) (default "A4")--orientation stringPage orientation (portrait/landscape) (default "portrait")--margin-top stringTop margin (default "10mm")--margin-right stringRight margin (default "10mm")--margin-bottom stringBottom margin (default "10mm")--margin-left stringLeft margin (default "10mm")
--no-backgroundDo not print background--grayscaleGenerate grayscale PDF
--format stringImage format (png/jpeg) (default "png")--quality intImage quality (1-100, for JPEG) (default 90)--width intViewport width (default 1024)--height intViewport height (default 768)
package main
import (
"os"
"github.com/chinmay-sawant/gochromedp"
)
func main() {
// Convert HTML to PDF
html := "<html><body><h1>Hello World!</h1></body></html>"
options := &gochromedp.ConvertOptions{
PageSize: "A4",
Orientation: "portrait",
MarginTop: "10mm",
}
pdfData, err := gochromedp.ConvertHTMLToPDF(html, options)
if err != nil {
panic(err)
}
os.WriteFile("output.pdf", pdfData, 0644)
// Convert URL to PDF
pdfData, err = gochromedp.ConvertURLToPDF("https://example.com", options)
if err != nil {
panic(err)
}
os.WriteFile("webpage.pdf", pdfData, 0644)
// Convert HTML to image
imageData, err := gochromedp.ConvertHTMLToImage(html, &gochromedp.ConvertOptions{
Format: "png",
Width: 1024,
Height: 768,
})
if err != nil {
panic(err)
}
os.WriteFile("html-screenshot.png", imageData, 0644)
// Convert URL to image
imageData, err = gochromedp.ConvertURLToImage("https://example.com", &gochromedp.ConvertOptions{
Format: "png",
Width: 1024,
Height: 768,
})
if err != nil {
panic(err)
}
os.WriteFile("screenshot.png", imageData, 0644)
}gochromedp pdf example.html output.pdfgochromedp image --width 1920 --height 1080 --quality 95 https://github.com screenshot.pnggochromedp pdf --page-size Letter --margin-top 25mm --margin-bottom 25mm --margin-left 20mm --margin-right 20mm document.html print.pdf#!/bin/bash
urls=("https://example.com" "https://github.com" "https://golang.org")
for url in "${urls[@]}"; do
filename=$(echo $url | sed 's|https://||; s|/|_|g')
gochromedp pdf "$url" "${filename}.pdf"
donegochromedp uses the Chrome DevTools Protocol (CDP) through the chromedp Go library to control a headless Chromium instance. This provides:
- Modern rendering engine with full CSS and JavaScript support
- Better font rendering and layout accuracy
- Web standards compliance (ES6+, CSS3, etc.)
- Security through sandboxed browser execution
- Performance optimizations for headless operation
| Feature | wkhtmltopdf | gochromedp |
|---|---|---|
| Rendering Engine | Qt WebKit (old) | Chromium (modern) |
| CSS Support | Limited | Full CSS3 |
| JavaScript | Basic ES5 | Full ES6+ |
| Fonts | System fonts only | Web fonts + system |
| Performance | Fast | Slightly slower |
| Maintenance | Unmaintained | Active development |
| Dependencies | Qt libraries | Chrome/Chromium |
Ensure Chrome or Chromium is installed and accessible:
# Linux
sudo apt-get install chromium-browser
# macOS
brew install chromium
# Windows - Download from https://www.chromium.org/Try with different Chrome flags or ensure no other Chrome instances are running.
For large documents, increase memory limits:
gochromedp pdf --memory-pressure-off large-document.html output.pdfMIT License - see LICENSE file for details.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
- chromedp - Chrome DevTools Protocol library
- cobra - CLI framework
- Inspired by wkhtmltopdf