1
0
Fork 0
mirror of https://github.com/24eme/signaturepdf synced 2024-05-17 13:16:34 +02:00

The upload and the signature in a single page and the page is no longer reloaded after the upload

This commit is contained in:
Vincent LAURENT 2022-03-26 11:02:47 +01:00
parent 8495642ab8
commit b8f216d166
4 changed files with 114 additions and 95 deletions

14
app.php
View file

@ -40,18 +40,16 @@ function convertPHPSizeToBytes($sSize)
}
$f3->route('GET /',
function($f3) {
$f3->reroute('/signature');
}
);
$f3->route('GET /signature',
function($f3) {
$f3->set('maxSize', min(array(convertPHPSizeToBytes(ini_get('post_max_size')), convertPHPSizeToBytes(ini_get('upload_max_filesize')))));
$f3->set('maxPage', ini_get('max_file_uploads') - 1);
echo View::instance()->render('index.html.php');
}
);
$f3->route('GET /sign',
function($f3) {
$f3->set('maxPage', ini_get('max_file_uploads') - 1);
echo View::instance()->render('pdf.html.php');
echo View::instance()->render('signature.html.php');
}
);
$f3->route('POST /image2svg',

View file

@ -17,14 +17,11 @@ var menuOffcanvas = null;
var currentCursor = null;
var signaturePad = null;
var loadPDF = async function(url) {
var loadPDF = async function(pdfBlob, filename) {
var pdfjsLib = window['pdfjs-dist/build/pdf'];
pdfjsLib.GlobalWorkerOptions.workerSrc = '/vendor/pdf.worker.js?legacy';
const cache = await caches.open('pdf');
var responsePdf = await cache.match(url);
var pdfBlob = await responsePdf.blob();
url = await URL.createObjectURL(pdfBlob);
let url = await URL.createObjectURL(pdfBlob);
var dataTransfer = new DataTransfer();
dataTransfer.items.add(new File([pdfBlob], filename, {
@ -161,13 +158,13 @@ var is_mobile = function() {
var responsiveDisplay = function() {
if(is_mobile()) {
document.body.style.paddingRight = "inherit";
document.getElementById('page-signature').style.paddingRight = "inherit";
menu.classList.remove('show');
menuOffcanvas.hide();
document.getElementById('container-pages').classList.remove('vh-100');
} else {
menuOffcanvas.show();
document.body.style.paddingRight = "350px";
document.getElementById('page-signature').style.paddingRight = "350px";
document.getElementById('container-pages').classList.add('vh-100');
}
menu.classList.remove('d-md-block');
@ -879,8 +876,66 @@ var createSignaturePad = function() {
});
};
(function () {
async function getPDFBlobFromCache(cacheUrl) {
const cache = await caches.open('pdf');
let responsePdf = await cache.match(cacheUrl);
if(!responsePdf) {
return null;
}
let pdfBlob = await responsePdf.blob();
return pdfBlob;
}
async function uploadFromUrl(url) {
var response = await fetch(url);
if(response.status != 200) {
return;
}
var pdfBlob = await response.blob();
if(pdfBlob.type != 'application/pdf' && pdfBlob.type != 'application/octet-stream') {
return;
}
let dataTransfer = new DataTransfer();
let filename = url.replace(/^.*\//, '');
dataTransfer.items.add(new File([pdfBlob], filename, {
type: 'application/pdf'
}));
document.getElementById('input_pdf_upload').files = dataTransfer.files;
document.getElementById('input_pdf_upload').dispatchEvent(new Event("change"));
}
var pageUpload = async function() {
history.replaceState({}, "Signature de PDF", "/signature");
document.getElementById('input_pdf_upload').value = '';
document.getElementById('page-upload').classList.remove('d-none');
document.getElementById('page-signature').classList.add('d-none');
document.getElementById('input_pdf_upload').focus();
const cache = await caches.open('pdf');
document.getElementById('input_pdf_upload').addEventListener('change', async function(event) {
if(document.getElementById('input_pdf_upload').files[0].size > maxSize) {
alert("Le PDF ne doit pas dépasser <?php echo round($maxSize / 1024 / 1024) ?> Mo");
document.getElementById('input_pdf_upload').value = "";
return;
}
let filename = document.getElementById('input_pdf_upload').files[0].name;
let response = new Response(document.getElementById('input_pdf_upload').files[0], { "status" : 200, "statusText" : "OK" });
let urlPdf = '/pdf/'+filename;
await cache.put(urlPdf, response);
history.replaceState({}, "Signature de PDF", '/signature#'+filename);
pageSignature(urlPdf)
});
}
var pageSignature = async function(url) {
document.getElementById('page-upload').classList.add('d-none');
document.getElementById('page-signature').classList.remove('d-none');
fabric.Textbox.prototype._wordJoiners = /[]/;
menu = document.getElementById('sidebarTools');
menuOffcanvas = new bootstrap.Offcanvas(menu);
@ -895,11 +950,34 @@ var createSignaturePad = function() {
fontCaveat = font;
});
let pdfBlob = await getPDFBlobFromCache(url);
let filename = url.replace('/pdf/', '');
if(!pdfBlob) {
document.location = '/signature';
return;
}
createSignaturePad();
responsiveDisplay();
displaysSVG();
stateAddLock();
createEventsListener();
loadPDF(url);
loadPDF(pdfBlob, filename);
};
(function () {
if(window.location.hash && window.location.hash.match(/^\#http/)) {
let hashUrl = window.location.hash.replace(/^\#/, '');
pageUpload();
uploadFromUrl(hashUrl);
} else if(window.location.hash) {
pageSignature('/pdf/'+window.location.hash.replace(/^\#/, ''));
} else {
pageUpload();
}
window.addEventListener('hashchange', function() {
//window.location.href = window.location.href;
window.location.reload();
})
//pageSignature(url);
})();

View file

@ -1,74 +0,0 @@
<!doctype html>
<html lang="fr_FR">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/vendor/bootstrap.min.css?5.1.1" rel="stylesheet">
<link href="/vendor/bootstrap-icons.css?1.5.0" rel="stylesheet">
<title>Signature PDF</title>
</head>
<body>
<div class="px-4 py-5 my-5 text-center">
<h1 class="display-5 fw-bold"><i class="bi bi-vector-pen"></i> Signer un PDF</h1>
<div class="col-lg-3 mx-auto">
<div class="col-12">
<label for="input_pdf_upload" class="form-label">Choisir un PDF</label>
<input id="input_pdf_upload" class="form-control form-control-lg" type="file" accept=".pdf,application/pdf">
<p class="mt-1 opacity-50"><small class="text-muted">Le PDF ne doit pas dépasser <?php echo round($maxSize / 1024 / 1024) ?> Mo et <?php echo $maxPage ?> pages</small></p>
<a class="btn btn-sm btn-link opacity-75" href="/#https://raw.githubusercontent.com/24eme/signaturepdf/master/tests/files/document.pdf">Tester avec un PDF de démo</a>
</div>
</div>
</div>
<footer class="text-center text-muted mb-2 fixed-bottom">
<small>Logiciel libre sous license AGPL-3.0 : <a href="https://github.com/24eme/signaturepdf">voir le code source</a></small>
</footer>
<script>
(async function () {
const cache = await caches.open('pdf');
var maxSize = <?php echo $maxSize ?>;
document.getElementById('input_pdf_upload').addEventListener('change', async function(event) {
if(document.getElementById('input_pdf_upload').files[0].size > maxSize) {
alert("Le PDF ne doit pas dépasser <?php echo round($maxSize / 1024 / 1024) ?> Mo");
document.getElementById('input_pdf_upload').value = "";
return;
}
let filename = document.getElementById('input_pdf_upload').files[0].name;
var response = new Response(document.getElementById('input_pdf_upload').files[0], { "status" : 200, "statusText" : "OK" });
let urlPdf = '/pdf/#'+filename;
await cache.put(urlPdf, response);
document.location = '/sign/#'+filename;
});
async function uploadFromUrl(url) {
var response = await fetch(url);
if(response.status != 200) {
return;
}
var pdfBlob = await response.blob();
if(pdfBlob.type != 'application/pdf' && pdfBlob.type != 'application/octet-stream') {
return;
}
var dataTransfer = new DataTransfer();
var filename = url.replace(/^.*\//, '');
dataTransfer.items.add(new File([pdfBlob], filename, {
type: 'application/pdf'
}));
document.getElementById('input_pdf_upload').files = dataTransfer.files;
document.getElementById('input_pdf_upload').dispatchEvent(new Event("change"));
history.replaceState({}, "Signature de PDF", "/");
}
if(window.location.hash && window.location.hash.match(/^\#http/)) {
uploadFromUrl(window.location.hash.replace(/^\#/, ''));
}
window.addEventListener('hashchange', function() {
uploadFromUrl(window.location.hash.replace(/^\#/, ''));
})
})();
</script>
</body>
</html>

View file

@ -10,7 +10,24 @@
<link href="/css/app.css" rel="stylesheet">
<title>Signature PDF</title>
</head>
<body class="bg-light" style="padding-right: 350px;">
<body class="bg-light">
<div id="page-upload">
<div class="px-4 py-5 my-5 text-center">
<h1 class="display-5 fw-bold"><i class="bi bi-vector-pen"></i> Signer un PDF</h1>
<div class="col-lg-3 mx-auto">
<div class="col-12">
<label for="input_pdf_upload" class="form-label">Choisir un PDF</label>
<input id="input_pdf_upload" class="form-control form-control-lg" type="file" accept=".pdf,application/pdf">
<p class="mt-1 opacity-50"><small class="text-muted">Le PDF ne doit pas dépasser <?php echo round($maxSize / 1024 / 1024) ?> Mo et <?php echo $maxPage ?> pages</small></p>
<a class="btn btn-sm btn-link opacity-75" href="/signature#https://raw.githubusercontent.com/24eme/signaturepdf/master/tests/files/document.pdf">Tester avec un PDF de démo</a>
</div>
</div>
</div>
<footer class="text-center text-muted mb-2 fixed-bottom">
<small>Logiciel libre sous license AGPL-3.0 : <a href="https://github.com/24eme/signaturepdf">voir le code source</a></small>
</footer>
</div>
<div id="page-signature" style="padding-right: 350px;" class="d-none">
<div style="height: 65px;" class="d-md-none"></div>
<div id="container-pages" class="col-12 pt-1 pb-1 text-center vh-100">
</div>
@ -122,6 +139,7 @@
</div>
</div>
</div>
</div>
<span id="is_mobile" class="d-md-none"></span>
<script src="/vendor/bootstrap.min.js?5.1.1"></script>
@ -130,10 +148,9 @@
<script src="/vendor/signature_pad.umd.min.js?3.0.0-beta.3"></script>
<script src="/vendor/opentype.min.js?1.3.3"></script>
<script>
var url = '/pdf/'+window.location.hash;
var maxSize = <?php echo $maxSize ?>;
var maxPage = <?php echo $maxPage ?>;
var filename = window.location.hash.replace(/^\#/, '');
</script>
<script src="/js/app.js"></script>
<script src="/js/signature.js?202203261059"></script>
</body>
</html>