forked from sharonchoong/svg-exportJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg-export.min.js
1 lines (1 loc) · 7.15 KB
/
svg-export.min.js
1
(function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):(t="undefined"!=typeof globalThis?globalThis:t||self,e(t.svgExport=t.svgExport||{}))})(this,function(t){"use strict";function e(t){var e=document.createElement("div");if(e.className="tempdiv-svg-exportJS","string"==typeof t&&(e.insertAdjacentHTML("beforeend",t.trim()),t=e.firstChild),!t.nodeType||1!==t.nodeType)return null;var n=t.cloneNode(!0);return n.style.display=null,e.appendChild(n),e.style.visibility="hidden",e.style.display="table",e.style.position="absolute",document.body.appendChild(e),n}function n(t){t&&t.pdfOptions&&(Object.keys(h.pdfOptions).forEach(function(e){if(t.pdfOptions.hasOwnProperty(e)&&typeof t.pdfOptions[e]==typeof h.pdfOptions[e]){if(""===t.pdfOptions[e])return;h.pdfOptions[e]=t.pdfOptions[e]}}),h.pdfOptions.pageLayout.margin||(h.pdfOptions.pageLayout.margin=50),h.pdfOptions.pageLayout.margins||(h.pdfOptions.pageLayout.margins={})),h.pdfOptions.pageLayout.margins.top=h.pdfOptions.pageLayout.margins.top||h.pdfOptions.pageLayout.margin,h.pdfOptions.pageLayout.margins.bottom=h.pdfOptions.pageLayout.margins.bottom||h.pdfOptions.pageLayout.margin,h.pdfOptions.pageLayout.margins.left=h.pdfOptions.pageLayout.margins.left||h.pdfOptions.pageLayout.margin,h.pdfOptions.pageLayout.margins.right=h.pdfOptions.pageLayout.margins.top||h.pdfOptions.pageLayout.margin,delete h.pdfOptions.pageLayout.margin,t&&h.pdfOptions.pageLayout.size||(h.pdfOptions.pageLayout.size=[Math.max(300,h.width)+h.pdfOptions.pageLayout.margins.left+h.pdfOptions.pageLayout.margins.right,Math.max(300,h.height)+h.pdfOptions.pageLayout.margins.top+h.pdfOptions.pageLayout.margins.bottom+(h.pdfOptions.addTitleToPage?2*h.pdfOptions.pdfTitleFontSize+10:0)+(""!==h.pdfOptions.chartCaption?4*h.pdfOptions.pdfCaptionFontSize+10:0)])}function i(t,e){h={originalWidth:100,originalHeight:100,width:100,height:100,scale:1,useCSS:!0,transparentBackgroundReplace:"white",pdfOptions:{customFonts:[],pageLayout:{margin:50,margins:{}},addTitleToPage:!0,chartCaption:"",pdfTextFontFamily:"Helvetica",pdfTitleFontSize:20,pdfCaptionFontSize:14}},h.originalHeight=-1!==t.style.getPropertyValue("height").indexOf("%")||t.getAttribute("height")&&-1!==t.getAttribute("height").indexOf("%")?t.getBBox().height*h.scale:t.getBoundingClientRect().height*h.scale,h.originalWidth=-1!==t.style.getPropertyValue("width").indexOf("%")||t.getAttribute("width")&&-1!==t.getAttribute("width").indexOf("%")?t.getBBox().width*h.scale:t.getBoundingClientRect().width*h.scale,e&&e.scale&&"number"==typeof e.scale&&(h.scale=e.scale),e&&e.height?"number"==typeof e.height&&(h.height=e.height*h.scale):h.height=h.originalHeight*h.scale,e&&e.width?"number"==typeof e.width&&(h.width=e.width*h.scale):h.width=h.originalWidth*h.scale,e&&!1===e.useCSS&&(h.useCSS=!1),e&&e.transparentBackgroundReplace&&(h.transparentBackgroundReplace=e.transparentBackgroundReplace),n(e)}function o(t,e){if("function"==typeof getComputedStyle){t.childNodes.forEach(function(t,n){1===t.nodeType&&o(t,e.childNodes[parseInt(n,10)])});var n=window.getComputedStyle(t);if(n.length>0)for(const t of n)-1===["width","height","inline-size","block-size"].indexOf(t)&&e.style.setProperty(t,n.getPropertyValue(t))}}function a(t,e,n){void 0===n&&(n=!0),h.useCSS&&"object"==typeof e&&(o(e,t),t.style.display=null),t.style.width=null,t.style.height=null,t.setAttribute("width",h.width),t.setAttribute("height",h.height),t.setAttribute("preserveAspectRatio","none"),t.setAttribute("viewBox","0 0 "+h.originalWidth+" "+h.originalHeight);for(var i=document.getElementsByClassName("tempdiv-svg-exportJS");i.length>0;)i[0].parentNode.removeChild(i[0]);if(n){var a=new XMLSerializer,p=a.serializeToString(t).replace(/currentColor/g,"black");return p.match(/^<svg[^>]+xmlns="http\:\/\/www\.w3\.org\/2000\/svg"/)||(p=p.replace(/^<svg/,'<svg xmlns="http://www.w3.org/2000/svg"')),p.match(/^<svg[^>]+"http\:\/\/www\.w3\.org\/1999\/xlink"/)||(p=p.replace(/^<svg/,'<svg xmlns:xlink="http://www.w3.org/1999/xlink"')),p}return t}function p(t){var e=[];return t.forEach(function(t){var n=new Promise(function(e,n){var i=new XMLHttpRequest;i.onreadystatechange=function(){4===i.readyState&&200===i.status&&e(i.response)},i.open("GET",t,!0),i.responseType="arraybuffer",i.send(null)});e.push(n)}),e}function s(t,e,n){if(e=e.replace(/[/\\?%*:|"<>]/g,"_"),navigator.msSaveBlob){for(var i=decodeURIComponent(t.split(",")[1]),o=[],a=t.split(",")[0].split(":")[1].split(";")[0],p=0;p<i.length;p++)o.push(i.charCodeAt(p));var s=null;return s=null!=n?n.msToBlob():new Blob([new Uint8Array(o)],{type:a}),navigator.msSaveBlob(s,e)}var r=document.createElement("a");r.download=e,r.href=t,document.body.appendChild(r),r.click(),document.body.removeChild(r)}function r(t,n,o){var p=e(t);if(p){null==n&&(n="chart"),i(p,o);var r=a(p,t);r='<?xml version="1.0" standalone="no"?>\r\n'+r;var d="data:image/svg+xml;charset=utf-8,"+encodeURIComponent(r);s(d,n+".svg")}}function d(t,n,o,p){if("object"==typeof canvg){p=p.toLowerCase().replace("jpg","jpeg"),"png"!==p&&"jpeg"!==p&&(p="png");var r=e(t);if(r){null==n&&(n="chart");var d=document.createElement("canvas");o&&(o.width||o.height)||(o||(o={}),o.scale=10),i(r,o);var f=a(r,t);"jpeg"===p&&(f=f.replace(">",'><rect x="0" y="0" width="'+h.width+'" height="'+h.height+'" fill="'+h.transparentBackgroundReplace+'"/>'));var g=d.getContext("2d");canvg.Canvg.fromString(g,f).start();var l=d.toDataURL("image/"+p);s(l,n+"."+p,d)}}}function f(t,e,n){d(t,e,n,"png")}function g(t,e,n){d(t,e,n,"jpeg")}function l(t,e,n){h.pdfOptions.addTitleToPage&&t.font(h.pdfOptions.pdfTextFontFamily).fontSize(h.pdfOptions.pdfTitleFontSize).text(e,{width:h.pdfOptions.pageLayout.size[0]-h.pdfOptions.pageLayout.margins.left-h.pdfOptions.pageLayout.margins.right}),SVGtoPDF(t,n,h.pdfOptions.pageLayout.margins.left,t.y+10,{width:h.width,height:h.height,preserveAspectRatio:"none",useCSS:h.useCSS}),""!==h.pdfOptions.chartCaption&&t.font(h.pdfOptions.pdfTextFontFamily).fontSize(h.pdfOptions.pdfCaptionFontSize).text(h.pdfOptions.chartCaption,h.pdfOptions.pageLayout.margins.left,h.pdfOptions.pageLayout.size[1]-h.pdfOptions.pageLayout.margins.bottom-4*h.pdfOptions.pdfCaptionFontSize,{width:h.pdfOptions.pageLayout.size[0]-h.pdfOptions.pageLayout.margins.left-h.pdfOptions.pageLayout.margins.right})}function u(t,n,o){if("function"==typeof PDFDocument&&"function"==typeof SVGtoPDF&&"function"==typeof blobStream){var r=e(t);if(r){null==n&&(n="chart"),i(r,o);var d=a(r,t,!1),f=new PDFDocument(h.pdfOptions.pageLayout),g=f.pipe(blobStream());if(h.pdfOptions.customFonts.length>0){var u=p(h.pdfOptions.customFonts.map(function(t){return t.url}));Promise.all(u).then(function(t){t.forEach(function(t,e){var n=h.pdfOptions.customFonts[parseInt(e,10)],i=d.querySelectorAll('[style*="'+n.fontName+'"]');i.forEach(function(t){t.style.fontFamily=n.fontName}),-1===n.url.indexOf(".ttc")&&-1===n.url.indexOf(".dfont")||!n.styleName?f.registerFont(n.fontName,t):f.registerFont(n.fontName,t,n.styleName)}),l(f,n,d),f.end()})}else l(f,n,d),f.end();g.on("finish",function(){var t=g.toBlobURL("application/pdf");s(t,n+".pdf")})}}}var c="1.0.0",h={};t.version=c,t.downloadSvg=r,t.downloadPng=f,t.downloadJpeg=g,t.downloadPdf=u,Object.defineProperty(t,"__esModule",{value:!0})});