/* * * A canvas to PDF converter. Uses a mock canvas context to build a PDF document. * * Licensed under the MIT license: * http://www.opensource.org/licenses/mit-license.php * * Author: * Joshua Gould * * @license Copyright (c) 2017 Joshua Gould */ !function(t){"use strict";var e=void 0!==e?e:{};t.canvas2pdf=e;const o=new Set,i={},n=[];function r(t,e,o,i){t=t%360+360*(t<0);var n=o+(o<.5?o:1-o)*(e=isNaN(t)||isNaN(e)?0:e),r=2*o-n;return m(s(t>=240?t-240:t+120,r,n),s(t,r,n),s(t<120?t+240:t-120,r,n),i)}function s(t,e,o){return 255*(t<60?e+(o-e)*t/60:t<180?o:t<240?e+(o-e)*(240-t)/60:e)}e.setFontPath=function(t){e.fontPath=t},e.runAfterFontsLoaded=function(t){try{t()}catch(e){console.log("will retry after fonts loaded: "+e),n.push(t)}},e.addFonts=function(t){if(Object.assign(i,t),Object.keys(t).forEach((t=>o.delete(t))),!o.size){const t=[...n];n.splice(0),t.forEach(e.runAfterFontsLoaded)}};var a="\\s*([+-]?\\d+)\\s*",h="\\s*([+-]?\\d*\\.?\\d+(?:[eE][+-]?\\d+)?)\\s*",c="\\s*([+-]?\\d*\\.?\\d+(?:[eE][+-]?\\d+)?)%\\s*",p=new RegExp("^rgb\\("+[a,a,a]+"\\)$"),d=new RegExp("^rgb\\("+[c,c,c]+"\\)$"),f=new RegExp("^rgba\\("+[a,a,a,h]+"\\)$"),u=new RegExp("^rgba\\("+[c,c,c,h]+"\\)$"),l=new RegExp("^hsl\\("+[h,c,c]+"\\)$"),g=new RegExp("^hsla\\("+[h,c,c,h]+"\\)$"),m=function(t,e,o,i){return{c:t/255+" "+e/255+" "+o/255,a:i}},y=function(t){if(!t)return{c:"0 0 0",a:1};if(!t.toString().startsWith("rgb")&&!t.toString().startsWith("hsl")){var e=document.createElement("div");e.style.color=t,document.body.appendChild(e),t=window.getComputedStyle(e).color+"",document.body.removeChild(e)}var o,i=((t=t.replace(/\s/g,""))+"").trim().toLowerCase();return(o=p.exec(i))?m(o[1],o[2],o[3],1):(o=d.exec(i))?m(255*o[1]/100,255*o[2]/100,255*o[3]/100,1):(o=f.exec(i))?m(o[1],o[2],o[3],o[4]):(o=u.exec(i))?m(255*o[1]/100,255*o[2]/100,255*o[3]/100,o[4]):(o=l.exec(i))?r(o[1],o[2]/100,o[3]/100):(o=g.exec(i))?r(o[1],o[2]/100,o[3]/100,o[4]):{c:t,a:1}}; /* @license MIT LICENSE Copyright (c) 2014 Devon Govett Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */function x(){this.objects=[],this.catalog=this.add(new P),this.pages=this.add(new b),this.catalog.setPages(this.pages),this.pageWidth=8.27,this.pageHeight=11.69,this.textStyle=new A,this.fonts=[],this.lineWidth=1,this.lineEndType=0,this.alpha=1,e.useFlateDecode=!!window.pako||!!window.fflate,e.deflate=function(t){var e=t;if(window.pako)return window.pako.deflate(e);if(window.fflate){if("string"==typeof e)e=(new TextEncoder).encode(e);return window.fflate.zlibSync(e)}return e}}function P(){}function b(){this.pages=[]}function C(t,e,o,i){this.pdf=t,this.fonts=[],this.images=[],this.fillImages=[],this.alphas=[],this.fillColor="0 0 0 ",this.strokeColor="0 0 0 ",this.lineWidth=2,this.lineCap=0,this.lineJoin=1,this.fontSize=12,this.font="normal",this.width=72*e,this.height=72*o,this.parentPagesList=i,this._ctm=[1,0,0,1,0,0]}function v(t){const e=t.unicodes.map(((t,e)=>`<${O.toHex(e)}><${O.toHex(t)}>`)).join("\n");this.stream=`/CIDInit /ProcSet findresource begin\n 10 dict begin\n begincmap\n /CIDSystemInfo\n << /Registry (Adobe)\n /Ordering (UCS)\n /Supplement 0\n >> def\n /CMapName /Adobe-Identity-UCS def\n /CMapType 2 def\n 1 begincodespacerange\n <0000> \n endcodespacerange\n ${t.unicodes.length} beginbfchar\n ${e}\n endbfchar\n endcmap\n CMapName currentdict /CMap defineresource pop\n end\n end`}function T(){}function S(){}function w(t){this.buffer=atob(t.base64)}function F(t){this.fontName=t,this.descendantFonts=[],this.subtype="Type1",this.encoding="WinAnsiEncoding"}function I(){this.stream=""}e.PdfContext=function(t,e,o){var i=this,n=new x;this.doc=n;var r=document.createElement("canvas");this.context=r.getContext("2d"),n.pageSetWidth(t/72),n.pageSetHeight(e/72),n.addPage(o);var s="10px Helvetica";this.textAlign="left",this.textBaseline="alphabetic";Object.defineProperty(this,"fillStyle",{get:function(){return this.fillColor},set:function(t){if(this.fillColor=t,t instanceof k)console.log("TODO",i.doc);else if(t.toString().startsWith("rgb")||t.toString().startsWith("hsl")){var e=y(t);i.doc.fillColor(e.c,e.a)}}}),Object.defineProperty(this,"strokeStyle",{get:function(){return i.doc.strokeColor()},set:function(t){var e=y(t);i.doc.strokeColor(e.c,e.a)}}),Object.defineProperty(this,"lineWidth",{get:function(){return i.doc.setLineWidth()},set:function(t){i.doc.setLineWidth(t)}}),Object.defineProperty(this,"lineCap",{get:function(){return"butt"},set:function(t){i.doc.lineCap(t)}}),Object.defineProperty(this,"lineJoin",{get:function(){return"miter"},set:function(t){i.doc.lineJoin(t)}}),Object.defineProperty(this,"miterLimit",{get:function(){return 10},set:function(){}}),Object.defineProperty(this,"globalAlpha",{get:function(){return i.doc.opacity()},set:function(t){i.doc.opacity(t)}}),Object.defineProperty(this,"font",{get:function(){return s},set:function(t){this.context.font=t,s=t;var e,o,n=((o=(e=/^\s*(?=(?:(?:[-a-z]+\s*){0,2}(italic|oblique))?)(?=(?:(?:[-a-z]+\s*){0,2}(small-caps))?)(?=(?:(?:[-a-z]+\s*){0,2}(bold(?:er)?|lighter|[1-9]00))?)(?:(?:normal|\1|\2|\3)\s*){0,3}((?:xx?-)?(?:small|large)|medium|smaller|larger|[.\d]+(?:\%|in|[cem]m|ex|p[ctx]))(?:\s*\/\s*(normal|[.\d]+(?:\%|in|[cem]m|ex|p[ctx])))?\s*([-_,\'\"\sa-z0-9]+?)\s*$/i).exec(i.font))||(console.log("error parsing font "+i.font),o=e.exec("10px Helvetica")),o[6].indexOf("sans-serif")>-1&&(o[6]="Helvetica"),o[6].indexOf("serif")>-1&&(o[6]="Times-Roman"),{style:o[1]||"normal",size:parseInt(o[4])||10,family:o[6]||"Helvetica",weight:o[3]||"normal"});i.doc.fontSize(n.size),i.doc.font(n.family),i.doc.setFontStyle("normal"!=n.weight,"normal"!=n.style,!1)}}),this.font=s,this.strokeStyle="rgb(0,0,0)",this.fillStyle="rgb(0,0,0)"},e.PdfContext.prototype.end=function(){this.doc.end()},e.PdfContext.prototype.addPage=function(t){this.doc.addPage(t)},e.PdfContext.prototype.save=function(){this.doc.save()},e.PdfContext.prototype.restore=function(){this.doc.restore()},e.PdfContext.prototype.transform=function(t,e,o,i,n,r){this.doc.transform(t,e,o,i,n,r)},e.PdfContext.prototype.scale=function(t,e){this.doc.scale(t,e)},e.PdfContext.prototype.rotate=function(t){var e=180*t/Math.PI;this.doc.rotate(e)},e.PdfContext.prototype.translate=function(t,e){this.doc.translate(t,e)},e.PdfContext.prototype.beginPath=function(){this.doc.beginPath()},e.PdfContext.prototype.moveTo=function(t,e){this.doc.moveTo(t,e)},e.PdfContext.prototype.closePath=function(){this.doc.closePath()},e.PdfContext.prototype.lineTo=function(t,e){this.doc.lineTo(t,e)},e.PdfContext.prototype.stroke=function(){this.doc.stroke()},e.PdfContext.prototype.fill=function(t){this.doc.fill(t)},e.PdfContext.prototype.rect=function(t,e,o,i){this.doc.rect(t,e,o,i)},e.PdfContext.prototype.fillRect=function(t,e,o,i){this.doc.beginPath(),this.doc.rect(t,e,o,i),this.doc.fill()},e.PdfContext.prototype.strokeRect=function(t,e,o,i){this.doc.beginPath(),this.doc.rect(t,e,o,i),this.doc.stroke()},e.PdfContext.prototype.clearRect=function(t,e,o,i){var n=this.doc.fillColor();this.doc.fillColor("white"),this.doc.rect(t,e,o,i),this.doc.fill(),this.doc.fillColor(n)},e.PdfContext.prototype.arc=function(t,e,o,i,n,r){this.doc.arc(t,e,o,i,n,r)},e.PdfContext.prototype.bezierCurveTo=function(t,e,o,i,n,r){this.doc.bezierCurveTo(t,e,o,i,n,r)},e.PdfContext.prototype.quadraticCurveTo=function(t,e,o,i){this.doc.quadraticCurveTo(t,e,o,i)},e.PdfContext.prototype.createLinearGradient=function(t,e,o,i){return this.doc.linearGradient(t,e,o,i)},e.PdfContext.prototype.createRadialGradient=function(t,e,o,i,n,r){var s=this.doc.radialGradient(t,e,o,i,n,r);return s.addColorStop=function(t,e){var o=y(e);s.stop(t,o.c,o.a)},s},e.PdfContext.prototype.fillText=function(t,e,o){t&&t.trim().length&&this.doc.textAdd(e,o,t)},e.PdfContext.prototype.strokeText=function(){console.log("strokeText not implemented, use fillText")},e.PdfContext.prototype.measureText=function(t){return this.context.measureText(t+"")},e.PdfContext.prototype.clip=function(){this.doc.clip()},e.PdfContext.prototype.getPDFbase64=function(){return this.doc.getBase64Text()},e.PdfContext.prototype.getPDFtext=function(){return this.doc.getText()},e.PdfContext.prototype.drawImage=function(t,e,o,i,n){var r=this.doc.currentPage._ctm,s=r[0]*r[3]-r[1]*r[2];return this.doc.drawImage(t,e,o,i,n,Math.sqrt(Math.abs(s)))},e.PdfContext.prototype.setLineDash=function(t){return this.doc.setLineDash(t)},e.PdfContext.prototype.createPattern=function(t){var e=this.doc.imageTileLoad(t);return{setTransform:t=>{e.matrix=t}}},e.PdfContext.prototype.setTransform=function(){console.log("setTransform not implemented")},e.PdfContext.prototype.drawFocusRing=function(){console.log("drawFocusRing not implemented")},e.PdfContext.prototype.createImageData=function(){console.log("drawFocusRing not implemented")},e.PdfContext.prototype.getImageData=function(){console.log("getImageData not implemented")},e.PdfContext.prototype.putImageData=function(){console.log("putImageData not implemented")},e.PdfContext.prototype.globalCompositeOperation=function(){console.log("globalCompositeOperation not implemented")},e.PdfContext.prototype.arcTo=function(){console.log("arcTo not implemented")},x.prototype.add=function(t){return this.objects.push(t),t.id=this.objects.length,t},x.prototype.pageSetWidth=function(t){this.pageWidth=t},x.prototype.pageSetHeight=function(t){this.pageHeight=t},x.prototype.addPage=function(t){this.currentPage=new C(this,this.pageWidth,this.pageHeight,this.pages),this.currentPage.dpi=t?.dpi||72,this.add(this.currentPage),this.pages.addPage(this.currentPage);var e=new I;this.add(e),this.currentPage.setStream(e),t&&t.verticalFlip||this.currentPage.transform(1,0,0,-1,0,this.currentPage.height)},x.prototype.font=function(t){this.currentPage.setFontName(t)},x.prototype.fontSize=function(t){this.currentPage.setFontSize(t)},x.prototype.lineJoin=function(t){this.currentPage.setLineJoin(t)},x.prototype.lineCap=function(t){this.currentPage.setLineCap(t)},x.prototype.setLineWidth=function(t){this.currentPage.setLineWidth(t)},x.prototype.strokeColor=function(t,e){this.currentPage.setStrokeColor(t,e)},x.prototype.fillColor=function(t,e){this.currentPage.setFillColor(t,e)},x.prototype.pageSetCurrent=function(t){this.currentPage=t},x.prototype.font=function(t){this.textStyle.setFontName(t)},x.prototype.fontSetSize=function(t){this.textStyle.fontSetSize(t)},x.prototype.setFontStyle=function(t,e){this.textStyle.setFontStyle(t,e)},x.prototype.setFont=function(t){var n=t.getFontName();if(null!=n){var r,s=null;for(r=0;rt.charCodeAt(0)>127)))&&(i.setFontName(i.fontName.startsWith("Times")?"RobotoSerif":"Roboto"),this.currentPage.isUnicode=!0),this.setFont(i),this.currentPage.textAdd(t,e,o,i)},x.prototype.imageLoadFromCanvas=function(t,e){(t=new R(t)).scale=e,this.add(t),t.mask&&this.add(t.mask),this.currentImage=t},x.prototype.imageTileLoadFromCanvas=function(t){return(t=new M(t,this.currentPage.fillColor,1*this.currentPage.alpha)).vectorPattern&&this.add(t.vectorPattern),this.add(t),this.currentPage.currentImageTile=t,t},x.prototype.linearGradient=function(t,e,o,i){var n=new k(t,e,o,i,this.currentPage);return this.add(n),this.currentPage.currentImageTile=n,this.addPatternToPage(n),n},x.prototype.doDrawImage=function(t,e,o,i){this.currentPage.drawImage(t,e,this.currentImage,this.alpha,o,i)},x.prototype.addPatternToPage=function(t){this.currentPage.addPatternToPage(t)},x.prototype.moveTo=function(t,e){this.currentPage.moveTo(t,e)},x.prototype.lineTo=function(t,e){this.currentPage.lineTo(t,e)},x.prototype.bezierCurveTo=function(t,e,o,i,n,r){this.currentPage.bezierCurveTo(t,e,o,i,n,r)},x.prototype.translate=function(t,e){this.currentPage.translate(t,e)},x.prototype.transform=function(t,e,o,i,n,r){this.currentPage.transform(t,e,o,i,n,r)},x.prototype.drawImage=function(t,e,o,i,n,r){var s=1;if("img"==t.nodeName.toLowerCase()){var a=document.createElement("canvas");t.src.startsWith&&t.src.startsWith("data:image/svg")&&(s=2*Math.max(1,r||1)*this.currentPage.dpi/72),a.width=t.width*s,a.height=t.height*s,a.getContext("2d").drawImage(t,0,0,t.width,t.height,0,0,a.width,a.height),t=a}this.imageLoadFromCanvas(t,s),this.doDrawImage(e,o,t.width,t.height)},x.prototype.imageTileLoad=function(t){if(t.nodeName&&"img"==t.nodeName.toLowerCase()){var e=document.createElement("canvas");e.width=t.width,e.height=t.height,e.getContext("2d").drawImage(t,0,0),t=e}var o=this.imageTileLoadFromCanvas(t);return this.addPatternToPage(this.currentPage.currentImageTile),o},x.prototype.scale=function(t,e,o){this.currentPage.scale(t,e,o)},x.prototype.rotate=function(t,e){this.currentPage.rotate(t,e)},x.prototype.beginPath=function(){this.currentPage.beginPath()},x.prototype.arc=function(t,e,o,i,n,r){this.currentPage.arc(t,e,o,i,n,r)},x.prototype.closePath=function(){this.currentPage.closePath()},x.prototype.stroke=function(){this.currentPage.stroke()},x.prototype.clip=function(t){this.currentPage.clip(t)},x.prototype.opacity=function(t){if(void 0===t)return this.currentPage.currentAlpha;this.currentPage.setAlpha(t)},x.prototype.setLineDash=function(t){this.currentPage.setLineDash(t)},x.prototype.save=function(){this.currentPage.saveContext()},x.prototype.restore=function(){this.currentPage.restoreContext()},x.prototype.fill=function(t){this.currentPage.fill(t)},x.prototype.rect=function(t,e,o,i){this.currentPage.rect(t,e,o,i)},x.prototype.quadraticCurveTo=function(t,e,o,i){this.currentPage.quadraticCurveTo(t,e,o,i)},x.prototype.graphicsSetAlpha=function(t){this.alpha=Math.floor(100*t/255)/100,this.textStyle.setAlpha(t)},x.prototype.graphicsSetLineEndType=function(t){this.lineEndType=t},x.prototype._write=function(t){this.stream+=t+"\n"},x.prototype.getObject=function(t){var e,o;for(this.stream="",this._write("%PDF-1.7"),e=0;e0)for(var i=0;i0)for(i=0;i0)for(var h=0;h0)for(h=0;h0&&(e.Resources.Font=o),this.alphas.length>0&&(e.Resources.ExtGState=r),this.fillImages.length>0&&(e.Resources.Pattern=c),this.images.length>0&&(e.Resources.XObject=a),O.makeObject(e,this.id)},v.prototype.getObject=function(){var t=D(this.stream),e={Length:t.length};return O.makeObject(e,this.id,t)},T.prototype.getObject=function(){const t={Supplement:0,Registry:new j("(Adobe)"),Ordering:new j("(Identity)")};return O.makeObject(t,this.id)},S.prototype.getObject=function(){const t={CapHeight:939,StemV:56,FontBBox:this.spec.bbox,FontFile2:j.of(this.fontFile),Descent:this.spec.bbox[2],Type:"FontDescriptor",Flags:32,FontName:"Unicode",ItalicAngle:0,Ascent:this.spec.bbox[3]};return O.makeObject(t,this.id)},w.prototype.getObject=function(){const t={Length:this.buffer.length,Length1:this.buffer.length};return O.makeObject(t,this.id,this.buffer)},F.TIMES=["Times-Roman","Times-Italic","Times-Bold","Times-BoldItalic"],F.HELVETICA=["Helvetica","Helvetica-Oblique","Helvetica-Bold","Helvetica-BoldOblique"],F.ROBOTO=["Roboto-Regular","Roboto-Italic","Roboto-Bold","Roboto-BoldItalic"],F.ROBOTO_SERIF=["RobotoSerif-Regular","RobotoSerif-Italic","RobotoSerif-Bold","RobotoSerif-BoldItalic"],F.getPDFName=function(t,e,o){var i=(e?2:0)+(o?1:0);return"Helvetica"==t?F.HELVETICA[i]:"Roboto"==t?F.ROBOTO[i]:"RobotoSerif"==t?F.ROBOTO_SERIF[i]:F.TIMES[i]},F.prototype.getObject=function(){var t={Subtype:this.subtype,Name:"F"+this.id,BaseFont:this.baseFont||this.fontName,Encoding:this.encoding,Type:"Font",DescendantFonts:this.descendantFonts.map((t=>j.of(t)))};if(this.toUnicode&&(t.ToUnicode=j.of(this.toUnicode)),this.systemInfo&&(t.CIDSystemInfo=j.of(this.systemInfo)),this.fontDescriptor&&(t.FontDescriptor=j.of(this.fontDescriptor)),this.cidToGidMap){t.FontMatrix=this.spec.matrix,t.FontBBox=this.spec.bbox;const e=this.spec.matrix[0]/.001;t.W=new j("["+this.spec.widths.map(((t,o)=>t&&o?`${o} [${Math.round(t*e)}]`:"")).join("")+"]"),t.CIDToGIDMap=this.cidToGidMap}return O.makeObject(t,this.id)},I.prototype.addText=function(t){this.stream+=t},I.prototype.addTextCheckMerge=function(t){function e(t,e){return-1!==t.indexOf(e,t.length-e.length)}"f* "==t&&e(this.stream," S ")?this.stream=this.stream.substring(0,this.stream.length-3)+" B* ":"S "==t&&e(this.stream," f* ")?this.stream=this.stream.substring(0,this.stream.length-4)+" B* ":"f "==t&&e(this.stream," S ")||"S "==t&&e(this.stream," f ")?this.stream=this.stream.substring(0,this.stream.length-3)+" B ":this.stream+=t},I.prototype.replaceText=function(t,e){this.stream=this.stream.replace(t,e)},I.prototype.getObject=function(){var t=D(this.stream),o={Length:t.length};return e.useFlateDecode&&(o.Filter="FlateDecode"),O.makeObject(o,this.id,t)};var O,j,D=function(t){if(e.useFlateDecode)t=e.deflate(t);else if("string"==typeof t)return t;for(var o=[],i=0;it*(1-i)+i)),a=new Uint8Array(this.width*this.height*3),h=0,c=this.height-1;c>=0;c--)for(var p=0;pMath.round(parseFloat(t)/255*l+s[e]*(255-l)))),a[h++]=d,a[h++]=f,a[h++]=u}e.useFlateDecode&&n.push("/Filter /FlateDecode "),this.stream=n.join("")+"ID\n"+D(a)+"\nEI\n"}else{var g=t.doc.currentPage;this.boundingBox=t.boundingBox||[0,0,72*g.pdf.pageWidth,72*g.pdf.pageHeight],this.alphas=g.alphas,this.vectorPattern=new B(g.pdfStream.stream,this.boundingBox)}}function A(){this.fontName="Helvetica",this.fontSize=12,this.italic=!1,this.bold=!1,this.font=void 0,this.alpha=1}R.prototype.loadData=function(t){for(var e=t.getContext("2d"),o=new Uint8Array(this.height*this.width*3),i=e.getImageData(0,0,this.width,this.height),n=0,r=new Uint8Array(this.height*this.width),s=0,a=!1,h=0;ht.charCodeAt(0)>127))){var l="";for(a=f=0,u=p.length;f"}return"("+(p=n.replace(e,(function(e){return t[e]})))+")"}if(n instanceof j)return n.toString();if(n instanceof Date)return"(D:"+o(n.getUTCFullYear(),4)+o(n.getUTCMonth()+1,2)+o(n.getUTCDate(),2)+o(n.getUTCHours(),2)+o(n.getUTCMinutes(),2)+o(n.getUTCSeconds(),2)+"Z)";if(Array.isArray(n))return"["+function(){var t,e,o;for(o=[],t=0,e=n.length;t>"),c.join("")+"\n"}return""+n},i}(),j=function(){function t(t){this.str=t}return t.prototype.toString=function(){return this.str},t.of=function(e){return new t(e.id+" 0 R")},t}()}("undefined"!=typeof window?window:this);