Image to PDF Golang Examples

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

Basic examples

Basic examples

PNG file to PDF file

package main

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

func main() {
    // create the API client instance
    client := pdfcrowd.NewImageToPdfClient("your_username", "your_apikey")

    // run the conversion and write the result to a file
    err := client.ConvertFileToFile("/path/to/logo.png", "logo.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())
    }
}

PNG file to in-memory PDF

package main

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

func main() {
    // create the API client instance
    client := pdfcrowd.NewImageToPdfClient("your_username", "your_apikey")

    // run the conversion and store the result into the "pdf" variable
    pdf, err := client.ConvertFile("/path/to/logo.png")

    // check for the conversion error
    handleError(err)

    // at this point the "pdf" variable contains PDF 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())
    }
}

PNG file to PDF stream

package main

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

func main() {
    // create the API client instance
    client := pdfcrowd.NewImageToPdfClient("your_username", "your_apikey")

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

    // 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/logo.png", 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())
    }
}

PNG url to PDF file

package main

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

func main() {
    // create the API client instance
    client := pdfcrowd.NewImageToPdfClient("your_username", "your_apikey")

    // run the conversion and write the result to a file
    err := client.ConvertUrlToFile("https://pdfcrowd.com/static/images/logo.png", "logo.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())
    }
}

PNG url to in-memory PDF

package main

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

func main() {
    // create the API client instance
    client := pdfcrowd.NewImageToPdfClient("your_username", "your_apikey")

    // run the conversion and store the result into the "pdf" variable
    pdf, err := client.ConvertUrl("https://pdfcrowd.com/static/images/logo.png")

    // check for the conversion error
    handleError(err)

    // at this point the "pdf" variable contains PDF 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())
    }
}

PNG url to PDF stream

package main

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

func main() {
    // create the API client instance
    client := pdfcrowd.NewImageToPdfClient("your_username", "your_apikey")

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

    // 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("https://pdfcrowd.com/static/images/logo.png", 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 PNG to PDF 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 the API client instance
    client := pdfcrowd.NewImageToPdfClient("your_username", "your_apikey")

    // run the conversion and write the result to a file
    err := client.ConvertRawDataToFile(readFile("/path/to/logo.png"), "logo.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())
    }
}

In-memory PNG to in-memory PDF

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 the API client instance
    client := pdfcrowd.NewImageToPdfClient("your_username", "your_apikey")

    // run the conversion and store the result into the "pdf" variable
    pdf, err := client.ConvertRawData(readFile("/path/to/logo.png"))

    // check for the conversion error
    handleError(err)

    // at this point the "pdf" variable contains PDF 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 PNG to PDF 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 the API client instance
    client := pdfcrowd.NewImageToPdfClient("your_username", "your_apikey")

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

    // 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.ConvertRawDataToStream(readFile("/path/to/logo.png"), 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.NewImageToPdfClient("your_username", "your_apikey")

    // configure the conversion
    client.SetDebugLog(true)

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

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