La route pour sauvegardé un pdf signé n'a plus besoin du paramètre key

et le nom de fichier peut être récupéré depuis le champs d'upload
This commit is contained in:
Vincent LAURENT 2021-11-12 09:03:35 +01:00
parent 07acab564b
commit 84790c924c
4 changed files with 15 additions and 23 deletions

27
app.php
View File

@ -93,19 +93,20 @@ $f3->route('POST /image2svg',
array_map('unlink', glob($imageFile."*")); array_map('unlink', glob($imageFile."*"));
} }
); );
$f3->route('POST /@key/save', $f3->route('POST /sign',
function($f3) { function($f3) {
$key = $f3->get('PARAMS.key'); $filename = null;
$files = Web::instance()->receive(function($file,$formFieldName){ $files = Web::instance()->receive(function($file,$formFieldName){
if(strpos(Web::instance()->mime($file['tmp_name'], true), 'application/pdf') !== 0) { if(strpos(Web::instance()->mime($file['tmp_name'], true), 'application/pdf') !== 0) {
return false; return false;
} }
return true; return true;
}, true, function($fileBaseName, $formFieldName) use ($key) { }, true, function($fileBaseName, $formFieldName) use ($f3, &$filename) {
$filename = str_replace(".pdf", "_signe.pdf", $fileBaseName);
return $key.".pdf"; $tmpfile = tempnam($f3->get('UPLOADS'), 'pdfsignature_pdf');
unlink($tmpfile);
return basename($tmpfile).".pdf";
}); });
$pdfFile = null; $pdfFile = null;
@ -121,27 +122,23 @@ $f3->route('POST /@key/save',
} }
$svgData = $_POST['svg']; $svgData = $_POST['svg'];
$filename = null;
if(isset($_POST['filename']) && $_POST['filename']) {
$filename = str_replace(".pdf", "_signe.pdf", $_POST['filename']);
}
$svgFiles = ""; $svgFiles = "";
foreach($svgData as $index => $svgItem) { foreach($svgData as $index => $svgItem) {
$svgFile = $f3->get('UPLOADS').$key.'_'.$index.'.svg'; $svgFile = $pdfFile.'_'.$index.'.svg';
file_put_contents($svgFile, $svgItem); file_put_contents($svgFile, $svgItem);
$svgFiles .= $svgFile . " "; $svgFiles .= $svgFile . " ";
} }
shell_exec(sprintf("rsvg-convert -f pdf -o %s %s", $f3->get('UPLOADS').$key.'.svg.pdf', $svgFiles)); shell_exec(sprintf("rsvg-convert -f pdf -o %s %s", $pdfFile.'.svg.pdf', $svgFiles));
shell_exec(sprintf("pdftk %s multibackground %s output %s", $f3->get('UPLOADS').$key.'.svg.pdf', $f3->get('UPLOADS').$key.'.pdf', $f3->get('UPLOADS').$key.'_signe.pdf')); shell_exec(sprintf("pdftk %s multibackground %s output %s", $pdfFile.'.svg.pdf', $pdfFile, $pdfFile.'_signe.pdf'));
Web::instance()->send($f3->get('UPLOADS').$key.'_signe.pdf', null, 0, TRUE, $filename); Web::instance()->send($pdfFile.'_signe.pdf', null, 0, TRUE, $filename);
if($f3->get('DEBUG')) { if($f3->get('DEBUG')) {
return; return;
} }
array_map('unlink', glob($f3->get('UPLOADS').$key."*")); array_map('unlink', glob($pdfFile."*"));
} }
); );

View File

@ -473,10 +473,6 @@ loadingTask.promise.then(function(pdf) {
xhr.send( formData ); xhr.send( formData );
} }
if(filename) {
document.getElementById('input_filename').value = filename;
}
document.getElementById('save').addEventListener('click', function(event) { document.getElementById('save').addEventListener('click', function(event) {
canvasEditions.forEach(function(canvasEdition, index) { canvasEditions.forEach(function(canvasEdition, index) {
document.getElementById('data-svg-'+index).value = canvasEdition.toSVG(); document.getElementById('data-svg-'+index).value = canvasEdition.toSVG();

View File

@ -16,7 +16,7 @@
<div class="col-12"> <div class="col-12">
<label for="input_pdf_upload" class="form-label">Choisir un PDF</label> <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"> <input id="input_pdf_upload" class="form-control form-control-lg" type="file" accept=".pdf,application/pdf">
<p><small class="text-muted">(Le PDF ne doit pas dépasser <?php echo round($maxSize / 1024 / 1024) ?> mo)</small></p> <p><small class="text-muted">Le PDF ne doit pas dépasser <?php echo round($maxSize / 1024 / 1024) ?> Mo</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> <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> </div>
@ -39,7 +39,7 @@
document.getElementById('input_pdf_upload').addEventListener('change', async function(event) { document.getElementById('input_pdf_upload').addEventListener('change', async function(event) {
if(document.getElementById('input_pdf_upload').files[0].size > maxSize) { 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"); alert("Le PDF ne doit pas dépasser <?php echo round($maxSize / 1024 / 1024) ?> Mo");
document.getElementById('input_pdf_upload').value = ""; document.getElementById('input_pdf_upload').value = "";
return; return;
} }

View File

@ -68,8 +68,7 @@
<button type="button" id="btn-add-svg" class="btn btn-sm btn-light" data-bs-toggle="modal" data-bs-target="#modalAddSvg"><i class="bi bi-plus-circle"></i> Ajouter un élément</button> <button type="button" id="btn-add-svg" class="btn btn-sm btn-light" data-bs-toggle="modal" data-bs-target="#modalAddSvg"><i class="bi bi-plus-circle"></i> Ajouter un élément</button>
</div> </div>
<form class="position-absolute bottom-0 pb-2 ps-0 pe-4 w-100 d-none d-sm-none d-md-block" id="form_pdf" action="/<?php echo $key ?>/save" method="post" enctype="multipart/form-data"> <form class="position-absolute bottom-0 pb-2 ps-0 pe-4 w-100 d-none d-sm-none d-md-block" id="form_pdf" action="/sign" method="post" enctype="multipart/form-data">
<input id="input_filename" type="hidden" name="filename" value="" />
<input id="input_pdf" name="pdf" type="file" class="d-none" /> <input id="input_pdf" name="pdf" type="file" class="d-none" />
<div class="d-grid gap-2 mt-2"> <div class="d-grid gap-2 mt-2">
<button class="btn btn-primary" disabled="disabled" type="submit" id="save"><i class="bi bi-download"></i> Télécharger le PDF Signé</button> <button class="btn btn-primary" disabled="disabled" type="submit" id="save"><i class="bi bi-download"></i> Télécharger le PDF Signé</button>