How to Convert Rich Text Format (RTF) to a PNG Image Array using Go
Converting any document to PNG image format is the simplest & fastest way to protect that document from unwanted edits by outside viewers; it’s also a great way to ensure people on any platform/operating system can view them without trouble. Thankfully, our Document Conversion API endpoint can help you make dozens of Document-to-PNG conversions at scale with zero hassle. In this article, I’ll demonstrate how you can easily take advantage of our RTF to PNG Array API, which automatically creates one PNG image per page of an input RTF file. To use this API, you’ll just need to get a free-tier Cloudmersive API key (visit our website and register a free account to get one) and follow brief instructions provided below to structure your API call in Golang.
Calling this API is extremely straightforward: all you need to do is copy the below code into your file, configure your file path & API key headers (where indicated in the code comments), and call the function:
package main
import (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.cloudmersive.com/convert/rtf/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))
}