[747e744] | 1 | |
---|
| 2 | |
---|
| 3 | var __PDF_DOC, |
---|
| 4 | __CURRENT_PAGE, |
---|
| 5 | __TOTAL_PAGES, |
---|
| 6 | __PAGE_RENDERING_IN_PROGRESS = 0, |
---|
| 7 | __CANVAS = $('#pdf-canvas2').get(0), |
---|
| 8 | __CANVAS_CTX = __CANVAS.getContext('2d'), |
---|
| 9 | __id = "", |
---|
[0500480] | 10 | __NUMBER = 0, |
---|
| 11 | __CLICK = 0; |
---|
[747e744] | 12 | |
---|
| 13 | $("#pdf-canvas2").hide(); |
---|
| 14 | function showPDF2(pdf_url) { |
---|
| 15 | $("#pdf-loader2").show(); |
---|
| 16 | |
---|
| 17 | PDFJS.getDocument({ url: pdf_url }).then(function(pdf_doc) { |
---|
| 18 | __PDF_DOC = pdf_doc; |
---|
| 19 | __TOTAL_PAGES = __PDF_DOC.numPages; |
---|
| 20 | |
---|
| 21 | // Hide the pdf loader and show pdf container in HTML |
---|
| 22 | $("#pdf-loader2").hide(); |
---|
| 23 | $("#pdf-contents2").show(); |
---|
| 24 | $("#pdf-total-pages2").text(__TOTAL_PAGES); |
---|
| 25 | |
---|
| 26 | // Show the |
---|
| 27 | showPage2(1); |
---|
| 28 | |
---|
| 29 | }).catch(function(error) { |
---|
| 30 | // If error re-show the upload button |
---|
| 31 | $("#pdf-loader2").hide(); |
---|
| 32 | $("#upload-button2").show(); |
---|
| 33 | |
---|
| 34 | alert(error.message); |
---|
| 35 | });; |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | function showPage2(page_no) { |
---|
| 39 | __PAGE_RENDERING_IN_PROGRESS = 1; |
---|
| 40 | __CURRENT_PAGE = page_no; |
---|
| 41 | |
---|
| 42 | // Disable Prev & Next buttons while page is being loaded |
---|
| 43 | $("#pdf-next2, #pdf-prev2").attr('disabled', 'disabled'); |
---|
| 44 | |
---|
| 45 | // While page is being rendered hide the canvas and show a loading message |
---|
| 46 | $("#pdf-canvas2").hide(); |
---|
| 47 | $("#page-loader2").show(); |
---|
| 48 | |
---|
| 49 | // Update current page in HTML |
---|
| 50 | $("#pdf-current-page2").text(page_no); |
---|
[327fd70] | 51 | |
---|
| 52 | VALOR_PAGE = page_no; |
---|
[747e744] | 53 | |
---|
| 54 | // Fetch the page |
---|
| 55 | __PDF_DOC.getPage(page_no).then(function(page) { |
---|
| 56 | // As the canvas is of a fixed width we need to set the scale of the viewport accordingly |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | var scale_required = __CANVAS.width / page.getViewport(1).width; |
---|
| 60 | |
---|
| 61 | // Get viewport of the page at required scale |
---|
| 62 | var viewport = page.getViewport(scale_required); |
---|
| 63 | |
---|
| 64 | // Set canvas height |
---|
| 65 | |
---|
| 66 | __CANVAS.height = viewport.height; |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | var renderContext = { |
---|
| 70 | canvasContext: __CANVAS_CTX, |
---|
| 71 | viewport: viewport |
---|
| 72 | }; |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | // Render the page contents in the canvas |
---|
| 76 | page.render(renderContext).then(function() { |
---|
| 77 | __PAGE_RENDERING_IN_PROGRESS = 0; |
---|
| 78 | |
---|
| 79 | // Re-enable Prev & Next buttons |
---|
| 80 | $("#pdf-next2, #pdf-prev2").removeAttr('disabled'); |
---|
| 81 | |
---|
| 82 | // Show the canvas and hide the page loader |
---|
| 83 | $("#page-loader2").hide(); |
---|
| 84 | |
---|
| 85 | __id = __CANVAS.toDataURL() |
---|
| 86 | |
---|
[327fd70] | 87 | var viewdimen = page.getViewport(1); |
---|
[e3349ca] | 88 | |
---|
| 89 | $("#container1").attr("style", "background-image: url('"+__id+"');background-size: cover; background-repeat: no-repeat; height:"+viewdimen.height+"px; width:"+viewdimen.width+"px"); |
---|
[0500480] | 90 | |
---|
[747e744] | 91 | |
---|
| 92 | }); |
---|
| 93 | }); |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | // Upon click this should should trigger click on the #file-to-upload file input element |
---|
| 99 | // This is better than showing the not-good-looking file input element |
---|
| 100 | $("#upload-button2").on('click', function() { |
---|
| 101 | //$("#img").removeAttr('src'); |
---|
| 102 | //$("#img").removeAttr('class'); |
---|
| 103 | //$(".cropper-container").remove(); |
---|
[e3349ca] | 104 | $("#pdf-main-container2").hide(); |
---|
[9be8a48] | 105 | $("#pdf-main-container").show(); |
---|
| 106 | $("#myJson_wrapper").hide(); |
---|
| 107 | CONTAINER = true; |
---|
[327fd70] | 108 | $("#file-sign-ft_Vble").trigger('click'); |
---|
[747e744] | 109 | |
---|
| 110 | }); |
---|
| 111 | |
---|
| 112 | // When user chooses a PDF file |
---|
[327fd70] | 113 | $("#file-sign-ft_Vble").on('change', function() { |
---|
[747e744] | 114 | |
---|
| 115 | // Validate whether PDF |
---|
[9be8a48] | 116 | if(['application/pdf'].indexOf($("#file-sign-ft_Vble").get(0).files[0].type) == -1) { |
---|
| 117 | alert('Error : Not a PDF'); |
---|
| 118 | return; |
---|
| 119 | } |
---|
[747e744] | 120 | |
---|
| 121 | |
---|
[0500480] | 122 | if (__CLICK == 0){ |
---|
| 123 | $('#firmar-documento').attr("disabled","disabled").show(); |
---|
| 124 | $('#texto').show(); |
---|
| 125 | __CLICK += 1; |
---|
| 126 | } |
---|
[f1ec144] | 127 | else{ |
---|
[9be8a48] | 128 | |
---|
[025e022] | 129 | $('#myJson_wrapper').hide(); |
---|
[f1ec144] | 130 | $('#firmar-documento').show(); |
---|
| 131 | } |
---|
[0500480] | 132 | |
---|
[9be8a48] | 133 | // Send the object url of the pdf |
---|
| 134 | showPDF2(URL.createObjectURL($("#file-sign-ft_Vble").get(0).files[0])); |
---|
| 135 | |
---|
| 136 | |
---|
[68a964b] | 137 | |
---|
| 138 | |
---|
[747e744] | 139 | }); |
---|
| 140 | |
---|
| 141 | // Previous page of the PDF |
---|
| 142 | $("#pdf-prev2").on('click', function() { |
---|
| 143 | if(__CURRENT_PAGE != 1) |
---|
| 144 | showPage2(--__CURRENT_PAGE); |
---|
| 145 | }); |
---|
| 146 | |
---|
| 147 | // Next page of the PDF |
---|
| 148 | $("#pdf-next2").on('click', function() { |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | if(__CURRENT_PAGE != __TOTAL_PAGES) |
---|
| 152 | |
---|
| 153 | showPage2(++__CURRENT_PAGE); |
---|
| 154 | |
---|
| 155 | }); |
---|
| 156 | |
---|
| 157 | |
---|