PPTX
Export
Generate .pptx files as Buffer, Blob, Base64, string, or stream
Packer converts a Presentation into various output formats.
Overview
import { Packer, Presentation } from "@office-open/pptx";
const pres = new Presentation({
/* ... */
});
| Method | Environment | Returns |
|---|---|---|
Packer.toBuffer(pres) | Node.js | Promise<Buffer> |
Packer.toBlob(pres) | Browser | Promise<Blob> |
Packer.toBase64String(pres) | Both | Promise<string> |
Packer.toString(pres) | Both | Promise<string> |
Packer.toStream(pres) | Node.js | NodeJS.ReadableStream |
toBuffer (Node.js)
Returns a Buffer that can be written to a file:
import { Packer, Presentation } from "@office-open/pptx";
import fs from "node:fs";
const pres = new Presentation({ slides: [] });
const buffer = await Packer.toBuffer(pres);
fs.writeFileSync("output.pptx", buffer);
toBlob (Browser)
Returns a Blob for browser download or upload:
const blob = await Packer.toBlob(pres);
// Download
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "presentation.pptx";
a.click();
URL.revokeObjectURL(url);
toBase64String
Returns a Base64-encoded string:
const base64 = await Packer.toBase64String(pres);
console.log(base64);
// "UEsDBBQAAAAIA..."
Useful for embedding in HTML or sending via API:
// Data URI for embedding
const dataUri = `data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64,${base64}`;
toString
Returns the raw string representation of the file content:
const str = await Packer.toString(pres);
toStream (Node.js)
Returns a readable stream for piping:
import { createWriteStream } from "node:fs";
const stream = Packer.toStream(pres);
stream.pipe(createWriteStream("output.pptx"));
Full Example — Save to File
import { Presentation, Slide, Shape, Paragraph, TextRun, Packer } from "@office-open/pptx";
import fs from "node:fs";
const pres = new Presentation({
title: "Export Demo",
slides: [
new Slide({
children: [
new Shape({
x: 1,
y: 1,
width: 8,
height: 5,
paragraphs: [
new Paragraph({
alignment: "center",
children: [
new TextRun({
text: "Generated Presentation",
fontSize: 36,
bold: true,
}),
],
}),
],
}),
],
}),
],
});
const buffer = await Packer.toBuffer(pres);
fs.writeFileSync("export-demo.pptx", buffer);
console.log("Saved to export-demo.pptx");