forked from gitbrent/PptxGenJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodejs-demo.js
123 lines (101 loc) · 4.58 KB
/
nodejs-demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* NAME: nodejs-demo.js
* AUTH: Brent Ely (https://github.com/gitbrent/)
* DATE: 20181023
* DESC: PptxGenJS feature demos for Node.js
* REQS: npm 4.x + `npm install pptxgenjs`
*
* USAGE: `node nodejs-demo.js` (runs local tests with callbacks etc)
* USAGE: `node nodejs-demo.js All` (runs all pre-defined tests in `pptxgenjs-demo.js`)
* USAGE: `node nodejs-demo.js Text` (runs pre-defined single test in `pptxgenjs-demo.js`)
*/
// ============================================================================
const express = require('express'); // Not core - Only required for streaming
const app = express(); // Not core - Only required for streaming
const fs = require('fs');
var gConsoleLog = true;
function getTimestamp() {
var dateNow = new Date();
var dateMM = dateNow.getMonth() + 1; dateDD = dateNow.getDate(); dateYY = dateNow.getFullYear(), h = dateNow.getHours(); m = dateNow.getMinutes();
return dateNow.getFullYear() +''+ (dateMM<=9 ? '0' + dateMM : dateMM) +''+ (dateDD<=9 ? '0' + dateDD : dateDD) + (h<=9 ? '0' + h : h) + (m<=9 ? '0' + m : m);
}
// ============================================================================
if (gConsoleLog) console.log(`
-------------
STARTING DEMO
-------------
`);
// STEP 1: Load pptxgenjs library
var PptxGenJS;
if (fs.existsSync('../dist/pptxgen.js')) {
// for LOCAL TESTING
PptxGenJS = require('../dist/pptxgen.js');
if (gConsoleLog) console.log('--=== LOCAL MODE ===--');
let pptx = new PptxGenJS();
if (gConsoleLog) console.log(`* pptxgenjs ver: ${pptx.version}`);
}
else {
PptxGenJS = require("pptxgenjs");
}
var pptx = new PptxGenJS();
var demo = require("../examples/pptxgenjs-demo.js");
if (gConsoleLog) console.log(`* save location: ${__dirname}`);
// ============================================================================
// EX: Regular callback - will be sent the export filename once the file has been written to fs
function saveCallback(filename) {
if (gConsoleLog) {
console.log('`saveCallback()`: Export filename: '+ filename);
}
}
// EX: JSZip callback - take the specified output (`data`) and do whatever
function jszipCallback(data) {
if (gConsoleLog) {
console.log('jszipCallback(): Here are 0-100 chars of `data`:\n');
console.log( data.substring(0,100) );
}
}
// EX: Callback that receives the PPT binary data - use this to stream file
function streamCallback(data) {
var strFilename = "Node-Demo-Streamed-Callback.pptx";
app.get('/', (req,res) => {
res.writeHead(200, { 'Content-disposition':'attachment;filename='+strFilename, 'Content-Length':data.length });
res.end(new Buffer(data, 'binary'));
});
app.listen(3000, () => {
console.log('PptxGenJS Node Demo app listening on port 3000!');
console.log('Visit: http://localhost:3000/');
console.log('(press Ctrl-C to quit app)');
});
}
// ============================================================================
// STEP 2: Run predefined test from `pptxgenjs-demo.js` //-OR-// Local Tests (callbacks, etc.)
if ( process.argv.length == 3 ) {
if ( process.argv[2].toLowerCase() == 'all' ) demo.runEveryTest();
else demo.execGenSlidesFuncs( process.argv[2] );
}
else {
// STEP 3: Omit an arg to run only these below
var exportName = 'PptxGenJS_Demo_Node_'+getTimestamp();
var pptx = new PptxGenJS();
var slide = pptx.addNewSlide();
slide.addText( 'New Node Presentation', {x:1.5, y:1.5, w:6, h:2, margin:0.1, fill:'FFFCCC'} );
// **NOTE**: Only uncomment one EXAMPLE at a time
// EXAMPLE 1: Inline save (saves to the local directory where this process is running)
//pptx.save( exportName+'-ex1' ); if (gConsoleLog) console.log('\nFile created:\n'+' * '+exportName+'-ex1');
// EXAMPLE 2: Use an inline callback function
pptx.save( exportName+'-ex2', function(filename){ console.log('Ex2 inline callback exported: '+exportName+'-ex2'); } );
// EXAMPLE 3: Use defined callback function
//pptx.save( exportName+'-ex3', saveCallback );
// EXAMPLE 4: Save in various formats - JSZip offers: ['arraybuffer', 'base64', 'binarystring', 'blob', 'nodebuffer', 'uint8array']
//pptx.save( exportName+'-jszip', jszipCallback, 'base64' );
// EXAMPLE 5: Use callback with 'http' in filename to get content back instead of writing a file - use this for streaming, cloud, etc.
// TEST: Use your browser and go to "http://localhost:3000/", then the file will be donwloaded
//pptx.save( 'http', streamCallback );
// **NOTE** If you continue to use the `pptx` variable, new Slides will be added to the existing set
}
// ============================================================================
if (gConsoleLog) console.log(`
--------------
DEMO COMPLETE!
--------------
`);