PDF to Image / Golang Examples

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

Basic examples

Basic examples

PDF file to PNG file

package main

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

func main() {
    // Create an API client instance.
    client := pdfcrowd.NewPdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.SetOutputFormat("png")
    client.SetForceZip(true)

    // Run the conversion and save the result to a file.
    err := client.ConvertFileToFile("/path/to/invoice.pdf", "invoice.zip")

    // 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())
    }
}

PDF file to in-memory PNG

package main

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

func main() {
    // Create an API client instance.
    client := pdfcrowd.NewPdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.SetOutputFormat("png")
    client.SetForceZip(true)

    // Run the conversion and store the result in the `png` variable.
    png, err := client.ConvertFile("/path/to/invoice.pdf")

    // Check for the conversion error.
    handleError(err)

    // at this point the "png" 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())
    }
}

PDF file to PNG stream

package main

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

func main() {
    // Create an API client instance.
    client := pdfcrowd.NewPdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.SetOutputFormat("png")
    client.SetForceZip(true)

    // Create an output stream for the conversion result
    outputStream, err := os.Create("invoice.zip")

    // Check for a file creation error.
    handleError(err)

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

    // run the conversion and write the result to the output stream.
    err = client.ConvertFileToStream("/path/to/invoice.pdf", 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())
    }
}

PDF url to PNG file

package main

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

func main() {
    // Create an API client instance.
    client := pdfcrowd.NewPdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.SetOutputFormat("png")
    client.SetForceZip(true)

    // Run the conversion and save the result to a file.
    err := client.ConvertUrlToFile("https://pdfcrowd.com/static/pdf/apisamples/invoice.pdf", "invoice.zip")

    // 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())
    }
}

PDF url to in-memory PNG

package main

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

func main() {
    // Create an API client instance.
    client := pdfcrowd.NewPdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.SetOutputFormat("png")
    client.SetForceZip(true)

    // Run the conversion and store the result in the `png` variable.
    png, err := client.ConvertUrl("https://pdfcrowd.com/static/pdf/apisamples/invoice.pdf")

    // Check for the conversion error.
    handleError(err)

    // at this point the "png" 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())
    }
}

PDF url to PNG stream

package main

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

func main() {
    // Create an API client instance.
    client := pdfcrowd.NewPdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.SetOutputFormat("png")
    client.SetForceZip(true)

    // Create an output stream for the conversion result
    outputStream, err := os.Create("invoice.zip")

    // Check for a file creation error.
    handleError(err)

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

    // run the conversion and write the result to the output stream.
    err = client.ConvertUrlToStream("https://pdfcrowd.com/static/pdf/apisamples/invoice.pdf", 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())
    }
}

In-memory PDF to PNG file

package main

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

func readFile(fileName string) []byte {
    content, err := ioutil.ReadFile(fileName)
    handleError(err)
    return content
}

func main() {
    // Create an API client instance.
    client := pdfcrowd.NewPdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.SetOutputFormat("png")
    client.SetForceZip(true)

    // Run the conversion and save the result to a file.
    err := client.ConvertRawDataToFile(readFile("/path/to/hello_world.pdf"), "invoice.zip")

    // 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())
    }
}

In-memory PDF to in-memory PNG

package main

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

func readFile(fileName string) []byte {
    content, err := ioutil.ReadFile(fileName)
    handleError(err)
    return content
}

func main() {
    // Create an API client instance.
    client := pdfcrowd.NewPdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.SetOutputFormat("png")
    client.SetForceZip(true)

    // Run the conversion and store the result in the `png` variable.
    png, err := client.ConvertRawData(readFile("/path/to/hello_world.pdf"))

    // Check for the conversion error.
    handleError(err)

    // at this point the "png" 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())
    }
}

In-memory PDF to PNG stream

package main

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

func readFile(fileName string) []byte {
    content, err := ioutil.ReadFile(fileName)
    handleError(err)
    return content
}

func main() {
    // Create an API client instance.
    client := pdfcrowd.NewPdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.SetOutputFormat("png")
    client.SetForceZip(true)

    // Create an output stream for the conversion result
    outputStream, err := os.Create("invoice.zip")

    // Check for a file creation error.
    handleError(err)

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

    // run the conversion and write the result to the output stream.
    err = client.ConvertRawDataToStream(readFile("/path/to/hello_world.pdf"), 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 an API client instance.
    client := pdfcrowd.NewPdfToImageClient("demo", "ce544b6ea52a5621fb9d55f8b542d14d")

    // If unsure whether the input is a single-page file, enforce ZIP output.
    client.SetOutputFormat("png")
    client.SetDebugLog(true)
    client.SetForceZip(true)

    // Run the conversion and save the result to a file.
    err := client.ConvertFileToFile("/path/to/invoice.pdf", "invoice.zip")

    // 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 total number of pages in the output document
    fmt.Println("Page count:", client.GetPageCount())
    
    // 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())
    }
}