UNPKG

object-exporter

Version:

A little JavaScript plugin to generate PDF, XLS, CSV and DOC from JavaScript Object or DOM element only from the frontend!

2 lines 15.2 kB
(()=>{var e={213:function(e,t,o){var n,r;void 0===(r="function"==typeof(n=function(){"use strict";function t(e,t,o){var n=new XMLHttpRequest;n.open("GET",e),n.responseType="blob",n.onload=function(){l(n.response,t,o)},n.onerror=function(){console.error("could not download file")},n.send()}function n(e){var t=new XMLHttpRequest;t.open("HEAD",e,!1);try{t.send()}catch(e){}return 200<=t.status&&299>=t.status}function r(e){try{e.dispatchEvent(new MouseEvent("click"))}catch(o){var t=document.createEvent("MouseEvents");t.initMouseEvent("click",!0,!0,window,0,0,0,80,20,!1,!1,!1,!1,0,null),e.dispatchEvent(t)}}var i="object"==typeof window&&window.window===window?window:"object"==typeof self&&self.self===self?self:"object"==typeof o.g&&o.g.global===o.g?o.g:void 0,a=i.navigator&&/Macintosh/.test(navigator.userAgent)&&/AppleWebKit/.test(navigator.userAgent)&&!/Safari/.test(navigator.userAgent),l=i.saveAs||("object"!=typeof window||window!==i?function(){}:"download"in HTMLAnchorElement.prototype&&!a?function(e,o,a){var l=i.URL||i.webkitURL,s=document.createElement("a");o=o||e.name||"download",s.download=o,s.rel="noopener","string"==typeof e?(s.href=e,s.origin===location.origin?r(s):n(s.href)?t(e,o,a):r(s,s.target="_blank")):(s.href=l.createObjectURL(e),setTimeout((function(){l.revokeObjectURL(s.href)}),4e4),setTimeout((function(){r(s)}),0))}:"msSaveOrOpenBlob"in navigator?function(e,o,i){if(o=o||e.name||"download","string"!=typeof e)navigator.msSaveOrOpenBlob(function(e,t){return void 0===t?t={autoBom:!1}:"object"!=typeof t&&(console.warn("Deprecated: Expected third argument to be a object"),t={autoBom:!t}),t.autoBom&&/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(e.type)?new Blob(["\ufeff",e],{type:e.type}):e}(e,i),o);else if(n(e))t(e,o,i);else{var a=document.createElement("a");a.href=e,a.target="_blank",setTimeout((function(){r(a)}))}}:function(e,o,n,r){if((r=r||open("","_blank"))&&(r.document.title=r.document.body.innerText="downloading..."),"string"==typeof e)return t(e,o,n);var l="application/octet-stream"===e.type,s=/constructor/i.test(i.HTMLElement)||i.safari,d=/CriOS\/[\d]+/.test(navigator.userAgent);if((d||l&&s||a)&&"undefined"!=typeof FileReader){var c=new FileReader;c.onloadend=function(){var e=c.result;e=d?e:e.replace(/^data:[^;]*;/,"data:attachment/file;"),r?r.location.href=e:location=e,r=null},c.readAsDataURL(e)}else{var u=i.URL||i.webkitURL,f=u.createObjectURL(e);r?r.location=f:location.href=f,r=null,setTimeout((function(){u.revokeObjectURL(f)}),4e4)}});i.saveAs=l.saveAs=l,e.exports=l})?n.apply(t,[]):n)||(e.exports=r)},468:(e,t,o)=>{"use strict";o.r(t),o.d(t,{BotInfo:()=>l,BrowserInfo:()=>r,NodeInfo:()=>i,ReactNativeInfo:()=>s,SearchBotDeviceInfo:()=>a,browserName:()=>h,detect:()=>p,detectOS:()=>y,getNodeVersion:()=>v,parseUserAgent:()=>b});var n=function(e,t,o){if(o||2===arguments.length)for(var n,r=0,i=t.length;r<i;r++)!n&&r in t||(n||(n=Array.prototype.slice.call(t,0,r)),n[r]=t[r]);return e.concat(n||Array.prototype.slice.call(t))},r=function(e,t,o){this.name=e,this.version=t,this.os=o,this.type="browser"},i=function(e){this.version=e,this.type="node",this.name="node",this.os=process.platform},a=function(e,t,o,n){this.name=e,this.version=t,this.os=o,this.bot=n,this.type="bot-device"},l=function(){this.type="bot",this.bot=!0,this.name="bot",this.version=null,this.os=null},s=function(){this.type="react-native",this.name="react-native",this.version=null,this.os=null},d=/(nuhk|curl|Googlebot|Yammybot|Openbot|Slurp|MSNBot|Ask\ Jeeves\/Teoma|ia_archiver)/,c=3,u=[["aol",/AOLShield\/([0-9\._]+)/],["edge",/Edge\/([0-9\._]+)/],["edge-ios",/EdgiOS\/([0-9\._]+)/],["yandexbrowser",/YaBrowser\/([0-9\._]+)/],["kakaotalk",/KAKAOTALK\s([0-9\.]+)/],["samsung",/SamsungBrowser\/([0-9\.]+)/],["silk",/\bSilk\/([0-9._-]+)\b/],["miui",/MiuiBrowser\/([0-9\.]+)$/],["beaker",/BeakerBrowser\/([0-9\.]+)/],["edge-chromium",/EdgA?\/([0-9\.]+)/],["chromium-webview",/(?!Chrom.*OPR)wv\).*Chrom(?:e|ium)\/([0-9\.]+)(:?\s|$)/],["chrome",/(?!Chrom.*OPR)Chrom(?:e|ium)\/([0-9\.]+)(:?\s|$)/],["phantomjs",/PhantomJS\/([0-9\.]+)(:?\s|$)/],["crios",/CriOS\/([0-9\.]+)(:?\s|$)/],["firefox",/Firefox\/([0-9\.]+)(?:\s|$)/],["fxios",/FxiOS\/([0-9\.]+)/],["opera-mini",/Opera Mini.*Version\/([0-9\.]+)/],["opera",/Opera\/([0-9\.]+)(?:\s|$)/],["opera",/OPR\/([0-9\.]+)(:?\s|$)/],["pie",/^Microsoft Pocket Internet Explorer\/(\d+\.\d+)$/],["pie",/^Mozilla\/\d\.\d+\s\(compatible;\s(?:MSP?IE|MSInternet Explorer) (\d+\.\d+);.*Windows CE.*\)$/],["netfront",/^Mozilla\/\d\.\d+.*NetFront\/(\d.\d)/],["ie",/Trident\/7\.0.*rv\:([0-9\.]+).*\).*Gecko$/],["ie",/MSIE\s([0-9\.]+);.*Trident\/[4-7].0/],["ie",/MSIE\s(7\.0)/],["bb10",/BB10;\sTouch.*Version\/([0-9\.]+)/],["android",/Android\s([0-9\.]+)/],["ios",/Version\/([0-9\._]+).*Mobile.*Safari.*/],["safari",/Version\/([0-9\._]+).*Safari/],["facebook",/FB[AS]V\/([0-9\.]+)/],["instagram",/Instagram\s([0-9\.]+)/],["ios-webview",/AppleWebKit\/([0-9\.]+).*Mobile/],["ios-webview",/AppleWebKit\/([0-9\.]+).*Gecko\)$/],["curl",/^curl\/([0-9\.]+)$/],["searchbot",/alexa|bot|crawl(er|ing)|facebookexternalhit|feedburner|google web preview|nagios|postrank|pingdom|slurp|spider|yahoo!|yandex/]],f=[["iOS",/iP(hone|od|ad)/],["Android OS",/Android/],["BlackBerry OS",/BlackBerry|BB10/],["Windows Mobile",/IEMobile/],["Amazon OS",/Kindle/],["Windows 3.11",/Win16/],["Windows 95",/(Windows 95)|(Win95)|(Windows_95)/],["Windows 98",/(Windows 98)|(Win98)/],["Windows 2000",/(Windows NT 5.0)|(Windows 2000)/],["Windows XP",/(Windows NT 5.1)|(Windows XP)/],["Windows Server 2003",/(Windows NT 5.2)/],["Windows Vista",/(Windows NT 6.0)/],["Windows 7",/(Windows NT 6.1)/],["Windows 8",/(Windows NT 6.2)/],["Windows 8.1",/(Windows NT 6.3)/],["Windows 10",/(Windows NT 10.0)/],["Windows ME",/Windows ME/],["Windows CE",/Windows CE|WinCE|Microsoft Pocket Internet Explorer/],["Open BSD",/OpenBSD/],["Sun OS",/SunOS/],["Chrome OS",/CrOS/],["Linux",/(Linux)|(X11)/],["Mac OS",/(Mac_PowerPC)|(Macintosh)/],["QNX",/QNX/],["BeOS",/BeOS/],["OS/2",/OS\/2/]];function p(e){return e?b(e):"undefined"==typeof document&&"undefined"!=typeof navigator&&"ReactNative"===navigator.product?new s:"undefined"!=typeof navigator?b(navigator.userAgent):v()}function m(e){return""!==e&&u.reduce((function(t,o){var n=o[0],r=o[1];if(t)return t;var i=r.exec(e);return!!i&&[n,i]}),!1)}function h(e){var t=m(e);return t?t[0]:null}function b(e){var t=m(e);if(!t)return null;var o=t[0],i=t[1];if("searchbot"===o)return new l;var s=i[1]&&i[1].split(".").join("_").split("_").slice(0,3);s?s.length<c&&(s=n(n([],s,!0),function(e){for(var t=[],o=0;o<e;o++)t.push("0");return t}(c-s.length),!0)):s=[];var u=s.join("."),f=y(e),p=d.exec(e);return p&&p[1]?new a(o,u,f,p[1]):new r(o,u,f)}function y(e){for(var t=0,o=f.length;t<o;t++){var n=f[t],r=n[0];if(n[1].exec(e))return r}return null}function v(){return"undefined"!=typeof process&&process.version?new i(process.version.slice(1)):null}}},t={};function o(n){var r=t[n];if(void 0!==r)return r.exports;var i=t[n]={exports:{}};return e[n].call(i.exports,i,i.exports,o),i.exports}o.d=(e,t)=>{for(var n in t)o.o(t,n)&&!o.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},o.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),o.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),o.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{"use strict";function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(e){return t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},t(e)}function n(e,o,n,r,i,a){var l='<table style="border-collapse: collapse;" width="100%;">';!0===a&&(l+="<thead>");var s=0;if("object"!==t(o[0])){l+="<tr>";for(var d=0;d<o.length;d++)l+='<th style="'+r+'" >'+o[d]+"</th>";l+="</tr>",!0===a&&(l+="</thead>");for(var c=0;c<n.length;c++){l+='<tr style="'+i+'">';for(var u=function(t){var r=n[c][Object.keys(n[c])[t]];!0===/<[a-z][\s\S]*>/i.test(r)?l+=r:l+='<td style="'+i+'" '+("csv"===e.toLowerCase()?'width="'+o[t].flex/s*100+'%;"':"")+" >"+r+"</td>"},f=0;f<Object.keys(n[c]).length-1;f++)u(f);l+="</tr>"}l+="</table>"}else{for(var p=function(e){s+="flex"in o[e]?o[e].flex:1},m=0;m<o.length;m++)p(m);l+="<tr>";for(var h=0;h<o.length;h++)l+='<th style="'+r+'" width="'+o[h].flex/s*100+'%;" >'+o[h].alias+"</th>";l+="</tr>",!0===a&&(l+="</thead>");for(var b=0;b<n.length;b++){l+='<tr style="'+i+'">';for(var y=function(t){var r=n[b][o[t].name];l+='<td style="'+i+'" '+("csv"===e.toLowerCase()?'width="'+o[t].flex/s*100+'%;"':"")+" >"+r+"</td>"},v=0;v<o.length;v++)y(v);l+="</tr>"}}return l}var r=o(468).detect;var i=function(e){return window.btoa(unescape(encodeURIComponent(e)))},a=function(e,t){return e.replace(/{(\w+)}/g,(function(e,o){return t[o]}))},l="data:application/vnd.ms-excel;base64,",s='<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head>\x3c!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--\x3e<meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>',d=o(468).detect;var c=o(213);const u=function(e,t){var o=document.createElement("iframe");return o.setAttribute("style","visibility: hidden; height: 0; width: 0; position: absolute;"),o.setAttribute("id","jsObjExporterFrameId"),o.srcdoc="</head><body>"+t+"</body></html>",o};var f=o(468).detect;function p(e){return p="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},p(e)}"undefined"!=typeof window&&(window.objectExporter=function(){var t={type:"object",headers:null,exportable:null,fileName:"export",headerStyle:"font-size:16px; font-weight:bold;",cellStyle:"font-size:14px;",sheetName:"worksheet",documentTitle:"test document title",documentTitleStyle:"color:red;",repeatHeader:!0,columnSeparator:","},o=arguments[0];if(void 0===o)throw new Error("obj2csv expects at least 1 exportable!");if("object"!==p(o))throw new Error('Unexpected argument type! Expected "object", got '+p(o));if(t.exportable=o.exportable,t.type=void 0!==o.type?o.type:t.type,t.headers=void 0!==o.headers?o.headers:t.headers,t.fileName=void 0!==o.fileName?o.fileName:t.fileName,t.headerStyle=void 0!==o.headerStyle?o.headerStyle:t.headerStyle,t.cellStyle=void 0!==o.cellStyle?o.cellStyle:t.cellStyle,t.sheetName=void 0!==o.sheetName?o.sheetName:t.sheetName,t.documentTitle=void 0!==o.documentTitle?o.documentTitle:t.documentTitle,t.documentTitleStyle=void 0!==o.documentTitleStyle?o.documentTitleStyle:t.documentTitleStyle,t.repeatHeader=void 0!==o.repeatHeader?o.repeatHeader:t.repeatHeader,t.columnSeparator=void 0!==o.columnSeparator?o.columnSeparator:t.columnSeparator,!t.exportable)throw new Error("Invalid exportable!");if(!t.type||"string"!=typeof t.type)throw new Error("Invalid exportable type! only string type is acceptable!");if(-1===["csv","xls","pdf","doc","html"].indexOf(t.type.toLowerCase()))throw new Error('Invalid exportable type. Available types are "CSV", "XLS", "pdf" and "DOC".');if("boolean"!=typeof t.repeatHeader&&void 0!==t.repeatHeader)throw new Error('Invalid value for the repeat header parameter. Available types are "true" and "false".');switch(t.type){case"csv":!function(t){!function(t,o,n,r){if(t)if("object"!==e(t[0]))o.unshift(t);else{for(var i={},a=0;a<t.length;a++)i[t[a].name]=t[a].alias;o.unshift(i)}var l=function(t,o){for(var n="object"!==e(t)?JSON.parse(t):t,r="",i=0;i<n.length;i++){var a="";for(var l in n[i])a+=n[i][l]+o;r+=(a=a.substring(0,a.length-1))+"\r\n"}return r}(JSON.stringify(o),r),s=n+".csv",d=new Blob([l],{type:"text/csv;charset=utf-8;"});if(navigator.msSaveBlob)navigator.msSaveBlob(d,s);else{var c=document.createElement("a");if(void 0!==c.download){var u=URL.createObjectURL(d);c.setAttribute("href",u),c.setAttribute("download",s),c.style.visibility="hidden",document.body.appendChild(c),c.click(),document.body.removeChild(c)}}}(t.headers,t.exportable,t.fileName,t.columnSeparator)}(t);break;case"xls":!function(e){!function(e,t,o,d,c,u,f,p){var m='<span style="'+p+'">'+f+"</span><br>",h={worksheet:u,table:m+=n("xls",e,t,d,c,!1)},b=document.createElement("a"),y=o+".xls";b.setAttribute("href",l+i(a(s,h))),b.setAttribute("download",y),b.style.visibility="hidden";var v=r();if("edge"===v.name||"ie"===v.name){if(window.navigator.msSaveBlob){var w=new Blob([m],{type:"data:application/vnd.ms-excel;"});navigator.msSaveBlob(w,y)}}else document.body.appendChild(b),b.click(),document.body.removeChild(b)}(e.headers,e.exportable,e.fileName,e.headerStyle,e.cellStyle,e.sheetName,e.documentTitle,e.documentTitleStyle)}(t);break;case"pdf":!function(e){!function(e,t,o,r,i,a,l){var s=document.createElement("iframe");s.setAttribute("style","visibility: hidden; height: 0; width: 0; position: absolute;"),s.setAttribute("id","objectExporterPrintableBodyId"),s.srcdoc="<html></html>",document.getElementsByTagName("body")[0].appendChild(s);var c=document.getElementById("objectExporterPrintableBodyId");s.onload=function(){var s=d(),u=c.contentWindow||c.contentDocument;u.document&&(u=u.document);var f='<span style="'+t+'">'+e+"</span><br>";f+=n("pdf",o,r,i,a,l),u.body.innerHTML=f;var p=document.createElement("style");p.innerHTML="",u.head.appendChild(p),c.focus(),"edge"===s.name||"ie"===s.name?c.contentWindow.document.execCommand("print",!1,null):c.contentWindow.print()}}(e.documentTitle,e.documentTitleStyle,e.headers,e.exportable,e.headerStyle,e.cellStyle,e.repeatHeader)}(t);break;case"doc":!function(e){!function(e,t,o,r,i,a){for(var l="<html><body>"+n("doc",e,t,r,i,a)+"</body></html>",s=new Uint8Array(l.length),d=0;d<l.length;d++)s[d]=l.charCodeAt(d);var u=new Blob([s],{type:"text/html"});(0,c.saveAs)(u,o+".doc")}(e.headers,e.exportable,e.fileName,e.headerStyle,e.cellStyle,e.repeatHeader)}(t);break;case"html":!function(e){var t=f(),o=document.getElementById(e.exportable);if(void 0===o)throw new Error("There is no DOM object available for the requested id.");var n=u(e,o.innerHTML);document.getElementById("jsObjExporterFrameId")&&document.getElementById("jsObjExporterFrameId").remove(),document.getElementsByTagName("body")[0].appendChild(n);var r=document.getElementById("jsObjExporterFrameId");n.onload=function(){r.focus(),"edge"===t.name||"ie"===t.name?r.contentWindow.document.execCommand("print",!1,null):r.contentWindow.print()}}(t)}})})()})(); //# sourceMappingURL=objectexporter.min.js.map