How to Convert a PowerPoint PPTX Presentation to PDF using Go

Cloudmersive
2 min readNov 3, 2022

--

When it comes to creating big, exciting presentations with animations and other complex features, PowerPoint is still a go-to application for individuals and businesses alike. However, PowerPoint’s deficiencies start to show up when we need to share those bulky files over networks with our peers. PowerPoint PPTX files can be quite large, and moreover, it’s not a given that our peers will have a PowerPoint application to launch those files in. By transitioning PPTX to PDF format, we can reduce the file size while retaining high quality presentation material that can be viewed on any operating system.

With our PPTX to PDF API, you can take control of this common-place file conversion and build it into your applications directly. All you’ll need to do is register a free account on our website (this will provide a free-tier API key with a limit of 800 API calls per month) and follow brief instructions below to structure your API call using Go code examples (these are complementary; provided from our API console page).

To structure our API call, all we need to do is copy & paste the code examples below into our file, and then include our API key and file inputs in their respective fields (indicated by the code comments):

package mainimport (
"fmt"
"bytes"
"mime/multipart"
"os"
"path/filepath"
"io"
"net/http"
"io/ioutil"
)
func main() {url := "https://api.cloudmersive.com/convert/pptx/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))
}

And that’s all there is to it, folks — nice and easy. If you’re looking for different code examples, visit our API Console page and you’ll find comparable code examples available in 10+ additional programming languages there (including C#, Java, Python, Node.js, etc.).

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet