How to Autodetect & Convert a Document to a PNG Image Array using Go
Converting documents into PNG images is a straightforward process with a straightforward goal, not all that dissimilar to converting documents into PDF format. PNG images are viewable by anyone on any operating system and, because they are 2-dimensional raster files, their contents can’t be edited in any non-superficial way. With our Document to PNG Array API, you can convert dozens of common document types (including all major Office document formats & over 100 image formats) into PNG format with zero hassle. All you need to do is follow instructions below to structure your API call using the ready-to-run Go code examples provided below, and register a free account on our website to get your free-tier API key (these are valid for a hard limit of 800 API calls per month with no additional commitments; enterprise accounts are also available for large-scale projects).
To call the API, all you need to do is copy & paste the below code examples into your file, configure your API key & file input headers, and you’re all done:
package main
import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.cloudmersive.com/convert/autodetect/to/png"
method := "POST"
payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
file, errFile1 := os.Open("/path/to/file")
defer file.Close()
part1,
errFile1 := writer.CreateFormFile("inputFile",filepath.Base("/path/to/file"))
_, errFile1 = io.Copy(part1, file)
if errFile1 != nil {
fmt.Println(errFile1)
return
}
err := writer.Close()
if err != nil {
fmt.Println(err)
return
}
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "multipart/form-data")
req.Header.Add("Apikey", "YOUR-API-KEY-HERE")
req.Header.Set("Content-Type", writer.FormDataContentType())
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
That’s all there is to it. No need to worry about converting to PNG anymore!