[ca5e9e5] | 1 | |
---|
| 2 | var __PDF_DOC, |
---|
| 3 | __CURRENT_PAGE, |
---|
| 4 | __TOTAL_PAGES, |
---|
| 5 | __PAGE_RENDERING_IN_PROGRESS = 0, |
---|
| 6 | __CANVAS = $('#pdf-canvas').get(0), |
---|
| 7 | __CANVAS_CTX = __CANVAS.getContext('2d'), |
---|
| 8 | __id = "", |
---|
| 9 | __NUMBER = 0; |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | function cropperrr(){ |
---|
| 13 | console.log("siiiiiiiiiiiiiiiiii"); |
---|
| 14 | $("#img").cropper({ |
---|
| 15 | aspectRatio: 16 / 9, |
---|
| 16 | done: function (data) { |
---|
| 17 | console.log(data); |
---|
| 18 | $('#dataX').val(parseInt(data['x'])); |
---|
| 19 | $('#dataY').val(parseInt(data['y'])); |
---|
| 20 | $('#dataWidth').val(parseInt(data['width'])); |
---|
| 21 | $('#dataHeight').val(parseInt(data['height'])); |
---|
| 22 | }, |
---|
| 23 | |
---|
| 24 | }); |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | return true |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | function file(){ |
---|
| 32 | if ($("#file-to-upload").get(0).files[0]=== undefined ){ |
---|
| 33 | console.log("siiiiiiiiiiifdfdsfds"); |
---|
| 34 | __NUMBER += 1; |
---|
| 35 | |
---|
| 36 | }else{ |
---|
| 37 | console.log("noooo"); |
---|
| 38 | } |
---|
| 39 | return __NUMBER; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | $("#pdf-canvas").hide(); |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | function showPDF(pdf_url) { |
---|
| 48 | $("#pdf-loader").show(); |
---|
| 49 | |
---|
| 50 | PDFJS.getDocument({ url: pdf_url }).then(function(pdf_doc) { |
---|
| 51 | __PDF_DOC = pdf_doc; |
---|
| 52 | __TOTAL_PAGES = __PDF_DOC.numPages; |
---|
| 53 | |
---|
| 54 | // Hide the pdf loader and show pdf container in HTML |
---|
| 55 | $("#pdf-loader").hide(); |
---|
| 56 | $("#pdf-contents").show(); |
---|
| 57 | $("#pdf-total-pages").text(__TOTAL_PAGES); |
---|
| 58 | |
---|
| 59 | // Show the |
---|
| 60 | showPage(1); |
---|
| 61 | |
---|
| 62 | }).catch(function(error) { |
---|
| 63 | // If error re-show the upload button |
---|
| 64 | $("#pdf-loader").hide(); |
---|
| 65 | $("#upload-button").show(); |
---|
| 66 | |
---|
| 67 | alert(error.message); |
---|
| 68 | });; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | function showPage(page_no) { |
---|
| 72 | __PAGE_RENDERING_IN_PROGRESS = 1; |
---|
| 73 | __CURRENT_PAGE = page_no; |
---|
| 74 | |
---|
| 75 | // Disable Prev & Next buttons while page is being loaded |
---|
| 76 | $("#pdf-next, #pdf-prev").attr('disabled', 'disabled'); |
---|
| 77 | |
---|
| 78 | // While page is being rendered hide the canvas and show a loading message |
---|
| 79 | $("#pdf-canvas").hide(); |
---|
| 80 | $("#page-loader").show(); |
---|
| 81 | |
---|
| 82 | // Update current page in HTML |
---|
| 83 | $("#pdf-current-page").text(page_no); |
---|
| 84 | $("#pagepdf").val(page_no); |
---|
| 85 | |
---|
| 86 | // Fetch the page |
---|
| 87 | __PDF_DOC.getPage(page_no).then(function(page) { |
---|
| 88 | // As the canvas is of a fixed width we need to set the scale of the viewport accordingly |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | var scale_required = __CANVAS.width / page.getViewport(1).width; |
---|
| 92 | |
---|
| 93 | // Get viewport of the page at required scale |
---|
| 94 | var viewport = page.getViewport(scale_required); |
---|
| 95 | |
---|
| 96 | // Set canvas height |
---|
| 97 | |
---|
| 98 | __CANVAS.height = viewport.height; |
---|
| 99 | |
---|
| 100 | |
---|
| 101 | var renderContext = { |
---|
| 102 | canvasContext: __CANVAS_CTX, |
---|
| 103 | viewport: viewport |
---|
| 104 | }; |
---|
| 105 | |
---|
| 106 | |
---|
| 107 | // Render the page contents in the canvas |
---|
| 108 | page.render(renderContext).then(function() { |
---|
| 109 | __PAGE_RENDERING_IN_PROGRESS = 0; |
---|
| 110 | |
---|
| 111 | // Re-enable Prev & Next buttons |
---|
| 112 | $("#pdf-next, #pdf-prev").removeAttr('disabled'); |
---|
| 113 | |
---|
| 114 | // Show the canvas and hide the page loader |
---|
| 115 | $("#pdf-canvas").hide(); |
---|
| 116 | $("#page-loader").hide(); |
---|
| 117 | __id = __CANVAS.toDataURL() |
---|
| 118 | $("#img").attr('src', __id); |
---|
| 119 | |
---|
| 120 | cropperrr(); |
---|
| 121 | |
---|
| 122 | $('.cropper-container img').attr('src', __id)[0]; |
---|
| 123 | |
---|
| 124 | $('.cropper-container img').attr('src', __id)[1]; |
---|
| 125 | |
---|
| 126 | }); |
---|
| 127 | }); |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | // Upon click this should should trigger click on the #file-to-upload file input element |
---|
| 133 | // This is better than showing the not-good-looking file input element |
---|
| 134 | $("#upload-button").on('click', function() { |
---|
| 135 | //$("#img").removeAttr('src'); |
---|
| 136 | //$("#img").removeAttr('class'); |
---|
| 137 | //$(".cropper-container").remove(); |
---|
| 138 | $("#file-to-upload").trigger('click'); |
---|
| 139 | |
---|
| 140 | }); |
---|
| 141 | |
---|
| 142 | // When user chooses a PDF file |
---|
| 143 | $("#file-to-upload").on('change', function() { |
---|
| 144 | |
---|
| 145 | var a = file(); |
---|
| 146 | console.log(a); |
---|
| 147 | // Validate whether PDF |
---|
| 148 | if(['application/pdf'].indexOf($("#file-to-upload").get(0).files[0].type) == -1) { |
---|
| 149 | alert('Error : Not a PDF'); |
---|
| 150 | return; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | // Send the object url of the pdf |
---|
| 156 | showPDF(URL.createObjectURL($("#file-to-upload").get(0).files[0])); |
---|
| 157 | |
---|
| 158 | }); |
---|
| 159 | |
---|
| 160 | // Previous page of the PDF |
---|
| 161 | $("#pdf-prev").on('click', function() { |
---|
| 162 | if(__CURRENT_PAGE != 1) |
---|
| 163 | showPage(--__CURRENT_PAGE); |
---|
| 164 | }); |
---|
| 165 | |
---|
| 166 | // Next page of the PDF |
---|
| 167 | $("#pdf-next").on('click', function() { |
---|
| 168 | |
---|
| 169 | |
---|
| 170 | if(__CURRENT_PAGE != __TOTAL_PAGES) |
---|
| 171 | |
---|
| 172 | showPage(++__CURRENT_PAGE); |
---|
| 173 | |
---|
| 174 | }); |
---|