From ebf7e0e8897d7e47ee85bf9ce44f8b93f1dcd1a0 Mon Sep 17 00:00:00 2001 From: Vincent LAURENT Date: Fri, 15 Apr 2022 00:47:36 +0200 Subject: [PATCH] Automatically reload the pdf if there is a new signature --- public/js/signature.js | 28 +++++++++++++++++++++++++--- templates/signature.html.php | 2 +- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/public/js/signature.js b/public/js/signature.js index 9756dcd..54d37d2 100644 --- a/public/js/signature.js +++ b/public/js/signature.js @@ -16,9 +16,10 @@ var menu = null; var menuOffcanvas = null; var currentCursor = null; var signaturePad = null; +var nblayers = null; var loadPDF = async function(pdfBlob, filename) { - let pdfjsLib = window['pdfjs-dist/build/pdf']; + const pdfjsLib = window['pdfjs-dist/build/pdf']; pdfjsLib.GlobalWorkerOptions.workerSrc = '/vendor/pdf.worker.js?legacy'; let url = await URL.createObjectURL(pdfBlob); @@ -37,7 +38,7 @@ var loadPDF = async function(pdfBlob, filename) { if(document.getElementById('input_pdf_share')) { document.getElementById('input_pdf_share').files = dataTransfer.files; } - var loadingTask = pdfjsLib.getDocument(url); + let loadingTask = pdfjsLib.getDocument(url); loadingTask.promise.then(function(pdf) { if(pdf.numPages > maxPage) { @@ -156,6 +157,23 @@ var loadPDF = async function(pdfBlob, filename) { }); }; +var reloadPDF = async function(url) { + const pdfjsLib = window['pdfjs-dist/build/pdf']; + pdfjsLib.GlobalWorkerOptions.workerSrc = '/vendor/pdf.worker.js?legacy'; + + pdfjsLib.getDocument(url).promise.then(function(pdf) { + for(let pageNumber = 1; pageNumber <= pdf.numPages; pageNumber++ ) { + pdf.getPage(pageNumber).then(function(page) { + page.render({ + canvasContext: document.getElementById('canvas-pdf-' + (page.pageNumber - 1)).getContext('2d'), + viewport: page.getViewport({scale: currentScale}), + enhanceTextSelection: true + }); + }); + } + }); +} + var is_mobile = function() { return !(window.getComputedStyle(document.getElementById('is_mobile')).display === "none"); }; @@ -994,7 +1012,11 @@ var updateNbLayers = function() { xhr.open('GET', '/signature/'+hash+'/nblayers', true); xhr.onload = function() { if (xhr.status == 200) { - let nblayers = xhr.response; + let newNblayers = xhr.response; + if(nblayers !== null && nblayers != newNblayers) { + reloadPDF('/signature/'+hash+'/pdf'); + } + nblayers = newNblayers; document.querySelectorAll('.nblayers').forEach(function(item) { item.innerHTML = nblayers; }); diff --git a/templates/signature.html.php b/templates/signature.html.php index 991eda3..2be1aaa 100644 --- a/templates/signature.html.php +++ b/templates/signature.html.php @@ -247,6 +247,6 @@ hash = ""; - +