From f0c25366db5ec7c3cbac8d6999146b87a63fdc36 Mon Sep 17 00:00:00 2001 From: Surya Paolo Date: Fri, 30 May 2025 10:49:32 +0200 Subject: [PATCH] - aggiornato la visualoizzazione per scaricare il PDF e per aprirlo --- src/server/modules/GenPdf.js | 51 +++++++++++++++++++++++++----------- src/server/version.txt | 2 +- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/src/server/modules/GenPdf.js b/src/server/modules/GenPdf.js index 0135f38..3838982 100644 --- a/src/server/modules/GenPdf.js +++ b/src/server/modules/GenPdf.js @@ -23,7 +23,9 @@ class GenPdf { } async launch() { - this.browser = await puppeteer.launch(); + this.browser = await puppeteer.launch({ + args: ['--no-sandbox', '--disable-setuid-sandbox'], + }); } async close() { @@ -47,23 +49,30 @@ class GenPdf { async autoScroll(page) { console.log('inizia a scrollare'); + + // Esegui lo scroll fino a quando tutta la pagina non è stata scrollata await page.evaluate(async () => { await new Promise((resolve) => { let totalHeight = 0; - console.log('autoScroll...'); - const distance = 100; - const timer = setInterval(() => { + const distance = 100; // distanza dello scroll ad ogni intervallo + const delay = 40; // tempo di intervallo in ms + + const scroll = async () => { + // Esegui lo scroll window.scrollBy(0, distance); totalHeight += distance; - if (totalHeight >= document.body.scrollHeight) { - clearInterval(timer); - resolve(); + // Verifica se è stato scrollato tutto il corpo della pagina + if (totalHeight < document.body.scrollHeight) { + setTimeout(scroll, delay); // Se non è finito lo scroll, continua + } else { + resolve(); // Scroll terminato } - }, 40); + }; + + scroll(); // Avvia lo scroll }); }); - console.log(' FINE: autoScroll...'); } async generatePdfFromUrl(url, filenameOut, options = {}) { @@ -77,7 +86,14 @@ class GenPdf { try { console.log(`caricamento pagina: ${url}`); await page.goto(url, { waitUntil: 'networkidle0' }); - await tools.attendiNSecondi(5); + + page.on('console', (msg) => { + if (msg.type() === 'error') { + console.error('Errore nella pagina:', msg.text()); + } + }); + + await tools.attendiNSecondi(8); let success = false; let numTentativi1 = 0; while (numTentativi1 < maxTentativi) { @@ -166,9 +182,6 @@ class GenPdf { console.log(`PDF finale generato: ${finalFilePath}`); - if (options.compressione) { - } - return finalFilePath; } catch (error) { console.error('Errore durante generazione PDF:', error); @@ -394,7 +407,12 @@ class GenPdf { const catalog = await Catalog.findById(options.idCatalog); if (catalog) { const url = - tools.getHostByIdApp(this.idapp) + '/cataloghi?id=' + catalog._id + '&stampa=' + (stampa ? '1' : '0') + '&hideHeader=1'; + tools.getHostByIdApp(this.idapp) + + '/cataloghi?id=' + + catalog._id + + '&stampa=' + + (stampa ? '1' : '0') + + '&hideHeader=1'; if (url) { const myfilenameout = await this.getPathByPage(catalog.idPageAssigned); @@ -419,6 +437,7 @@ class GenPdf { fileout_compressed: filenamerelative_compressed, filesize: await tools.getSizeFile(fullnamepath), filesize_compressed: await tools.getSizeFile(fullnamepath_compr), + error: '', }; } catch (error) { console.error('Errore durante la generazione del PDF dal catalogo ID:', error); @@ -429,7 +448,7 @@ class GenPdf { }; } - return { fileout: '', filesize: 0 }; + return { fileout: '', filesize: 0, error: '' }; } async compressPdf(inputPath, outputPath, compressione = 'printer') { @@ -439,7 +458,7 @@ class GenPdf { const outputFile = path.resolve(outputPath); const validQualities = ['screen', 'ebook', 'printer', 'prepress', 'default']; - if (!validQualities.includes(compressione)) compressione = 'screen'; + if (!validQualities.includes(compressione)) compressione = 'screen'; // Comando Ghostscript per compressione - impostazione per web (/screen) const gsCommand = `gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/${compressione} -dNOPAUSE -dBATCH -dQUIET -sOutputFile="${outputFile}" "${inputFile}"`; diff --git a/src/server/version.txt b/src/server/version.txt index f9b76f9..fc89a67 100644 --- a/src/server/version.txt +++ b/src/server/version.txt @@ -1 +1 @@ -1.2.50 \ No newline at end of file +1.2.52 \ No newline at end of file