Skip to content

Commit

Permalink
Made FileSaver optional for modern browsers, fix for Issue #57
Browse files Browse the repository at this point in the history
  • Loading branch information
GitBrent authored and GitBrent committed Mar 21, 2017
1 parent 78f2559 commit 6cc6248
Showing 1 changed file with 35 additions and 4 deletions.
39 changes: 35 additions & 4 deletions dist/pptxgen.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ if ( NODEJS ) {
var PptxGenJS = function(){
// CONSTANTS
var APP_VER = "1.3.0";
var APP_REL = "20170314";
var APP_REL = "20170320";
//
var LAYOUTS = {
'LAYOUT_4x3' : { name: 'screen4x3', width: 9144000, height: 6858000 },
Expand Down Expand Up @@ -192,7 +192,36 @@ var PptxGenJS = function(){
zip.generateAsync({type:'nodebuffer'}).then(function(content){ fs.writeFile(strExportName, content, callback(strExportName)); });
}
else {
zip.generateAsync({type:'blob'}).then(function(content){ saveAs(content, strExportName); });
zip.generateAsync({type:'blob'}).then(function(content){ writeFileToBrowser(strExportName, content, callback); });
}
}

function writeFileToBrowser(strExportName, content, callback) {
// DESIGN: Use `createObjectURL()` to push files to modern client browsers (FYI: it is synchronously executed)
// IE11 NOTE: Un-supported and using the MS-specific function with blob will cause a heinous security warning on page load, so no go on that.
// .........: If `saveAs` is defined, then FileSaver.js is included so we use it
if ( typeof saveAs !== 'undefined' ) {
saveAs(content, strExportName);
}
else if ( typeof window.URL.createObjectURL !== 'undefined' ) {
// STEP 1: Create element
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";

// STEP 2: Create blob, set element props, push to browser
var blob = new Blob([content], {type: "octet/stream"});
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = strExportName;
a.click();

// STEP 3: Clean-up
window.URL.revokeObjectURL(url);
document.body.removeChild(a);

// LAST: Callback (if any)
if ( callback ) callback(strExportName);
}
}

Expand Down Expand Up @@ -2371,7 +2400,7 @@ var PptxGenJS = function(){
var emuSlideTabW = ( opts.w ? inch2Emu(opts.w) : (gObjPptx.pptLayout.width - inch2Emu(arrInchMargins[1] + arrInchMargins[3])) );
var emuSlideTabH = ( opts.h ? inch2Emu(opts.h) : (gObjPptx.pptLayout.height - inch2Emu(arrInchMargins[0] + arrInchMargins[2])) );

// STEP 1: Grab overall table style/col widths
// STEP 1: Grab table col widths
$.each(['thead','tbody','tfoot'], function(i,val){
if ( $('#'+tabEleId+' > '+val+' > tr').length > 0 ) {
$('#'+tabEleId+' > '+val+' > tr:first-child').find('> th, > td').each(function(i,cell){
Expand Down Expand Up @@ -2403,11 +2432,13 @@ var PptxGenJS = function(){
$('#'+tabEleId+' > '+val+' > tr').each(function(i,row){
var arrObjTabCells = [];
$(row).find('> th, > td').each(function(i,cell){
// A: Covert colors from RGB to Hex
// A: Get RGB text/bkgd colors
var arrRGB1 = [];
var arrRGB2 = [];
arrRGB1 = $(cell).css('color').replace(/\s+/gi,'').replace('rgba(','').replace('rgb(','').replace(')','').split(',');
arrRGB2 = $(cell).css('background-color').replace(/\s+/gi,'').replace('rgba(','').replace('rgb(','').replace(')','').split(',');
// ISSUE#57: jQuery default is this rgba value of below giving unstyled tables a black bkgd, so use white instead (FYI: if cell has `background:#000000` jQuery returns 'rgb(0, 0, 0)', so this soln is pretty solid)
if ( $(cell).css('background-color') == 'rgba(0, 0, 0, 0)' ) arrRGB2 = [255,255,255];

// B: Create option object
var objOpts = {
Expand Down

0 comments on commit 6cc6248

Please sign in to comment.