Skip to content

Commit

Permalink
Implemented save callback for Issue #40
Browse files Browse the repository at this point in the history
Added the filename as return value
  • Loading branch information
GitBrent authored and GitBrent committed Feb 16, 2017
1 parent 10256be commit b2cd8b0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
43 changes: 21 additions & 22 deletions dist/pptxgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ var PptxGenJS = function(){
// STEP 3: Push the PPTX file to browser
var strExportName = ((gObjPptx.fileName.toLowerCase().indexOf('.ppt') > -1) ? gObjPptx.fileName : gObjPptx.fileName+gObjPptx.fileExtn);
if ( NODEJS ) {
zip.generateAsync({type:'nodebuffer'}).then(function(content){ fs.writeFile(strExportName, content, callback); });
zip.generateAsync({type:'nodebuffer'}).then(function(content){ fs.writeFile(strExportName, content, callback(strExportName)); });
}
else {
zip.generateAsync({type:'blob'}).then(function(content){ saveAs(content, strExportName); });
Expand Down Expand Up @@ -230,18 +230,6 @@ var PptxGenJS = function(){
}

function convertImgToDataURLviaCanvas(slideRel){
if ( NODEJS ) {
try {
var bitmap = fs.readFileSync(slideRel.path);
callbackImgToDataURLDone( new Buffer(bitmap).toString('base64'), slideRel );
}
catch(ex) {
console.error('ERROR: Unable to read image: '+slideRel.path);
callbackImgToDataURLDone(IMG_BROKEN, slideRel);
}
return;
}

// A: Create
var image = new Image();

Expand Down Expand Up @@ -285,18 +273,15 @@ var PptxGenJS = function(){
function callbackImgToDataURLDone(inStr, slideRel){
var intEmpty = 0;

// STEP 1: Store base64 data for this image
slideRel.data = inStr;

// STEP 2: Set data for this rel, count outstanding
// STEP 1: Set data for this rel, count outstanding
$.each(gObjPptx.slides, function(i,slide){
$.each(slide.rels, function(i,rel){
if ( rel.path == slideRel.path ) rel.data = inStr;
if ( !rel.data ) intEmpty++;
});
});

// STEP 3: Continue export process if all rels have base64 `data` now
// STEP 2: Continue export process if all rels have base64 `data` now
if ( intEmpty == 0 ) doExportPresentation();
}

Expand Down Expand Up @@ -1767,14 +1752,28 @@ var PptxGenJS = function(){
// STEP 1: Set export title (if any)
if ( inStrExportName ) gObjPptx.fileName = inStrExportName;

// STEP 2: Total all physical rels across the Presentation
// STEP 2: Read/Encode Images
// B: Total all physical rels across the Presentation
// PERF: Only send unique paths for encoding (encoding func will find and fill *ALL* matching paths across the Presentation)
gObjPptx.slides.forEach(function(slide,idx){
slide.rels.forEach(function(rel,idy){
if ( rel.type != 'online' && !rel.data && $.inArray(rel.path, arrRelsDone) == -1 ) {
intRels++;
convertImgToDataURLviaCanvas(rel, callbackImgToDataURLDone);
arrRelsDone.push(rel.path);
// Node encoding is syncronous, so we can load all images here, then call export with a callback (if any)
if ( NODEJS ) {
try {
var bitmap = fs.readFileSync(rel.path);
rel.data = new Buffer(bitmap).toString('base64');
}
catch(ex) {
console.error('ERROR: Unable to read image: '+rel.path);
rel.data = IMG_BROKEN;
}
}
else {
intRels++;
convertImgToDataURLviaCanvas(rel, callbackImgToDataURLDone);
arrRelsDone.push(rel.path);
}
}
});
});
Expand Down
27 changes: 23 additions & 4 deletions examples/nodejs-demo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* NAME: nodejs-demo.js
* AUTH: Brent Ely (https://github.com/gitbrent/)
* DATE: Jan 19, 2017
* DATE: Feb 15, 2017
* DESC: Demonstrate PptxGenJS on Node.js
* REQS: npm 4.x + `npm install pptxgenjs`
* EXEC: `node nodejs-demo.js`
Expand All @@ -18,8 +18,8 @@ STARTING TEST
// ============================================================================

// STEP 1: Load pptxgenjs and show version to verify everything loaded correctly
var pptx = require('../dist/pptxgen.js'); // for LOCAL TESTING
//var pptx = require("pptxgenjs");
//var pptx = require('../dist/pptxgen.js'); // for LOCAL TESTING
var pptx = require("pptxgenjs");
console.log(` * pptxgenjs version: ${pptx.getVersion()}`); // Loaded okay?

pptx.setTitle('PptxGenJS Node.js Demo');
Expand Down Expand Up @@ -437,6 +437,12 @@ function demoMedia() {

// ============================================================================

function saveCallback(filename) {
console.log('Good News Everyone! File created: '+ filename);
}

// ============================================================================

// STEP 3: Run all test funcs
demoBasic();
demoText();
Expand All @@ -447,7 +453,20 @@ demoMedia();
demoCorpPres();

// STEP 4: Export giant demo file
pptx.save('Node_Demo_'+getTimestamp());

// A: Inline save
//pptx.save('Node_Demo_'+getTimestamp());

// B: or Save using callback function
//pptx.save('Node_Demo_'+getTimestamp(), function(filename){ console.log('Created: '+filename); } );

// C: or use a predefined callback function
pptx.save('Node_Demo_'+getTimestamp(), saveCallback );


// **NOTE** If you continue to use the `pptx` variable, new Slides will be added to the existing set
// Create a new variable or reset `pptx` for an empty Presenation
// EX: pptx = require("pptxgenjs");

// ============================================================================

Expand Down

0 comments on commit b2cd8b0

Please sign in to comment.