HTML to Image Golang Examples

This page contains various examples of using the HTML to Image API in Golang. The examples are complete and fully functional. Read more about how to convert HTML to Image in Golang.

Basic examples
Template rendering examples

Basic examples

Webpage to PNG file

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")

    // run the conversion and write the result to a file
    err := client.ConvertUrlToFile("http://www.example.com", "example.png")

    // check for the conversion error
    handleError(err)
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}

Webpage to in-memory PNG

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")

    // run the conversion and store the result into the "image" variable
    image, err := client.ConvertUrl("http://www.example.com")

    // check for the conversion error
    handleError(err)

    // at this point the "image" variable contains PNG raw data and
    // can be sent in an HTTP response, saved to a file, etc.
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}

Webpage to PNG stream

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")

    // create an output stream for the conversion result
    outputStream, err := os.Create("example.png")

    // check for a file creation error
    handleError(err)

    // close the output stream
    defer outputStream.Close()

    // run the conversion and write the result into the output stream
    err = client.ConvertUrlToStream("http://www.example.com", outputStream)

    // check for the conversion error
    handleError(err)
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}

HTML file to PNG file

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")

    // run the conversion and write the result to a file
    err := client.ConvertFileToFile("/path/to/MyLayout.html", "MyLayout.png")

    // check for the conversion error
    handleError(err)
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}

HTML file to in-memory PNG

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")

    // run the conversion and store the result into the "image" variable
    image, err := client.ConvertFile("/path/to/MyLayout.html")

    // check for the conversion error
    handleError(err)

    // at this point the "image" variable contains PNG raw data and
    // can be sent in an HTTP response, saved to a file, etc.
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}

HTML file to PNG stream

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")

    // create an output stream for the conversion result
    outputStream, err := os.Create("MyLayout.png")

    // check for a file creation error
    handleError(err)

    // close the output stream
    defer outputStream.Close()

    // run the conversion and write the result into the output stream
    err = client.ConvertFileToStream("/path/to/MyLayout.html", outputStream)

    // check for the conversion error
    handleError(err)
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}

HTML string to PNG file

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")

    // run the conversion and write the result to a file
    err := client.ConvertStringToFile("<html><body><h1>Hello World!</h1></body></html>", "HelloWorld.png")

    // check for the conversion error
    handleError(err)
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}

HTML string to in-memory PNG

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")

    // run the conversion and store the result into the "image" variable
    image, err := client.ConvertString("<html><body><h1>Hello World!</h1></body></html>")

    // check for the conversion error
    handleError(err)

    // at this point the "image" variable contains PNG raw data and
    // can be sent in an HTTP response, saved to a file, etc.
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}

HTML string to PNG stream

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")

    // create an output stream for the conversion result
    outputStream, err := os.Create("HelloWorld.png")

    // check for a file creation error
    handleError(err)

    // close the output stream
    defer outputStream.Close()

    // run the conversion and write the result into the output stream
    err = client.ConvertStringToStream("<html><body><h1>Hello World!</h1></body></html>", outputStream)

    // check for the conversion error
    handleError(err)
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}

Get info about the current conversion

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")
    client.SetDebugLog(true)

    // run the conversion and write the result to a file
    err := client.ConvertFileToFile("/path/to/MyLayout.html", "MyLayout.png")

    // check for the conversion error
    handleError(err)
    
    // print URL of the debug log
    fmt.Println("Debug log url:", client.GetDebugLogUrl())
    
    // print the number of conversion credits remaining in your account
    fmt.Println("Remaining credit count:", client.GetRemainingCreditCount())
    
    // print the number of credits used for the conversion
    fmt.Println("Consumed credit count:", client.GetConsumedCreditCount())
    
    // print the unique identifier for the conversion
    fmt.Println("Job id:", client.GetJobId())
    
    // print size of the output data in bytes
    fmt.Println("Output size:", client.GetOutputSize())
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}

Template rendering examples

Create Image from JSON data

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")
    client.SetDataString(`{
            "name": "World",
            "product": "Pdfcrowd API"
        }`)

    // run the conversion and write the result to a file
    err := client.ConvertStringToFile("Hello {{ name }} from {{ product }}", "output.pdf")

    // check for the conversion error
    handleError(err)
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}

Create Image from XML data

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")
    client.SetDataString(`<?xml version="1.0" encoding="UTF-8"?>
        <data>
          <name>World</name>
          <product>Pdfcrowd API</product>
        </data>`)

    // run the conversion and write the result to a file
    err := client.ConvertStringToFile("Hello {{ data.name }} from {{ data.product }}", "output.pdf")

    // check for the conversion error
    handleError(err)
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}

Create Image from YAML data

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")
    client.SetDataString(`name: World
product: Pdfcrowd API`)

    // run the conversion and write the result to a file
    err := client.ConvertStringToFile("Hello {{ name }} from {{ product }}", "output.pdf")

    // check for the conversion error
    handleError(err)
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}

Create Image from CSV data

package main

import (
    "os"
    "fmt"
    "github.com/pdfcrowd/pdfcrowd-go"
)

func main() {
    // create the API client instance
    client := pdfcrowd.NewHtmlToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // configure the conversion
    client.SetOutputFormat("png")
    client.SetDataString(`name,product
World,Pdfcrowd API`)

    // run the conversion and write the result to a file
    err := client.ConvertStringToFile("Hello {{ name }} from {{ product }}", "output.pdf")

    // check for the conversion error
    handleError(err)
}

func handleError(err error) {
    if err != nil {
        why, ok := err.(pdfcrowd.Error)
        if ok {
            os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n", why))
        } else {
            os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n", err))
        }

        panic(err.Error())
    }
}