Complete Go examples for common HTML to Image tasks. See the Go SDK guide for installation and result handling.
You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed. Each Go example can be copied and run independently.
Choose file, in-memory, or output-stream handling for the PNG result. Stream examples use a file stream as their destination.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetOutputFormat("png") err := client.ConvertUrlToFile("https://example.com/", "example.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
package main import ( "log" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetOutputFormat("png") image, err := client.ConvertUrl("https://example.com/") // []byte checkError(err) fmt.Println(len(image)) } func checkError(err error) { if err != nil { log.Fatal(err) } }
package main import ( "log" "os" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetOutputFormat("png") output_stream, err := os.Create("example.png") checkError(err) err = client.ConvertUrlToStream("https://example.com/", output_stream) closeErr := output_stream.Close() checkError(err) checkError(closeErr) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Upload an existing document.html file. For local images and stylesheets, see Local assets.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetOutputFormat("png") err := client.ConvertFileToFile("document.html", "document.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
package main import ( "log" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetOutputFormat("png") image, err := client.ConvertFile("document.html") // []byte checkError(err) fmt.Println(len(image)) } func checkError(err error) { if err != nil { log.Fatal(err) } }
package main import ( "log" "os" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetOutputFormat("png") output_stream, err := os.Create("document.png") checkError(err) err = client.ConvertFileToStream("document.html", output_stream) closeErr := output_stream.Close() checkError(err) checkError(closeErr) } func checkError(err error) { if err != nil { log.Fatal(err) } }
These examples read HTML text from document.html. In an application, pass the HTML produced by your template renderer instead.
package main import ( "log" "os" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") htmlBytes, err := os.ReadFile("document.html") checkError(err) html := string(htmlBytes) client.SetOutputFormat("png") err = client.ConvertStringToFile(html, "html.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
package main import ( "log" "os" "fmt" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") htmlBytes, err := os.ReadFile("document.html") checkError(err) html := string(htmlBytes) client.SetOutputFormat("png") image, err := client.ConvertString(html) // []byte checkError(err) fmt.Println(len(image)) } func checkError(err error) { if err != nil { log.Fatal(err) } }
package main import ( "log" "os" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") htmlBytes, err := os.ReadFile("document.html") checkError(err) html := string(htmlBytes) client.SetOutputFormat("png") output_stream, err := os.Create("html.png") checkError(err) err = client.ConvertStringToStream(html, output_stream) closeErr := output_stream.Close() checkError(err) checkError(closeErr) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Define a small HTML document directly in your code.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") html := ` <!doctype html> <html><body><h1>Monthly report</h1></body></html>` client.SetOutputFormat("png") err := client.ConvertStringToFile(html, "report.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Open document.html as a binary input stream and save the PNG to document.png.
package main import ( "log" "os" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetOutputFormat("png") input_stream, err := os.Open("document.html") checkError(err) err = client.ConvertStreamToFile(input_stream, "document.png") closeErr := input_stream.Close() checkError(err) checkError(closeErr) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Include HTML and its local images, stylesheets, and scripts in a ZIP archive, keeping their relative paths.
Upload an existing document.zip. Select the main HTML file explicitly when the archive contains multiple documents.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetOutputFormat("png") err := client.ConvertFileToFile("document.zip", "output.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetOutputFormat("png") client.SetZipMainFilename("index.html") err := client.ConvertFileToFile("document.zip", "document.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Create document.zip from the existing document directory, then convert it to PNG.
package main import ( "archive/zip" "log" "os" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") archive, err := os.Create("document.zip") checkError(err) writer := zip.NewWriter(archive) err = writer.AddFS(os.DirFS("document")) zipErr := writer.Close() closeErr := archive.Close() checkError(err) checkError(zipErr) checkError(closeErr) client.SetOutputFormat("png") err = client.ConvertFileToFile("document.zip", "document.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Capture a 1280-pixel-wide viewport and scale the PNG output to 50%.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetOutputFormat("png") client.SetScaleFactor(50) client.SetScreenshotWidth(1280) err := client.ConvertUrlToFile("https://example.com/", "customized.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Replace the selectors with elements from your page.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetCustomCss(` /* Hide navigation, ads, and sidebar */ nav, .advertisement, .sidebar, footer { display: none !important; } /* Adjust layout for screenshot */ .content { width: 100% !important; max-width: none !important; }`) client.SetOutputFormat("png") err := client.ConvertUrlToFile("https://example.com/", "custom_css.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
This example removes widgets and expands sections by changing their CSS classes. Adjust the selectors and class names for your page.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetCustomJavascript(` // Remove dynamic content before conversion document.querySelectorAll('.video-player, .chat-widget').forEach(el => { el.remove(); }); // Expand collapsed sections document.querySelectorAll('.collapsed').forEach(el => { el.classList.remove('collapsed'); });`) client.SetOutputFormat("png") err := client.ConvertUrlToFile("https://example.com/", "custom_js.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Convert only #main in an existing document.html file. Adjust the filename and selector for your document.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetElementToConvert("#main") client.SetOutputFormat("png") err := client.ConvertFileToFile("document.html", "html_part.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Wait for #content-loaded to appear in an existing document.html file. Choose an element that appears when your content is ready.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetOutputFormat("png") client.SetWaitForElement("#content-loaded") err := client.ConvertFileToFile("document.html", "wait_element.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Add a delay of 2000 milliseconds. When the page has a reliable readiness marker, use Wait for a page element.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetJavascriptDelay(2000) client.SetOutputFormat("png") err := client.ConvertUrlToFile("https://example.com/", "with_delay.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Apply the page’s @media print rules. The page must provide print styles for this to change its appearance.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetOutputFormat("png") client.SetUsePrintMedia(true) err := client.ConvertUrlToFile("https://example.com/", "print_media.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }
Add these fragments to HTML you control.
<nav class="pdfcrowd-remove">This won't appear in the screenshot</nav> <div class="pdfcrowd-hide">This is invisible but takes up space</div>
pdfcrowd-remove removes the element from the layout. pdfcrowd-hide preserves its space.
<style> /* These styles only apply during image conversion */ .pdfcrowd-body h1 { font-size: 48px; } .pdfcrowd-body footer { display: none; } .pdfcrowd-body .main-content { width: 100%; } </style>
The .pdfcrowd-body prefix limits these rules to conversion; it does not change the page in a visitor’s browser.
Upload existing template.html and data.json files. See the template rendering guide for template syntax, data mapping, and other supported data formats.
package main import ( "log" "github.com/pdfcrowd/pdfcrowd-go" ) func main() { client := pdfcrowd.NewHtmlToImageClient("demo", "demo") client.SetDataFormat("json") client.SetOutputFormat("png") client.SetDataFile("data.json") err := client.ConvertFileToFile("template.html", "output.png") checkError(err) } func checkError(err error) { if err != nil { log.Fatal(err) } }