Skip to content

Commit

Permalink
Fix(exporter): angular-ui#3642 angular-ui#2921 IE downloads PDF rathe…
Browse files Browse the repository at this point in the history
…r than trying to open
  • Loading branch information
PaulL1 committed Jun 6, 2015
1 parent 1d0b3d4 commit 9ee1e89
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 16 deletions.
82 changes: 66 additions & 16 deletions src/features/exporter/js/exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@
* <br/>Defaults to 'download.csv'
*/
gridOptions.exporterCsvFilename = gridOptions.exporterCsvFilename ? gridOptions.exporterCsvFilename : 'download.csv';
/**
* @ngdoc object
* @name exporterPdfFilename
* @propertyOf ui.grid.exporter.api:GridOptions
* @description The default filename to use when saving the downloaded pdf, only used in IE (other browsers open pdfs in a new window)
* <br/>Defaults to 'download.pdf'
*/
gridOptions.exporterPdfFilename = gridOptions.exporterPdfFilename ? gridOptions.exporterPdfFilename : 'download.pdf';
/**
* @ngdoc object
* @name exporterOlderExcelCompatibility
Expand Down Expand Up @@ -872,29 +880,22 @@
var strMimeType = 'application/octet-stream;charset=utf-8';
var rawFile;
var ieVersion;

if (!fileName) {
var currentDate = new Date();
fileName = "CWS Export - " + currentDate.getFullYear() + (currentDate.getMonth() + 1) +
currentDate.getDate() + currentDate.getHours() +
currentDate.getMinutes() + currentDate.getSeconds() + ".csv";
}

ieVersion = this.isIE();
if (ieVersion && ieVersion < 10) {
var frame = D.createElement('iframe');
document.body.appendChild(frame);

frame.contentWindow.document.open("text/html", "replace");
frame.contentWindow.document.write('sep=,\r\n' + csvContent);
frame.contentWindow.document.close();
frame.contentWindow.focus();
frame.contentWindow.document.execCommand('SaveAs', true, fileName);

document.body.removeChild(frame);
return true;
}

// IE10+
if (navigator.msSaveBlob) {
return navigator.msSaveBlob(
Expand All @@ -904,7 +905,7 @@
fileName
);
}

//html5 A[download]
if ('download' in a) {
var blob = new Blob(
Expand All @@ -917,7 +918,7 @@
rawFile = 'data:' + strMimeType + ',' + encodeURIComponent(csvContent);
a.setAttribute('target', '_blank');
}

a.href = rawFile;
a.setAttribute('style', 'display:none;');
D.body.appendChild(a);
Expand All @@ -931,7 +932,7 @@
a.dispatchEvent(eventObj);
}
D.body.removeChild(a);

}, this.delay);
},

Expand Down Expand Up @@ -960,14 +961,63 @@
var docDefinition = self.prepareAsPdf(grid, exportColumnHeaders, exportData);

if (self.isIE()) {
var pdf = pdfMake.createPdf(docDefinition).download();
self.downloadPDF(grid.options.exporterPdfFilename, docDefinition);
} else {
pdfMake.createPdf(docDefinition).open();
}
});
},




/**
* @ngdoc function
* @name downloadPdf
* @methodOf ui.grid.exporter.service:uiGridExporterService
* @description Generates and retrieves the pdf as a blob, then downloads
* it as a file. Only used in IE, in all other browsers we use the native
* pdfMake.open function to just open the PDF
* @param {string} fileName the filename to give to the pdf, can be set
* through exporterPdfFilename
* @param {object} docDefinition a pdf docDefinition that we can generate
* and get a blob from
*/
downloadPDF: function (fileName, docDefinition) {
var D = document;
var a = D.createElement('a');
var strMimeType = 'application/octet-stream;charset=utf-8';
var rawFile;
var ieVersion;

ieVersion = this.isIE();
var doc = pdfMake.createPdf(docDefinition);
var blob;
doc.getBuffer( function (buffer) {
blob = new Blob([buffer]);
});

if (ieVersion && ieVersion < 10) {
var frame = D.createElement('iframe');
document.body.appendChild(frame);

frame.contentWindow.document.open("text/html", "replace");
frame.contentWindow.document.write(blob);
frame.contentWindow.document.close();
frame.contentWindow.focus();
frame.contentWindow.document.execCommand('SaveAs', true, fileName);

document.body.removeChild(frame);
return true;
}

// IE10+
if (navigator.msSaveBlob) {
return navigator.msSaveBlob(
blob, fileName
);
}
},


/**
* @ngdoc function
* @name renderAsPdf
Expand Down
3 changes: 3 additions & 0 deletions src/features/exporter/test/exporter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ describe('ui.grid.exporter uiGridExporterService', function () {
exporterMenuLabel: 'Export',
exporterCsvColumnSeparator: ',',
exporterCsvFilename: 'download.csv',
exporterPdfFilename: 'download.pdf',
exporterOlderExcelCompatibility: false,
exporterPdfDefaultStyle : { fontSize : 11 },
exporterPdfTableStyle : { margin : [ 0, 5, 0, 15 ] },
Expand Down Expand Up @@ -97,6 +98,7 @@ describe('ui.grid.exporter uiGridExporterService', function () {
exporterMenuLabel: 'custom export button',
exporterCsvColumnSeparator: ';',
exporterCsvFilename: 'myfile.csv',
exporterPdfFilename: 'myfile.pdf',
exporterOlderExcelCompatibility: true,
exporterPdfDefaultStyle : { fontSize : 12 },
exporterPdfTableStyle : { margin : [ 15, 5, 15, 15 ] },
Expand All @@ -121,6 +123,7 @@ describe('ui.grid.exporter uiGridExporterService', function () {
exporterMenuLabel: 'custom export button',
exporterCsvColumnSeparator: ';',
exporterCsvFilename: 'myfile.csv',
exporterPdfFilename: 'myfile.pdf',
exporterOlderExcelCompatibility: true,
exporterPdfDefaultStyle : { fontSize : 12 },
exporterPdfTableStyle : { margin : [ 15, 5, 15, 15 ] },
Expand Down

0 comments on commit 9ee1e89

Please sign in to comment.