- aggiornato la visualoizzazione per scaricare il PDF e per aprirlo

This commit is contained in:
Surya Paolo
2025-05-30 10:49:32 +02:00
parent d2aaf78c0e
commit f0c25366db
2 changed files with 36 additions and 17 deletions

View File

@@ -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') {

View File

@@ -1 +1 @@
1.2.50
1.2.52