How to Easily Convert Documents to PNG Arrays in Go

Cloudmersive
3 min readJun 3, 2024

--

We can look at PNG arrays as a slightly simpler & more efficient alternative to PDF documents.

When we convert files to PNG, we’re only storing the document image (i.e., screenshot) in our PNG file, rather than the image + the raster data like we do in a PDF. Thus, we end up with a smaller file size AND an equally high-quality image — not to mention we gain many of the same document security benefits (nobody can change the contents of our PNG documents).

If we’re looking to incorporate useful file conversion actions into our Go applications, we can easily generate PNG arrays using the code examples provided below.

More specifically, we’ll use this code to call a free API that automatically detects a huge variety of input file formats (including all major Office formats, 100+ image formats, and even HTML files) and returns an array of PNG URLs (one per page) for the input document.

This is designed to make our lives as developers much, much easier if we’re converting more than one file type to PNG on a regular basis. To authorize our API calls, we’ll just need a free Cloudmersive API key, which will allow us to make a limit of 800 API calls per month with zero commitments.

To structure our API call, we can begin by importing our packages:

package main

import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)

Next, we can use the below examples to call our function. Our function call performs a POST request to the api.cloudmersive.com public cloud endpoint, which quickly handles our autodetection & PNG conversion action before returning each page of our PNG result as URLs for download:

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

It’s worth pointing out that error handling in the above function simply entails printing errors to the console. If we want to incorporate more robust error handling in our application, we can return errors to the caller, log them for debugging, or even send them up our call stack.

To better understand how our response from this conversion action is structured, we can review the below example JSON response model:

{
"Successful": true,
"PngResultPages": [
{
"PageNumber": 0,
"URL": "string"
}
]
}

And that’s all there is to it — now we can easily convert multiple document types to PNG image arrays with a quick API call in Go.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.