forked from RavishaHesh/PDFJsAnnotations
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pdfannotate.min.js
1 lines (1 loc) · 5.97 KB
/
pdfannotate.min.js
1
var PDFAnnotate=function(t,e,n={}){this.number_of_pages=0,this.pages_rendered=0,this.active_tool=1,this.fabricObjects=[],this.fabricObjectsData=[],this.color="#212121",this.borderColor="#000000",this.borderSize=1,this.font_size=16,this.active_canvas=0,this.container_id=t,this.url=e,this.pageImageCompression=n.pageImageCompression?n.pageImageCompression.toUpperCase():"NONE",this.textBoxText="Sample Text",this.format,this.orientation;var a=this;pdfjsLib.getDocument(this.url).promise.then((function(t){var e=n.scale?n.scale:1.3;a.number_of_pages=t.numPages;for(var o=1;o<=t.numPages;o++)t.getPage(o).then((function(t){if(void 0===a.format||void 0===a.orientation){var n=t.getViewport({scale:1});a.format=[n.width,n.height],a.orientation=n.width>n.height?"landscape":"portrait"}var o=t.getViewport({scale:e}),i=document.createElement("canvas");document.getElementById(a.container_id).appendChild(i),i.className="pdf-canvas",i.height=o.height,i.width=o.width,context=i.getContext("2d");var r={canvasContext:context,viewport:o};t.render(r).promise.then((function(){$(".pdf-canvas").each((function(t,e){$(e).attr("id","page-"+(t+1)+"-canvas")})),a.pages_rendered++,a.pages_rendered==a.number_of_pages&&a.initFabric()}))}))}),(function(t){})),this.initFabric=function(){var t=this;let e=$("#"+t.container_id+" canvas");e.each((function(a,o){var i=o.toDataURL("image/png"),r=new fabric.Canvas(o.id,{freeDrawingBrush:{width:1,color:t.color}});t.fabricObjects.push(r),"function"==typeof n.onPageUpdated&&r.on("object:added",(function(){var e=Object.assign({},t.fabricObjectsData[a]);t.fabricObjectsData[a]=r.toJSON(),n.onPageUpdated(a+1,e,t.fabricObjectsData[a])})),r.setBackgroundImage(i,r.renderAll.bind(r)),$(r.upperCanvasEl).click((function(e){t.active_canvas=a,t.fabricClickHandler(e,r)})),r.on("after:render",(function(){t.fabricObjectsData[a]=r.toJSON(),r.off("after:render")})),a===e.length-1&&"function"==typeof n.ready&&n.ready()}))},this.fabricClickHandler=function(t,e){var n,a=this;2==a.active_tool?n=new fabric.IText(a.textBoxText,{left:t.clientX-e.upperCanvasEl.getBoundingClientRect().left,top:t.clientY-e.upperCanvasEl.getBoundingClientRect().top,fill:a.color,fontSize:a.font_size,selectable:!0}):4==a.active_tool&&(n=new fabric.Rect({left:t.clientX-e.upperCanvasEl.getBoundingClientRect().left,top:t.clientY-e.upperCanvasEl.getBoundingClientRect().top,width:100,height:100,fill:a.color,stroke:a.borderColor,strokeSize:a.borderSize})),n&&e.add(n)}};PDFAnnotate.prototype.enableSelector=function(){var t=this;t.active_tool=0,t.fabricObjects.length>0&&$.each(t.fabricObjects,(function(t,e){e.isDrawingMode=!1}))},PDFAnnotate.prototype.enablePencil=function(){var t=this;t.active_tool=1,t.fabricObjects.length>0&&$.each(t.fabricObjects,(function(t,e){e.isDrawingMode=!0}))},PDFAnnotate.prototype.enableAddText=function(t){var e=this;e.active_tool=2,"string"==typeof t&&(e.textBoxText=t),e.fabricObjects.length>0&&$.each(e.fabricObjects,(function(t,e){e.isDrawingMode=!1}))},PDFAnnotate.prototype.enableRectangle=function(){var t=this;t.fabricObjects[t.active_canvas];t.active_tool=4,t.fabricObjects.length>0&&$.each(t.fabricObjects,(function(t,e){e.isDrawingMode=!1}))},PDFAnnotate.prototype.enableAddArrow=function(t=null){var e=this;e.active_tool=3,e.fabricObjects.length>0&&$.each(e.fabricObjects,(function(n,a){a.isDrawingMode=!1,new Arrow(a,e.color,(function(){e.active_tool=0,"function"==typeof t&&t()}))}))},PDFAnnotate.prototype.addImageToCanvas=function(){var t=this.fabricObjects[this.active_canvas];if(t){var e=document.createElement("input");e.type="file",e.accept=".jpg,.jpeg,.png,.PNG,.JPG,.JPEG",e.onchange=function(){var n=new FileReader;n.addEventListener("load",(function(){e.remove();var n=new Image;n.onload=function(){t.add(new fabric.Image(n))},n.src=this.result}),!1),n.readAsDataURL(e.files[0])},document.getElementsByTagName("body")[0].appendChild(e),e.click()}},PDFAnnotate.prototype.deleteSelectedObject=function(){var t=this,e=t.fabricObjects[t.active_canvas].getActiveObject();e&&confirm("Are you sure ?")&&t.fabricObjects[t.active_canvas].remove(e)},PDFAnnotate.prototype.savePdf=function(t){var e=this,n=e.format||"a4",a=e.orientation||"portrait";if(e.fabricObjects.length){var o=new jspdf.jsPDF({format:n,orientation:a});void 0===t&&(t=`${(new Date).getTime()}.pdf`),e.fabricObjects.forEach((function(i,r){0!=r&&(o.addPage(n,a),o.setPage(r+1)),o.addImage(i.toDataURL({format:"png"}),"NONE"==e.pageImageCompression?"PNG":"JPEG",0,0,o.internal.pageSize.getWidth(),o.internal.pageSize.getHeight(),`page-${r+1}`,["FAST","MEDIUM","SLOW"].indexOf(e.pageImageCompression)>=0?e.pageImageCompression:void 0),r===e.fabricObjects.length-1&&o.save(t)}))}},PDFAnnotate.prototype.setBrushSize=function(t){$.each(this.fabricObjects,(function(e,n){n.freeDrawingBrush.width=parseInt(t,10)||1}))},PDFAnnotate.prototype.setColor=function(t){this.color=t,$.each(this.fabricObjects,(function(e,n){n.freeDrawingBrush.color=t}))},PDFAnnotate.prototype.setBorderColor=function(t){this.borderColor=t},PDFAnnotate.prototype.setFontSize=function(t){this.font_size=t},PDFAnnotate.prototype.setBorderSize=function(t){this.borderSize=t},PDFAnnotate.prototype.clearActivePage=function(){var t=this.fabricObjects[this.active_canvas],e=t.backgroundImage;confirm("Are you sure?")&&(t.clear(),t.setBackgroundImage(e,t.renderAll.bind(t)))},PDFAnnotate.prototype.serializePdf=function(t){var e=this,n=[];e.fabricObjects.forEach((function(a){a.clone((function(a){if(a.setBackgroundImage(null),a.setBackgroundColor(""),n.push(a),n.length===e.fabricObjects.length){var o={page_setup:{format:e.format,orientation:e.orientation},pages:n};t(JSON.stringify(o))}}))}))},PDFAnnotate.prototype.loadFromJSON=function(t){var e=this,{page_setup:n,pages:a}=t;void 0===a&&(a=t),"object"==typeof n&&"string"==typeof n.format&&"string"==typeof n.orientation&&(e.format=n.format,e.orientation=n.orientation),$.each(e.fabricObjects,(function(t,n){a.length>t&&n.loadFromJSON(a[t],(function(){e.fabricObjectsData[t]=n.toJSON()}))}))},PDFAnnotate.prototype.setDefaultTextForTextBox=function(t){"string"==typeof t&&(this.textBoxText=t)};