- aggiornato la visualoizzazione per scaricare il PDF e per aprirlo
This commit is contained in:
@@ -23,7 +23,9 @@ class GenPdf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async launch() {
|
async launch() {
|
||||||
this.browser = await puppeteer.launch();
|
this.browser = await puppeteer.launch({
|
||||||
|
args: ['--no-sandbox', '--disable-setuid-sandbox'],
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async close() {
|
async close() {
|
||||||
@@ -47,23 +49,30 @@ class GenPdf {
|
|||||||
|
|
||||||
async autoScroll(page) {
|
async autoScroll(page) {
|
||||||
console.log('inizia a scrollare');
|
console.log('inizia a scrollare');
|
||||||
|
|
||||||
|
// Esegui lo scroll fino a quando tutta la pagina non è stata scrollata
|
||||||
await page.evaluate(async () => {
|
await page.evaluate(async () => {
|
||||||
await new Promise((resolve) => {
|
await new Promise((resolve) => {
|
||||||
let totalHeight = 0;
|
let totalHeight = 0;
|
||||||
console.log('autoScroll...');
|
const distance = 100; // distanza dello scroll ad ogni intervallo
|
||||||
const distance = 100;
|
const delay = 40; // tempo di intervallo in ms
|
||||||
const timer = setInterval(() => {
|
|
||||||
|
const scroll = async () => {
|
||||||
|
// Esegui lo scroll
|
||||||
window.scrollBy(0, distance);
|
window.scrollBy(0, distance);
|
||||||
totalHeight += distance;
|
totalHeight += distance;
|
||||||
|
|
||||||
if (totalHeight >= document.body.scrollHeight) {
|
// Verifica se è stato scrollato tutto il corpo della pagina
|
||||||
clearInterval(timer);
|
if (totalHeight < document.body.scrollHeight) {
|
||||||
resolve();
|
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 = {}) {
|
async generatePdfFromUrl(url, filenameOut, options = {}) {
|
||||||
@@ -77,7 +86,14 @@ class GenPdf {
|
|||||||
try {
|
try {
|
||||||
console.log(`caricamento pagina: ${url}`);
|
console.log(`caricamento pagina: ${url}`);
|
||||||
await page.goto(url, { waitUntil: 'networkidle0' });
|
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 success = false;
|
||||||
let numTentativi1 = 0;
|
let numTentativi1 = 0;
|
||||||
while (numTentativi1 < maxTentativi) {
|
while (numTentativi1 < maxTentativi) {
|
||||||
@@ -166,9 +182,6 @@ class GenPdf {
|
|||||||
|
|
||||||
console.log(`PDF finale generato: ${finalFilePath}`);
|
console.log(`PDF finale generato: ${finalFilePath}`);
|
||||||
|
|
||||||
if (options.compressione) {
|
|
||||||
}
|
|
||||||
|
|
||||||
return finalFilePath;
|
return finalFilePath;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Errore durante generazione PDF:', error);
|
console.error('Errore durante generazione PDF:', error);
|
||||||
@@ -394,7 +407,12 @@ class GenPdf {
|
|||||||
const catalog = await Catalog.findById(options.idCatalog);
|
const catalog = await Catalog.findById(options.idCatalog);
|
||||||
if (catalog) {
|
if (catalog) {
|
||||||
const url =
|
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) {
|
if (url) {
|
||||||
const myfilenameout = await this.getPathByPage(catalog.idPageAssigned);
|
const myfilenameout = await this.getPathByPage(catalog.idPageAssigned);
|
||||||
@@ -419,6 +437,7 @@ class GenPdf {
|
|||||||
fileout_compressed: filenamerelative_compressed,
|
fileout_compressed: filenamerelative_compressed,
|
||||||
filesize: await tools.getSizeFile(fullnamepath),
|
filesize: await tools.getSizeFile(fullnamepath),
|
||||||
filesize_compressed: await tools.getSizeFile(fullnamepath_compr),
|
filesize_compressed: await tools.getSizeFile(fullnamepath_compr),
|
||||||
|
error: '',
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Errore durante la generazione del PDF dal catalogo ID:', 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') {
|
async compressPdf(inputPath, outputPath, compressione = 'printer') {
|
||||||
@@ -439,7 +458,7 @@ class GenPdf {
|
|||||||
const outputFile = path.resolve(outputPath);
|
const outputFile = path.resolve(outputPath);
|
||||||
|
|
||||||
const validQualities = ['screen', 'ebook', 'printer', 'prepress', 'default'];
|
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)
|
// Comando Ghostscript per compressione - impostazione per web (/screen)
|
||||||
const gsCommand = `gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/${compressione} -dNOPAUSE -dBATCH -dQUIET -sOutputFile="${outputFile}" "${inputFile}"`;
|
const gsCommand = `gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/${compressione} -dNOPAUSE -dBATCH -dQUIET -sOutputFile="${outputFile}" "${inputFile}"`;
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
1.2.50
|
1.2.52
|
||||||
Reference in New Issue
Block a user