1
0
mirror of https://github.com/24eme/signaturepdf.git synced 2023-08-25 09:33:08 +02:00

Trim du svg en js du coup pas de dépendances à inkscape

This commit is contained in:
Vincent LAURENT 2021-09-27 15:19:35 +02:00
parent 3a1b68240d
commit 40efb8f012
2 changed files with 27 additions and 5 deletions

View File

@ -87,10 +87,9 @@ $f3->route('POST /image2svg',
shell_exec(sprintf("convert -background white -flatten %s %s", $imageFile, $imageFile.".bmp")); shell_exec(sprintf("convert -background white -flatten %s %s", $imageFile, $imageFile.".bmp"));
shell_exec(sprintf("mkbitmap -x -f 8 %s -o %s", $imageFile.".bmp", $imageFile.".bpm")); shell_exec(sprintf("mkbitmap -x -f 8 %s -o %s", $imageFile.".bmp", $imageFile.".bpm"));
shell_exec(sprintf("potrace --svg %s -o %s", $imageFile.".bpm", $imageFile.".svg")); shell_exec(sprintf("potrace --svg %s -o %s", $imageFile.".bpm", $imageFile.".svg"));
shell_exec(sprintf("convert -trim %s %s", $imageFile.".svg", $imageFile."-trim.svg"));
header('Content-Type: image/svg+xml'); header('Content-Type: image/svg+xml');
echo file_get_contents($imageFile."-trim.svg"); echo file_get_contents($imageFile.".svg");
} }
); );
$f3->route('POST /@key/save', $f3->route('POST /@key/save',

View File

@ -87,6 +87,29 @@ loadingTask.promise.then(function(pdf) {
return new Blob([u8arr], {type:mime}); return new Blob([u8arr], {type:mime});
} }
function trimSvgWhitespace(svgContent) {
if(!svgContent) {
return null;
}
var svgContainer = document.createElement("div")
svgContainer.classList.add('invisible');
svgContainer.classList.add('position-absolute');
svgContainer.classList.add('top-0');
svgContainer.classList.add('start-0');
svgContainer.style = "z-index: -1;";
svgContainer.innerHTML = svgContent;
document.body.appendChild(svgContainer);
var svg = svgContainer.querySelector('svg');
var box = svg.getBBox();
svg.setAttribute("viewBox", [box.x, box.y, box.width, box.height].join(" "));
svgContent = svgContainer.innerHTML;
document.body.removeChild(svgContainer)
return svgContent = svgContainer.innerHTML;
document;
}
var signaturePad = new SignaturePad(document.getElementById('signature-pad'), { var signaturePad = new SignaturePad(document.getElementById('signature-pad'), {
penColor: 'rgb(0, 0, 0)', penColor: 'rgb(0, 0, 0)',
minWidth: 1.25, minWidth: 1.25,
@ -101,7 +124,7 @@ loadingTask.promise.then(function(pdf) {
xhr = new XMLHttpRequest(); xhr = new XMLHttpRequest();
xhr.open( 'POST', document.getElementById('form-image-upload').action, true ); xhr.open( 'POST', document.getElementById('form-image-upload').action, true );
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
var svgImage = "data:image/svg+xml;base64,"+btoa(this.responseText); var svgImage = "data:image/svg+xml;base64,"+btoa(trimSvgWhitespace(this.responseText));
document.getElementById('img-upload').src = svgImage; document.getElementById('img-upload').src = svgImage;
document.getElementById('img-upload').classList.remove("d-none"); document.getElementById('img-upload').classList.remove("d-none");
document.getElementById('btn_modal_ajouter').focus(); document.getElementById('btn_modal_ajouter').focus();
@ -144,7 +167,7 @@ loadingTask.promise.then(function(pdf) {
xhr.open( 'POST', document.getElementById('form-image-upload').action, true ); xhr.open( 'POST', document.getElementById('form-image-upload').action, true );
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
var svgImage = "data:image/svg+xml;base64,"+btoa(this.responseText); var svgImage = "data:image/svg+xml;base64,"+btoa(trimSvgWhitespace(this.responseText));
document.getElementById('img-upload').src = svgImage; document.getElementById('img-upload').src = svgImage;
document.getElementById('img-upload').classList.remove("d-none"); document.getElementById('img-upload').classList.remove("d-none");
document.getElementById('btn_modal_ajouter').focus(); document.getElementById('btn_modal_ajouter').focus();