Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. var __PDF_DOC,
  2. __CURRENT_PAGE,
  3. __TOTAL_PAGES,
  4. __PAGE_RENDERING_IN_PROGRESS = 0,
  5. __CANVAS = $('#pdf-canvas').get(0),
  6. __CANVAS_CTX = __CANVAS.getContext('2d');
  7.  
  8. function showPDF(pdf_url) {
  9. $("#pdf-loader").show();
  10.  
  11. PDFJS.getDocument({ url: pdf_url }).then(function(pdf_doc) {
  12. __PDF_DOC = pdf_doc;
  13. __TOTAL_PAGES = __PDF_DOC.numPages;
  14.  
  15. // Hide the pdf loader and show pdf container in HTML
  16. $("#pdf-loader").hide();
  17. $("#pdf-contents").show();
  18. $("#pdf-total-pages").text(__TOTAL_PAGES);
  19.  
  20. // Show the first page
  21. showPage(1);
  22. }).catch(function(error) {
  23. // If error re-show the upload button
  24. $("#pdf-loader").hide();
  25. $("#upload-button").show();
  26.  
  27. alert(error.message);
  28. });;
  29. }
  30.  
  31. function showPage(page_no) {
  32. __PAGE_RENDERING_IN_PROGRESS = 1;
  33. __CURRENT_PAGE = page_no;
  34.  
  35. // Disable Prev & Next buttons while page is being loaded
  36. $("#pdf-next, #pdf-prev").attr('disabled', 'disabled');
  37.  
  38. // While page is being rendered hide the canvas and show a loading message
  39. $("#pdf-canvas").hide();
  40. $("#page-loader").show();
  41.  
  42. // Update current page in HTML
  43. $("#pdf-current-page").text(page_no);
  44.  
  45. // Fetch the page
  46. __PDF_DOC.getPage(page_no).then(function(page) {
  47. // As the canvas is of a fixed width we need to set the scale of the viewport accordingly
  48. var scale_required = __CANVAS.width / page.getViewport(0.5).width;
  49.  
  50.  
  51. // Get viewport of the page at required scale
  52. var viewport = page.getViewport(scale_required);
  53.  
  54. // Set canvas height
  55. __CANVAS.height = viewport.height;
  56. __CANVAS.width = viewport.width;
  57.  
  58. var renderContext = {
  59. canvasContext: __CANVAS_CTX,
  60. viewport: viewport
  61. };
  62.  
  63. // Render the page contents in the canvas
  64. page.render(renderContext).then(function() {
  65. __PAGE_RENDERING_IN_PROGRESS = 0;
  66.  
  67. // Re-enable Prev & Next buttons
  68. $("#pdf-next, #pdf-prev").removeAttr('disabled');
  69.  
  70. // Show the canvas and hide the page loader
  71. $("#pdf-canvas").show();
  72. $("#page-loader").hide();
  73. });
  74. });
  75. }
  76.  
  77. // Upon click this should should trigger click on the #file-to-upload file input element
  78. // This is better than showing the not-good-looking file input element
  79. $("#upload-button").on('click', function() {
  80. $("#file-to-upload").trigger('click');
  81. });
  82.  
  83. // When user chooses a PDF file
  84. $("#file-to-upload").on('change', function() {
  85. // Validate whether PDF
  86. if(['application/pdf'].indexOf($("#file-to-upload").get(0).files[0].type) === -1) {
  87. alert('Error : Not a PDF');
  88. return;
  89. }
  90.  
  91. $("#upload-button").hide();
  92.  
  93. // Send the object url of the pdf
  94. showPDF(URL.createObjectURL($("#file-to-upload").get(0).files[0]));
  95. });
  96.  
  97.  
  98. // Previous page of the PDF
  99. $("#pdf-prev").on('click', function() {
  100. if(__CURRENT_PAGE !== 1)
  101. showPage(--__CURRENT_PAGE);
  102. });
  103.  
  104. // Next page of the PDF
  105. $("#pdf-next").on('click', function() {
  106. if(__CURRENT_PAGE !== __TOTAL_PAGES)
  107. showPage(++__CURRENT_PAGE);
  108. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement