Image to Image in Golang

This page describes how to use our cloud-based API to convert between images formats in Golang. The API is user-friendly and can be integrated into your application with just a few lines of code.

Installation

The Golang API client library provides easy access to the Pdfcrowd API. No third-party libraries are required.

Install the client library from Github
go get github.com/pdfcrowd/pdfcrowd-go

We also offer other installation options.

Authentication

The credentials to access the API are your Pdfcrowd username and the API key.

API Method Reference

Refer to the Image to Image Golang Reference for a description of all API methods.

Code Examples

Here are a few Golang examples to get you started quickly with the API. See more examples.

package main

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

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

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

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

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

func handleError(err error) {
    if err != nil {
        // report the error
        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))
        }

        // rethrow or handle the exception
        panic(err.Error())
    }
}
package main

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

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

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

    // run the conversion and write the result to a file
    err := client.ConvertUrlToFile("https://pdfcrowd.com/static/images/logo.png", "logo.jpg")

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

func handleError(err error) {
    if err != nil {
        // report the error
        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))
        }

        // rethrow or handle the exception
        panic(err.Error())
    }
}
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.NewImageToImageClient("your_username", "your_apikey")

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

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

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

func handleError(err error) {
    if err != nil {
        // report the error
        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))
        }

        // rethrow or handle the exception
        panic(err.Error())
    }
}

Troubleshooting