diff --git a/src/features/exporter/js/exporter.js b/src/features/exporter/js/exporter.js index 0187a44ad7..a9fe0892c3 100644 --- a/src/features/exporter/js/exporter.js +++ b/src/features/exporter/js/exporter.js @@ -214,6 +214,14 @@ *
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) + *
Defaults to 'download.pdf' + */ + gridOptions.exporterPdfFilename = gridOptions.exporterPdfFilename ? gridOptions.exporterPdfFilename : 'download.pdf'; /** * @ngdoc object * @name exporterOlderExcelCompatibility @@ -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( @@ -904,7 +905,7 @@ fileName ); } - + //html5 A[download] if ('download' in a) { var blob = new Blob( @@ -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); @@ -931,7 +932,7 @@ a.dispatchEvent(eventObj); } D.body.removeChild(a); - + }, this.delay); }, @@ -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 diff --git a/src/features/exporter/test/exporter.spec.js b/src/features/exporter/test/exporter.spec.js index 21a42bfd62..34e6ed121d 100644 --- a/src/features/exporter/test/exporter.spec.js +++ b/src/features/exporter/test/exporter.spec.js @@ -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 ] }, @@ -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 ] }, @@ -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 ] },