Quickly and easily create PowerPoint presentations with a few simple JavaScript commands in client web browsers or Node desktop apps.
- Widely Supported: Creates and downloads presentations on all current web browsers (Chrome, Edge, Firefox, etc.) and IE11
- Full Featured: Slides can include Tables, Shapes, Images, Text and Media - plus use Master Slides
- Easy To Use: Entire PowerPoint presentations can be created in a few lines of code
- Modern: Pure JavaScript solution - everything necessary to create PowerPoint PPT exports is included
- Use the unique Table-to-Slides feature to copy an HTML table into 1 or more Slides with a single command
Table of Contents (generated with DocToc)
- Live Demo
- Installation
- Presentations: Usage and Options
- Presentations: Adding Objects
- Master Slides and Corporate Branding
- Table-to-Slides Feature
- Full PowerPoint Shape Library
- Performance Considerations
- Issues / Suggestions
- Need Help?
- Development Roadmap
- Unimplemented Features
- Special Thanks
- Support Us
- License
Use JavaScript to Create a PowerPoint presentation with your web browser right now:
https://gitbrent.github.io/PptxGenJS
PptxGenJS requires only 2 additional JavaScript libraries:
<script lang="javascript" src="PptxGenJS/libs/jquery.min.js"></script>
<script lang="javascript" src="PptxGenJS/libs/jszip.min.js"></script>
<script lang="javascript" src="PptxGenJS/dist/pptxgen.js"></script>
npm install pptxgenjs
var pptx = require("pptxgenjs");
PptxGenJS PowerPoint presentations are created via JavaScript by following 4 basic steps:
- Create a new Presentation
- Add a Slide
- Add one or more objects (Tables, Shapes, Images, Text and Media) to the Slide
- Save the Presentation
var pptx = new PptxGenJS();
var slide = pptx.addNewSlide();
slide.addText('Hello World!', { x:1.5, y:1.5, font_size:18, color:'363636' });
pptx.save('Sample Presentation');
That's really all there is to it!
A Presentation is a single .pptx
file. When creating more than one Presentation, declare the pptx again to
start with a new, empty Presentation.
Client Browser:
var pptx = new PptxGenJS();
Node.js:
var pptx = require("pptxgenjs");
There are several optional properties that can be set:
pptx.setAuthor('Brent Ely');
pptx.setCompany('S.T.A.R. Laboratories');
pptx.setRevision('15');
pptx.setSubject('Annual Report');
pptx.setTitle('PptxGenJS Sample Presentation');
Setting the Layout (applies to all Slides in the Presentation):
pptx.setLayout('LAYOUT_WIDE');
Layout Name | Default | Layout Slide Size |
---|---|---|
LAYOUT_16x9 |
Yes | 10 x 5.625 inches |
LAYOUT_16x10 |
No | 10 x 6.25 inches |
LAYOUT_4x3 |
No | 10 x 7.5 inches |
LAYOUT_WIDE |
No | 13.3 x 7.5 inches |
LAYOUT_USER |
No | user defined - see below (inches) |
Custom user defined Layout sizes are supported - just supply a name
and the size in inches.
- Defining a new Layout using an object will also set this new size as the current Presentation Layout
// Defines and sets this new layout for the Presentation
pptx.setLayout({ name:'A3', width:16.5, height:11.7 });
Syntax:
var slide = pptx.addNewSlide();
slide.bkgd = 'F1F1F1';
slide.color = '696969';
Option | Type | Unit | Default | Description | Possible Values |
---|---|---|---|---|---|
bkgd |
string | FFFFFF |
background color | hex color code. | |
color |
string | 000000 |
default text color | hex color code. |
// Create a new Slide that will inherit properties from a pre-defined master page (margins, logos, text, background, etc.)
var slide1 = pptx.addNewSlide( pptx.masters.TITLE_SLIDE );
// The background color can be overridden on a per-slide basis:
var slide2 = pptx.addNewSlide( pptx.masters.TITLE_SLIDE, {bkgd:'FFFCCC'} );
slide.slideNumber({ x:1.0, y:'90%' });
// Slide Numbers can be styled:
slide.slideNumber({ x:1.0, y:'90%', fontFace:'Courier', fontSize:32, color:'CF0101' });
Option | Type | Unit | Default | Description | Possible Values |
---|---|---|---|---|---|
x |
number | inches | 0.3 |
horizontal location | 0-n OR 'n%'. (Ex: {x:'10%'} places number 10% from left edge) |
y |
number | inches | 90% |
vertical location | 0-n OR 'n%'. (Ex: {y:'90%'} places number 90% down the Slide) |
color |
string | text color | hex color code. Ex: {color:'0088CC'} |
||
fontFace |
string | font face | any available font. Ex: {fontFace:Arial} |
||
fontSize |
number | points | font size | 8-256. Ex: {fontSize:12} |
The Slide object returns a reference to itself, so calls can be chained.
Example:
slide
.addImage({ path:'images/logo1.png', x:1, y:2, w:3, h:3 })
.addImage({ path:'images/logo2.jpg', x:5, y:3, w:3, h:3 })
.addImage({ path:'images/logo3.png', x:9, y:4, w:3, h:3 });
Presentations require nothing more than passing a filename to save()
. Node.js users have more options available
examples of which can be found below.
- Simply provide a filename
pptx.save('Demo-Media');
- Node can accept a callback function that will return the filename once the save is complete
// A: File will be saved to the local working directory (`__dirname`)
pptx.save( 'Node_Demo' );
// B: Inline callback function
pptx.save( 'Node_Demo', function(filename){ console.log('Created: '+filename); } );
// C: Predefined callback function
pptx.save( 'Node_Demo', saveCallback );
Objects on the Slide are ordered from back-to-front based upon the order they were added.
For example, if you add an Image, then a Shape, then a Textbox: the Textbox will be in front of the Shape, which is in front of the Image.
// Syntax
slide.addText('TEXT', {OPTIONS});
slide.addText('Line 1\nLine 2', {OPTIONS});
slide.addText([ {text:'TEXT', options:{OPTIONS}} ]);
Option | Type | Unit | Default | Description | Possible Values |
---|---|---|---|---|---|
x |
number | inches | 1.0 |
horizontal location | 0-n OR 'n%'. (Ex: {x:'50%'} will place object in the middle of the Slide) |
y |
number | inches | 1.0 |
vertical location | 0-n OR 'n%'. |
w |
number | inches | width | 0-n OR 'n%'. (Ex: {w:'50%'} will make object 50% width of the Slide) |
|
h |
number | inches | height | 0-n OR 'n%'. | |
align |
string | left |
alignment | left or center or right |
|
autoFit |
boolean | false |
"Fit to Shape" | true or false |
|
bold |
boolean | false |
bold text | true or false |
|
breakLine |
boolean | false |
appends a line break | true or false (only applies when used in text object options) Ex: {text:'hi', options:{breakLine:true}} |
|
bullet |
boolean | false |
bulleted text | true or false |
|
bullet |
object | bullet options (number type or choose any unicode char) | object with type or code . Ex: bullet:{type:'number'} . Ex: bullet:{code:'2605'} |
||
color |
string | text color | hex color code. Ex: { color:'0088CC' } |
||
fill |
string | fill/bkgd color | hex color code. Ex: { color:'0088CC' } |
||
font_face |
string | font face | Ex: 'Arial' | ||
font_size |
number | points | font size | 1-256. Ex: { font_size:12 } |
|
inset |
number | inches | inset/padding | 1-256. Ex: { inset:1.25 } |
|
isTextBox |
boolean | false |
PPT "Textbox" | true or false |
|
italic |
boolean | false |
italic text | true or false |
|
margin |
number | points | margin | 0-99 (ProTip: use the same value from CSS padding ) |
|
shadow |
object | text shadow options | see options below. Ex: shadow:{ type:'outer' } |
||
underline |
boolean | false |
underline text | true or false |
|
valign |
string | vertical alignment | top or middle or bottom |
Option | Type | Unit | Default | Description | Possible Values |
---|---|---|---|---|---|
type |
string | outer | shadow type | outer or inner |
|
angle |
number | degrees | shadow angle | 0-359. Ex: { angle:180 } |
|
blur |
number | points | blur size | 1-256. Ex: { blur:3 } |
|
offset |
number | points | offset size | 1-256. Ex: { offset:8 } |
|
color |
string | text color | hex color code. Ex: { color:'0088CC' } |
||
opacity |
number | percent | opacity | 0-1. Ex: opacity:0.75 |
var pptx = new PptxGenJS();
var slide = pptx.addNewSlide();
// EX: Dynamic location using percentages
slide.addText('^ (50%/50%)', {x:'50%', y:'50%'});
// EX: Basic formatting
slide.addText('Hello', { x:0.5, y:0.7, w:3, color:'0000FF', font_size:64 });
slide.addText('World!', { x:2.7, y:1.0, w:5, color:'DDDD00', font_size:90 });
// EX: More formatting options
slide.addText(
'Arial, 32pt, green, bold, underline, 0 inset',
{ x:0.5, y:5.0, w:'90%', margin:0.5, font_face:'Arial', font_size:32, color:'00CC00', bold:true, underline:true, isTextBox:true }
);
// EX: Format some text
slide.addText('Hello World!', { x:2, y:4, font_face:'Arial', font_size:42, color:'00CC00', bold:true, italic:true, underline:true } );
// EX: Multiline Text / Line Breaks - use either "\r" or "\n"
slide.addText('Line 1\nLine 2\nLine 3', { x:2, y:3, color:'DDDD00', font_size:90 });
// EX: Format individual words or lines by passing an array of text objects with `text` and `options`
slide.addText(
[
{ text:'word-level', options:{ font_size:36, color:'99ABCC', align:'r', breakLine:true } },
{ text:'formatting', options:{ font_size:48, color:'FFFF00', align:'c' } }
],
{ x:0.5, y:4.1, w:8.5, h:2.0, fill:'F1F1F1' }
);
// EX: Bullets
slide.addText('Regular, black circle bullet', { x:8.0, y:1.4, w:'30%', h:0.5, bullet:true });
// Use line-break character to bullet multiple lines
slide.addText('Line 1\nLine 2\nLine 3', { x:8.0, y:2.4, w:'30%', h:1, fill:'F2F2F2', bullet:{type:'number'} });
// Bullets can also be applied on a per-line level
slide.addText(
[
{ text:'I have a star bullet' , options:{bullet:{code:'2605'}, color:'CC0000'} },
{ text:'I have a triangle bullet', options:{bullet:{code:'25BA'}, color:'00CD00'} },
{ text:'no bullets on this line' , options:{font_size:12} },
{ text:'I have a normal bullet' , options:{bullet:true, color:'0000AB'} }
],
{ x:8.0, y:5.0, w:'30%', h:1.4, color:'ABABAB', margin:1 }
);
// EX: Drop/Outer Shadow
slide.addText(
'Outer Shadow',
{
x:0.5, y:6.0, font_size:36, color:'0088CC',
shadow: {type:'outer', color:'696969', blur:3, offset:10, angle:45}
}
);
// EX: Formatting can be applied at the word/line level
// Provide an array of text objects with the formatting options for that `text` string value
// Line-breaks work as well
slide.addText(
[
{ text:'word-level\nformatting', options:{ font_size:36, font_face:'Courier New', color:'99ABCC', align:'r', breakLine:true } },
{ text:'...in the same textbox', options:{ font_size:48, font_face:'Arial', color:'FFFF00', align:'c' } }
],
{ x:0.5, y:4.1, w:8.5, h:2.0, margin:0.1, fill:'232323' }
);
pptx.save('Demo-Text');
Syntax:
slide.addTable( [rows] );
slide.addTable( [rows], {any Layout/Formatting OPTIONS} );
Option | Type | Unit | Default | Description | Possible Values |
---|---|---|---|---|---|
x |
number | inches | 1.0 |
horizontal location | 0-n OR 'n%'. (Ex: {x:'50%'} will place object in the middle of the Slide) |
y |
number | inches | 1.0 |
vertical location | 0-n OR 'n%'. |
w |
number | inches | width | 0-n OR 'n%'. (Ex: {w:'50%'} will make object 50% width of the Slide) |
|
h |
number | inches | height | 0-n OR 'n%'. | |
colW |
integer | inches | width for every column | Ex: Width for every column in table (uniform) 2.0 |
|
colW |
array | inches | column widths in order | Ex: Width for each of 5 columns [1.0, 2.0, 2.5, 1.5, 1.0] |
|
rowH |
integer | inches | height for every row | Ex: Height for every row in table (uniform) 2.0 |
|
rowH |
array | inches | row heights in order | Ex: Height for each of 5 rows [1.0, 2.0, 2.5, 1.5, 1.0] |
Option | Type | Default | Description | Possible Values |
---|---|---|---|---|
autoPage |
boolean | true |
auto-page table | true or false |
lineWeight |
float | 0 | line weight value | -1.0 to 1.0. Ex: {lineWeight:0.5} |
newPageStartY |
object | starting y value for tables on new Slides |
0-n OR 'n%'. Ex:{newPageStartY:0.5} |
Tables will auto-page by default and the table on new Slides will use the current Slide's top margin
value as the starting point for y
.
Tables will retain their existing x
, w
, and colW
values as they are continued onto subsequent Slides.
autoPage
: allows the auto-paging functionality (as table rows overflow the Slide, new Slides will be added) to be disabled.lineWeight
: adjusts the calculated height of lines. If too much empty space is left under each table, then increase lineWeight value. Conversely, if the tables are overflowing the bottom of the Slides, then reduce the lineWeight value. Also helpful when using some fonts that do not have the usual golden ratio.newPageStartY
: provides the ability to specify where new tables will be placed on new Slides. For example, you may place a table halfway down a Slide, but you wouldn't that to be the starting location for subsequent tables. Use this option to ensure there is no wasted space and to guarantee a professional look.
Option | Type | Unit | Default | Description | Possible Values |
---|---|---|---|---|---|
align |
string | left |
alignment | left or center or right (or l c r ) |
|
bold |
boolean | false |
bold text | true or false |
|
border |
object | cell border | object with pt and color values. Ex: {pt:'1', color:'f1f1f1'} |
||
border |
array | cell border | array of objects with pt and color values in TRBL order. |
||
color |
string | text color | hex color code. Ex: {color:'0088CC'} |
||
colspan |
integer | column span | 2-n. Ex: {colspan:2} |
||
fill |
string | fill/bkgd color | hex color code. Ex: {color:'0088CC'} |
||
font_face |
string | font face | Ex: 'Arial' | ||
font_size |
number | points | font size | 1-256. Ex: {font_size:12} |
|
italic |
boolean | false |
italic text | true or false |
|
margin |
number | points | margin | 0-99 (ProTip: use the same value from CSS padding ) |
|
margin |
array | points | margin | array of integer values in TRBL order. Ex: margin:[5,10,5,10] |
|
rowspan |
integer | row span | 2-n. Ex: {rowspan:2} |
||
underline |
boolean | false |
underline text | true or false |
|
valign |
string | vertical alignment | top or middle or bottom (or t m b ) |
- Formatting Options passed to
slide.addTable()
apply to every cell in the table - You can selectively override formatting at a cell-level providing any Formatting Option in the cell
options
- Table cells can be either a plain text string or an object with text and options properties
- When using an object, any of the formatting options above can be passed in
options
and will apply to that cell only
Bullets and word-level formatting are supported inside table cells. Passing an array of objects with text/options values
as the text
value allows fine-grained control over the text inside cells.
- Available formatting options are here: Text Options
- See below for examples or view the
examples/pptxgenjs-demo.html
page for lots more
// TABLE 1: Cell-level Formatting
var rows = [];
// Row One: cells will be formatted according to any options provided to `addTable()`
rows.push( ['First', 'Second', 'Third'] );
// Row Two: set/override formatting for each cell
rows.push([
{ text:'1st', options:{color:'ff0000'} },
{ text:'2nd', options:{color:'00ff00'} },
{ text:'3rd', options:{color:'0000ff'} }
]);
slide.addTable( rows, { x:0.5, y:1.0, w:9.0, color:'363636' } );
// TABLE 2: Using word-level formatting inside cells
// NOTE: An array of text/options objects provides fine-grained control over formatting
var arrObjText = [
{ text:'Red ', options:{color:'FF0000'} },
{ text:'Green ', options:{color:'00FF00'} },
{ text:'Blue', options:{color:'0000FF'} }
];
// EX A: Pass an array of text objects to `addText()`
slide.addText( arrObjText, { x:0.5, y:2.75, w:9, h:2, margin:0.1, fill:'232323' } );
// EX B: Pass the same objects as a cell's `text` value
var arrTabRows = [
[
{ text:'Cell 1 A', options:{font_face:'Arial' } },
{ text:'Cell 1 B', options:{font_face:'Courier'} },
{ text: arrObjText, options:{fill:'232323'} }
]
];
slide.addTable( arrTabRows, { x:0.5, y:5, w:9, h:2, colW:[1.5,1.5,6] } );
var pptx = new PptxGenJS();
var slide = pptx.addNewSlide();
slide.addText('Demo-03: Table', { x:0.5, y:0.25, font_size:18, font_face:'Arial', color:'0088CC' });
// TABLE 1: Single-row table
// --------
var rows = [ 'Cell 1', 'Cell 2', 'Cell 3' ];
var tabOpts = { x:0.5, y:1.0, w:9.0, fill:'F7F7F7', font_size:14, color:'363636' };
slide.addTable( rows, tabOpts );
// TABLE 2: Multi-row table (each rows array element is an array of cells)
// --------
var rows = [
['A1', 'B1', 'C1'],
['A2', 'B2', 'C2']
];
var tabOpts = { x:0.5, y:2.0, w:9.0, fill:'F7F7F7', font_size:18, color:'6f9fc9' };
slide.addTable( rows, tabOpts );
// TABLE 3: Formatting at a cell level - use this to selectively override table's cell options
// --------
var rows = [
[
{ text:'Top Lft', options:{ valign:'t', align:'l', font_face:'Arial' } },
{ text:'Top Ctr', options:{ valign:'t', align:'c', font_face:'Verdana' } },
{ text:'Top Rgt', options:{ valign:'t', align:'r', font_face:'Courier' } }
],
];
var tabOpts = { x:0.5, y:4.5, w:9.0, rowH:0.6, fill:'F7F7F7', font_size:18, color:'6f9fc9', valign:'m'} };
slide.addTable( rows, tabOpts );
// Multiline Text / Line Breaks - use either "\r" or "\n"
slide.addTable( ['Line 1\nLine 2\nLine 3'], { x:2, y:3, w:4 });
pptx.save('Demo-Tables');
Syntax (no text):
slide.addShape({SHAPE}, {OPTIONS});
Syntax (with text):
slide.addText("some string", {SHAPE, OPTIONS});
Check the pptxgen.shapes.js
file for a complete list of the hundreds of PowerPoint shapes available.
Option | Type | Unit | Default | Description | Possible Values |
---|---|---|---|---|---|
x |
number | inches | 1.0 |
horizontal location | 0-n OR 'n%'. (Ex: {x:'50%'} will place object in the middle of the Slide) |
y |
number | inches | 1.0 |
vertical location | 0-n OR 'n%'. |
w |
number | inches | width | 0-n OR 'n%'. (Ex: {w:'50%'} will make object 50% width of the Slide) |
|
h |
number | inches | height | 0-n OR 'n%'. | |
align |
string | left |
alignment | left or center or right |
|
fill |
string | fill/bkgd color | hex color code. Ex: {color:'0088CC'} |
||
fill |
object | fill/bkgd color | object with type , color and optional alpha keys. Ex: fill:{type:'solid', color:'0088CC', alpha:25} |
||
flipH |
boolean | flip Horizontal | true or false |
||
flipV |
boolean | flip Vertical | true or false |
||
line |
string | border line color | hex color code. Ex: {line:'0088CC'} |
||
line_head |
string | border line ending | arrow , diamond , oval , stealth , triangle or none |
||
line_size |
number | points | border line size | 1-256. Ex: {line_size:4} | |
line_tail |
string | border line heading | arrow , diamond , oval , stealth , triangle or none |
||
rotate |
integer | degrees | rotation degrees | 0-360. Ex: {rotate:180} |
var pptx = new PptxGenJS();
pptx.setLayout('LAYOUT_WIDE');
var slide = pptx.addNewSlide();
// Misc Shapes
slide.addShape(pptx.shapes.LINE, { x:4.15, y:4.40, w:5, h:0, line:'FF0000', line_size:1 });
slide.addShape(pptx.shapes.LINE, { x:4.15, y:4.80, w:5, h:0, line:'FF0000', line_size:2, line_head:'triangle' });
slide.addShape(pptx.shapes.LINE, { x:4.15, y:5.20, w:5, h:0, line:'FF0000', line_size:3, line_tail:'triangle' });
slide.addShape(pptx.shapes.LINE, { x:4.15, y:5.60, w:5, h:0, line:'FF0000', line_size:4, line_head:'triangle', line_tail:'triangle' });
slide.addShape(pptx.shapes.RECTANGLE, { x:0.50, y:0.75, w:5, h:3, fill:'FF0000' });
slide.addShape(pptx.shapes.OVAL, { x:4.15, y:0.75, w:5, h:2, fill:{ type:'solid', color:'0088CC', alpha:25 } });
// Adding text to Shapes:
slide.addText('RIGHT-TRIANGLE', { shape:pptx.shapes.RIGHT_TRIANGLE, align:'c', x:0.40, y:4.3, w:6, h:3, fill:'0088CC', line:'000000', line_size:3 });
slide.addText('RIGHT-TRIANGLE', { shape:pptx.shapes.RIGHT_TRIANGLE, align:'c', x:7.00, y:4.3, w:6, h:3, fill:'0088CC', line:'000000', flipH:true });
pptx.save('Demo-Shapes');
Syntax:
slide.addImage({OPTIONS});
Animated GIFs can be included in Presentations in one of two ways:
- Using Node.js: use either
data
orpath
options (Node can encode any image into base64) - Client Browsers: pre-encode the gif and add it using the
data
option (encoding images into GIFs is beyond any current browser)
Option | Type | Unit | Default | Description | Possible Values |
---|---|---|---|---|---|
x |
number | inches | 1.0 |
horizontal location | 0-n |
y |
number | inches | 1.0 |
vertical location | 0-n |
w |
number | inches | 1.0 |
width | 0-n |
h |
number | inches | 1.0 |
height | 0-n |
data |
string | image data (base64) | base64-encoded image string. (either data or path is required) |
||
path |
string | image path | Same as used in an (img src="") tag. (either data or path is required) |
NOTE
SVG images are not currently supported in PowerPoint or PowerPoint Online (even when encoded into base64). PptxGenJS does
properly encode and include SVG images, so they will begin showing once Microsoft adds support for this image type.
Deprecation Warning
Old positional parameters (e.g.: slide.addImage('images/chart.png', 1, 1, 6, 3)
) are now deprecated as of 1.1.0
var pptx = new PptxGenJS();
var slide = pptx.addNewSlide();
// Image by path
slide.addImage({ path:'images/chart_world_peace_near.png', x:1.0, y:1.0, w:8.0, h:4.0 });
// Image by data (base64-encoding)
slide.addImage({ data:'image/png;base64,iVtDafDrBF[...]=', x:3.0, y:5.0, w:6.0, h:3.0 });
// NOTE: Slide API calls return the same slide, so you can chain calls:
slide.addImage({ path:'images/cc_license_comp_chart.png', x:6.6, y:0.75, w:6.30, h:3.70 })
.addImage({ path:'images/cc_logo.jpg', x:0.5, y:3.50, w:5.00, h:3.70 })
.addImage({ path:'images/cc_symbols_trans.png', x:6.6, y:4.80, w:6.30, h:2.30 });
pptx.save('Demo-Images');
Syntax:
slide.addMedia({OPTIONS});
Both Video (mpg, mov, mp4, m4v, etc.) and Audio (mp3, wav, etc.) are supported (list of supported formats)
- Using Node.js: use either
data
orpath
options (Node can encode any media into base64) - Client Browsers: pre-encode the media and add it using the
data
option (encoding video/audio is beyond any current browser)
Online video (YouTube embeds, etc.) is supported in both client browser and in Node.js
Option | Type | Unit | Default | Description | Possible Values |
---|---|---|---|---|---|
x |
number | inches | 1.0 |
horizontal location | 0-n |
y |
number | inches | 1.0 |
vertical location | 0-n |
w |
number | inches | 1.0 |
width | 0-n |
h |
number | inches | 1.0 |
height | 0-n |
data |
string | media data (base64) | base64-encoded string | ||
path |
string | media path | relative path to media file | ||
link |
string | online url/link | link to online video. Ex: link:'https://www.youtube.com/embed/blahBlah' |
||
type |
string | media type | media type: audio or video (reqs: data or path ) or online (reqs:link ) |
var pptx = new PptxGenJS();
var slide = pptx.addNewSlide();
// Media by path (Node.js only)
slide.addMedia({ type:'audio', path:'../media/sample.mp3', x:1.0, y:1.0, w:3.0, h:0.5 });
// Media by data (client browser or Node.js)
slide.addMedia({ type:'audio', data:'audio/mp3;base64,iVtDafDrBF[...]=', x:3.0, y:1.0, w:6.0, h:3.0 });
// Online by link (client browser or Node.js)
slide.addMedia({ type:'online', link:'https://www.youtube.com/embed/Dph6ynRVyUc', x:1.0, y:4.0, w:8.0, h:4.5 });
pptx.save('Demo-Media');
Generating sample slides like those shown above is great for demonstrating library features, but the reality is most of us will be required to produce presentations that have a certain design or corporate branding.
PptxGenJS allows you to define Master Slides via objects that can then be used to provide branding functionality.
Slide Masters are defined using the same object style used in Slides. Add these objects as a variable to a file that
is included in the script src tags on your page, then reference them by name in your code.
E.g.: <script lang="javascript" src="pptxgenjs.masters.js"></script>
pptxgenjs.masters.js
contents:
var gObjPptxMasters = {
MASTER_SLIDE: {
title: 'Slide master',
margin: [ 0.5, 0.25, 1.0, 0.25 ],
bkgd: 'FFFFFF',
images: [ { path:'images/logo_square.png', x:9.3, y:4.9, w:0.5, h:0.5 } ],
shapes: [
{ type:'text', text:'ACME - Confidential', x:0, y:5.17, w:'100%', h:0.3, align:'center', valign:'top', color:'7F7F7F', font_size:8, bold:true },
{ type:'line', x:0.3, y:3.85, w:5.7, h:0.0, line:'007AAA' },
{ type:'rectangle', x:0, y:0, w:'100%', h:.65, w:5, h:3.2, fill:'003b75' }
],
slideNumber: { x:0.3, y:'90%' }
},
TITLE_SLIDE: {
title: 'I am the Title Slide',
bkgd: { data:'image/png;base64,R0lGONlhotPQBMAPyoAPosR[...]+0pEZbEhAAOw==' },
images: [ { x:'7.4', y:'4.1', w:'2', h:'1', data:'image/png;base64,R0lGODlhPQBEAPeoAJosM[...]+0pCZbEhAAOw==' } ]
}
};
Every object added to the global master slide variable gObjPptxMasters
can then be referenced
by their key names that you created (e.g.: "TITLE_SLIDE").
TIP:
Pre-encode your images (base64) and add the string as the optional data key/val
(see the TITLE_SLIDE.images
object above)
var pptx = new PptxGenJS();
var slide1 = pptx.addNewSlide( pptx.masters.TITLE_SLIDE );
slide1.addText('How To Create PowerPoint Presentations with JavaScript', { x:0.5, y:0.7, font_size:18 });
// NOTE: Base master slide properties can be overridden on a selective basis:
// Here we can set a new background color or image on-the-fly
var slide2 = pptx.addNewSlide( pptx.masters.MASTER_SLIDE, { bkgd:'0088CC' } );
var slide3 = pptx.addNewSlide( pptx.masters.MASTER_SLIDE, { bkgd:{ path:'images/title_bkgd.jpg' } } );
var slide4 = pptx.addNewSlide( pptx.masters.MASTER_SLIDE, { bkgd:{ data:'image/png;base64,tFfInmP[...]=' } } );
pptx.save();
Option | Type | Unit | Default | Description | Possible Values |
---|---|---|---|---|---|
bkgd |
string | ffffff |
color | hex color code. Ex: { bkgd:'0088CC' } |
|
bkgd |
object | image | object with path OR data. Ex: {path:'img/bkgd.png'} OR {data:'image/png;base64,iVBORwTwB[...]='} |
||
images |
array | image(s) | object array of path OR data. Ex: {path:'img/logo.png'} OR {data:'image/png;base64,tFfInmP[...]'} |
||
slideNumber |
object | Show slide numbers | ex: { x:1.0, y:'50%' } x and y can be either inches or percent |
||
margin |
number | inches | 1.0 |
Slide margins | 0.0 through Slide.width |
margin |
array | Slide margins | array of numbers in TRBL order. Ex: [0.5, 0.75, 0.5, 0.75] |
||
shapes |
array | shape(s) | array of shape objects. Ex: (see Shape section) | ||
title |
string | Slide title | some title |
A sample masters file is included in the distribution folder and contain a couple of different slides to get you started.
Location: PptxGenJS/dist/pptxgen.masters.js
Syntax:
slide.addSlidesForTable(htmlElementID);
slide.addSlidesForTable(htmlElementID, {OPTIONS});
Any variety of HTML tables can be turned into a series of slides (auto-paging) by providing the table's ID.
- Reproduces an HTML table - background colors, borders, fonts, padding, etc.
- Slide margins are based on either the Master Slide provided or options
NOTE: Nested tables are not supported in PowerPoint, so only the string contents of a single level deep table cell will be reproduced
Option | Type | Unit | Description | Possible Values |
---|---|---|---|---|
x |
number | inches | horizontal location | 0-256. Table will be placed here on each Slide |
y |
number | inches | vertical location | 0-256. Table will be placed here on each Slide |
w |
number | inches | width | 0-256. Default is (100% - Slide margins) |
h |
number | inches | height | 0-256. Default is (100% - Slide margins) |
master |
string | master slide name | Any pre-defined Master Slide. EX: { master:pptx.masters.TITLE_SLIDE } |
|
addHeaderToEach |
boolean | add table headers to each slide | EX: addHeaderToEach:true |
|
addImage |
string | add an image to each slide | Use the established syntax. EX: { addImage:{ path:"images/logo.png", x:10, y:0.5, w:1.2, h:0.75 } } |
|
addShape |
string | add a shape to each slide | Use the established syntax. | |
addTable |
string | add a table to each slide | Use the established syntax. | |
addText |
string | add text to each slide | Use the established syntax. |
A minimum column width can be specified by adding a data-pptx-min-width
attribute to any given <th>
tag.
Example:
<table id="tabAutoPaging" class="tabCool">
<thead>
<tr>
<th data-pptx-min-width="0.6" style="width: 5%">Row</th>
<th data-pptx-min-width="0.8" style="width:10%">Last Name</th>
<th data-pptx-min-width="0.8" style="width:10%">First Name</th>
<th style="width:75%">Description</th>
</tr>
</thead>
<tbody></tbody>
</table>
- Default
x
,y
andmargin
value is 0.5 inches, the table will take up all remaining space by default (h:100%, w:100%) - Your Master Slides should already have defined margins, so a Master Slide name is the only option you'll need most of the time
// Pass table element ID to addSlidesForTable function to produce 1-N slides
pptx.addSlidesForTable( 'myHtmlTableID' );
// Optionally, include a Master Slide name for pre-defined margins, background, logo, etc.
pptx.addSlidesForTable( 'myHtmlTableID', { master:pptx.masters.MASTER_SLIDE } );
// Optionally, add images/shapes/text/tables to each Slide
pptx.addSlidesForTable( 'myHtmlTableID', { addText:{ text:"Dynamic Title", options:{x:1, y:0.5, color:'0088CC'} } } );
pptx.addSlidesForTable( 'myHtmlTableID', { addImage:{ path:"images/logo.png", x:10, y:0.5, w:1.2, h:0.75 } } );
Design a Master Slide that already contains: slide layout, margins, logos, etc., then you can produce professional looking Presentations with a single line of code which can be embedded into a link or a button:
Add a button to a webpage that will create a Presentation using whatever table data is present:
<input type="button" value="Export to PPTX"
onclick="{ var pptx = new PptxGenJS(); pptx.addSlidesForTable('tableId',{ master:pptx.masters.MASTER_SLIDE }); pptx.save(); }">
SharePoint Integration
Placing a button like this into a WebPart is a great way to add "Export to PowerPoint" functionality
to SharePoint. (You'd also need to add the 4 <script>
includes in the same or another WebPart)
If you are planning on creating Shapes (basically anything other than Text, Tables or Rectangles), then you'll want to
include the pptxgen.shapes.js
library.
The shapes file contains a complete PowerPoint Shape object array thanks to the officegen project.
<script lang="javascript" src="PptxGenJS/dist/pptxgen.shapes.js"></script>
It takes CPU time to read and encode images! The more images you include and the larger they are, the more time will be consumed. The time needed to read/encode images can be completely eliminated by pre-encoding any images (see below).
Pre-encode images into a base64 string (eg: 'image/png;base64,iVBORw[...]=') for use as the data
option value.
This will both reduce dependencies (who needs another image asset to keep track of?) and provide a performance
boost (no time will need to be consumed reading and encoding the image).
Please file issues or suggestions on the issues page on github, or even better, submit a pull request. Feedback is always welcome!
When reporting issues, please include a code snippet or a link demonstrating the problem. Here is a small jsFiddle that is already configured and uses the latest PptxGenJS code.
Sometimes implementing a new library can be a difficult task and the slightest mistake will keep something from working. We've all been there!
If you are having issues getting a presentation to generate, check out the demos in the examples
directory. There
are demos for both nodejs and client-browsers that contain working examples of every available library feature.
- Use a pre-configured jsFiddle to test with: PptxGenJS Fiddle
- Use Ask Question on StackOverflow - be sure to tag it with "PptxGenJS"
Version 2.0 will be released in late 2017 and will drop support for IE11 as we move to adopt more JavaScript ES6 features and remove many instances of jQuery utility functions.
The PptxgenJS library is not designed to replicate all the functionality of PowerPoint, meaning several features are not on the development roadmap.
These include:
- Animations
- Cross-Slide Links
- Importing Existing Templates
- Outlines
- SmartArt
- Officegen Project - For the Shape definitions and XML code
- Dzmitry Dulko - For getting the project published on NPM
- Everyone who has submitted an Issue or Pull Request. :-)
Do you like this library and find it useful? Add a link to the PptxGenJS project on your blog, website or social media.
Thanks to everyone who supports this project! <3
Copyright © 2015-2017 Brent Ely