This page describes how to use the Pdfcrowd online 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.
Here are Golang examples for quickly getting started with the API.
See more examples.
packagemainimport("os""fmt""github.com/pdfcrowd/pdfcrowd-go")funcmain(){// create the API client instanceclient:=pdfcrowd.NewImageToImageClient("your_username","your_apikey")// configure the conversionclient.SetOutputFormat("jpg")// run the conversion and write the result to a fileerr:=client.ConvertFileToFile("/path/to/logo.png","logo.jpg")// check for the conversion errorhandleError(err)}funchandleError(errerror){iferr!=nil{why,ok:=err.(pdfcrowd.Error)ifok{os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n",why))}else{os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n",err))}panic(err.Error())}}
packagemainimport("os""fmt""github.com/pdfcrowd/pdfcrowd-go")funcmain(){// create the API client instanceclient:=pdfcrowd.NewImageToImageClient("your_username","your_apikey")// configure the conversionclient.SetOutputFormat("jpg")// run the conversion and write the result to a fileerr:=client.ConvertUrlToFile("https://pdfcrowd.com/static/images/logo.png","logo.jpg")// check for the conversion errorhandleError(err)}funchandleError(errerror){iferr!=nil{why,ok:=err.(pdfcrowd.Error)ifok{os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n",why))}else{os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n",err))}panic(err.Error())}}
packagemainimport("os""fmt""github.com/pdfcrowd/pdfcrowd-go""io/ioutil")funcreadFile(fileNamestring)[]byte{content,err:=ioutil.ReadFile(fileName)handleError(err)returncontent}funcmain(){// create the API client instanceclient:=pdfcrowd.NewImageToImageClient("your_username","your_apikey")// configure the conversionclient.SetOutputFormat("jpg")// run the conversion and write the result to a fileerr:=client.ConvertRawDataToFile(readFile("/path/to/logo.png"),"logo.jpg")// check for the conversion errorhandleError(err)}funchandleError(errerror){iferr!=nil{why,ok:=err.(pdfcrowd.Error)ifok{os.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n",why))}else{os.Stderr.WriteString(fmt.Sprintf("Generic Error: %s\n",err))}panic(err.Error())}}
Authentication
The credentials to access the API are your Pdfcrowd username and the API key.
Error Handling
It is recommended that you implement error handling to catch errors
that the API may return, see the example code below. A list of status
codes and their description can be found
here.
// call the API // print the erroros.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error: %s\n",why))// print the error codeos.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error Code: %v\n",why.getCode()))// print the error messageos.Stderr.WriteString(fmt.Sprintf("Pdfcrowd Error Message: %v\n",why.getMessage()))