Create Powerpoint with JavaScript

dublintech picture dublintech · Aug 27, 2012 · Viewed 10.2k times · Source

JavaScript cannot create files locally on the clients filesystem. However, I am wondering, if it is possible to somehow create a Powerpoint MIME in a web page (a div or a iframe) from some JSON and then let the UserAgent figure out it is Powerpoint in the expectation that the UserAgent will offer the user the choice to display it as a powerpoint presentation?

Note: The idea here is to be able to take some JSON and make a powerpoint presentation without having to make a request to a Server to create a Powerpoint file.

Answer

apsillers picture apsillers · Aug 27, 2012

One JavaScript library that can generate Powerpoint binary files is PptxGenJS.

Genreally speaking, you can create a link with a data URL that has a Powerpoint MIME type:

 data:ms-powerpoint;base64,aGVsbG8gd... // base64-encoded file

Run your logic to create a binary Powerpoint file, then base64-encode it (e.g. with btoa), and then dynamically generate a link or redirect window.location to the data URI.

var binaryPPFile = createPowerpointFromJSON(sourceJSON);
window.location = "data:ms-powerpoint;base64," + btoa(binaryPPFile);

My hypothetical createPowerpointFromJSON function might make calls to the PptxGenJS API, or any other Powerpoint-generating API.