How to Convert CSV to PDF using Go
While CSV and PDF formats are both compatible with every operating system you can think of, PDFs provide the distinct advantage of preventing undesired edits from taking place within your document when you send that file downstream to external networks. With a new PDF file in hand, you can set passwords & various other permissions to ensure that the integrity of your CSV file’s contents is protected. Our CSV to PDF conversion API makes that format transition both easy & economical: all you need to do is copy & paste the Go code examples provided below to structure your API call, and then get a free-tier API key to authenticate the service by registering a free account on our website (free-tier accounts come with a limit of 800 API calls per month & zero additional commitments).
Calling this API is a simple, one-step process. Simply copy the examples below into your file, configure your file path & API key inputs (as indicated by the code comments), and you’re all set:
package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)func main() {url := "https://api.cloudmersive.com/convert/csv/to/pdf"
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))
}