1
0
Fork 0
mirror of https://github.com/24eme/signaturepdf synced 2024-06-03 14:32:13 +02:00

organisation: Copying pages adds the total size of the PDF for each page and adds a loader

This commit is contained in:
Vincent LAURENT 2023-11-10 02:18:47 +01:00
parent 410f1c1a84
commit 24b24bddc7
2 changed files with 31 additions and 17 deletions

View file

@ -564,36 +564,41 @@ const DL = function (d,f) {
setTimeout(() => URL.revokeObjectURL(u)) setTimeout(() => URL.revokeObjectURL(u))
} }
const save = async function () { let save = async function () {
const PDFDocument = window['PDFLib'].PDFDocument const PDFDocument = window['PDFLib'].PDFDocument
const Rotation = window['PDFLib'].Rotation const Rotation = window['PDFLib'].Rotation
const pdf = await PDFDocument.create(); const pdf = await PDFDocument.create();
let filename = ""; let filename = "";
let pdfFiles = []; let pages = [];
for (let i = 0; i < document.querySelector('#input_pdf').files.length; i++) { for (let i = 0; i < document.querySelector('#input_pdf').files.length; i++) {
if(filename) { if(filename) {
filename += '_'; filename += '_';
} }
filename += document.querySelector('#input_pdf').files.item(i).name.replace(/\.pdf$/, ''); filename += document.querySelector('#input_pdf').files.item(i).name.replace(/\.pdf$/, '');
pdfFiles[getLetter(i)] = await PDFDocument.load(await document.querySelector('#input_pdf').files.item(i).arrayBuffer()); pdfFile = await PDFDocument.load(await document.querySelector('#input_pdf').files.item(i).arrayBuffer());
const pdfPages = await pdf.copyPages(pdfFile, pdfFile.getPageIndices());
for(j in pdfPages) {
const numPage = parseInt(j) + 1;
pages[getLetter(i)+numPage.toString()] = pdfPages[j];
}
} }
const pages = document.querySelector('#input_pages').value.split(','); const pagesOrganize = document.querySelector('#input_pages').value.split(',');
console.log(pages);
for(let i in pages) { for(let i in pagesOrganize) {
const page = pages[i].split('-')[0]; const pageOrganize = pagesOrganize[i].split('-')[0];
const rotation = pages[i].split('-')[1]; console.log(pageOrganize);
let pdfFile = pdfFiles[page.substr(0,1)]; const rotation = pagesOrganize[i].split('-')[1];
const [pdfPage] = await pdf.copyPages(pdfFile, [parseInt(page.substr(1)) - 1]); const pdfPage = pages[pageOrganize];
if(rotation) { if(rotation) {
pdfPage.setRotation(window['PDFLib'].degrees(parseInt(rotation))); pdfPage.setRotation(window['PDFLib'].degrees(parseInt(rotation)));
} }
pdf.addPage(pdfPage); pdf.addPage(pdfPage);
} }
const newPDF = new Blob([await pdf.save()], {type: "application/pdf"}); const newPDF = new Blob([await pdf.save()], {type: "application/pdf"});
DL(newPDF, filename+".pdf"); await DL(newPDF, filename+".pdf");
} }
var createEventsListener = function() { var createEventsListener = function() {
@ -611,9 +616,15 @@ var createEventsListener = function() {
event.preventDefault(); event.preventDefault();
buttonSave.disabled = true; buttonSave.disabled = true;
}); });
document.getElementById('save').addEventListener('click', function(event) { document.getElementById('save').addEventListener('click', async function(e) {
let order = []; e.preventDefault();
let btn = document.getElementById('save');
btn.disabled = true;
btn.querySelector('.spinner-grow').classList.remove('d-none');
btn.querySelector('.bi').classList.add('d-none');
let order = [];
let selectionMode = isSelectionMode(); let selectionMode = isSelectionMode();
document.querySelectorAll('.canvas-container').forEach(function(canvasContainer) { document.querySelectorAll('.canvas-container').forEach(function(canvasContainer) {
@ -635,8 +646,11 @@ var createEventsListener = function() {
} }
}); });
document.querySelector('#input_pages').value = order.join(','); document.querySelector('#input_pages').value = order.join(',');
save(); await save();
event.preventDefault();
btn.querySelector('.spinner-grow').classList.add('d-none');
btn.querySelector('.bi').classList.remove('d-none');
btn.disabled = false;
}); });
document.getElementById('save_mobile').addEventListener('click', function(event) { document.getElementById('save_mobile').addEventListener('click', function(event) {
document.getElementById('save').click(); document.getElementById('save').click();

View file

@ -82,7 +82,7 @@
<input id="input_pdf" name="pdf[]" type="file" class="d-none" /> <input id="input_pdf" name="pdf[]" type="file" class="d-none" />
<input id="input_pages" type="hidden" value="" name="pages" /> <input id="input_pages" type="hidden" value="" name="pages" />
<div id="btn_container" class="d-grid gap-2 mt-2"> <div id="btn_container" class="d-grid gap-2 mt-2">
<button class="btn btn-primary" type="submit" id="save"><?php echo sprintf(_("%s Download the full PDF"), '<i class="bi bi-download"></i>'); ?></button> <button class="btn btn-primary" type="submit" id="save"><span class="spinner-grow spinner-grow-sm d-none"></span><?php echo sprintf(_("%s Download the full PDF"), '<i class="bi bi-download"></i>'); ?></button>
</div> </div>
</form> </form>
</div> </div>