- versione 1.2.50
This commit is contained in:
@@ -76,7 +76,13 @@ const CatalogSchema = new Schema({
|
|||||||
backcolor: String,
|
backcolor: String,
|
||||||
|
|
||||||
pdf_generato: String,
|
pdf_generato: String,
|
||||||
|
pdf_generato_compressed: String,
|
||||||
|
pdf_generato_size: String,
|
||||||
|
pdf_generato_compr_size: String,
|
||||||
pdf_generato_stampa: String,
|
pdf_generato_stampa: String,
|
||||||
|
pdf_generato_stampa_compressed: String,
|
||||||
|
pdf_generato_stampa_compr_size: String,
|
||||||
|
pdf_generato_stampa_size: String,
|
||||||
data_generato: {
|
data_generato: {
|
||||||
type: Date,
|
type: Date,
|
||||||
},
|
},
|
||||||
@@ -90,10 +96,12 @@ const CatalogSchema = new Schema({
|
|||||||
type: Date,
|
type: Date,
|
||||||
},
|
},
|
||||||
pdf_online: String,
|
pdf_online: String,
|
||||||
|
pdf_online_size: String,
|
||||||
data_online: {
|
data_online: {
|
||||||
type: Date,
|
type: Date,
|
||||||
},
|
},
|
||||||
pdf_online_stampa: String,
|
pdf_online_stampa: String,
|
||||||
|
pdf_online_stampa_size: String,
|
||||||
data_online_stampa: {
|
data_online_stampa: {
|
||||||
type: Date,
|
type: Date,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ const RaccoltaCataloghiSchema = new Schema({
|
|||||||
|
|
||||||
nomefile_da_generare: String,
|
nomefile_da_generare: String,
|
||||||
|
|
||||||
pdf_generato: String,
|
pdf_generato_size: String,
|
||||||
pdf_generato_stampa: String,
|
pdf_generato_stampa: String,
|
||||||
data_generato: {
|
data_generato: {
|
||||||
type: Date,
|
type: Date,
|
||||||
@@ -46,10 +46,12 @@ const RaccoltaCataloghiSchema = new Schema({
|
|||||||
type: Date,
|
type: Date,
|
||||||
},
|
},
|
||||||
pdf_online: String,
|
pdf_online: String,
|
||||||
|
pdf_online_size: String,
|
||||||
data_online: {
|
data_online: {
|
||||||
type: Date,
|
type: Date,
|
||||||
},
|
},
|
||||||
pdf_online_stampa: String,
|
pdf_online_stampa: String,
|
||||||
|
pdf_online_stampa_size: String,
|
||||||
data_online_stampa: {
|
data_online_stampa: {
|
||||||
type: Date,
|
type: Date,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -163,7 +163,17 @@ async function compressPdf(inputFile, outputFile, compressione) {
|
|||||||
const validSettings = ['screen', 'ebook', 'printer', 'prepress', 'default'];
|
const validSettings = ['screen', 'ebook', 'printer', 'prepress', 'default'];
|
||||||
if (!validSettings.includes(compressione)) {
|
if (!validSettings.includes(compressione)) {
|
||||||
console.warn(`Compressione '${compressione}' non valida, uso 'ebook'`);
|
console.warn(`Compressione '${compressione}' non valida, uso 'ebook'`);
|
||||||
compression = 'ebook';
|
compressione = 'ebook';
|
||||||
|
}
|
||||||
|
|
||||||
|
const origSize = await tools.getSizeFile(inputFile);
|
||||||
|
|
||||||
|
let add = '';
|
||||||
|
|
||||||
|
// Eventualmente......
|
||||||
|
const in300dpi = false;
|
||||||
|
if (in300dpi) {
|
||||||
|
add += '-dDPIx=300 -dDPIy=300';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Costruisci comando Ghostscript per la compressione PDF
|
// Costruisci comando Ghostscript per la compressione PDF
|
||||||
@@ -177,6 +187,7 @@ async function compressPdf(inputFile, outputFile, compressione) {
|
|||||||
'-dBATCH',
|
'-dBATCH',
|
||||||
`-sOutputFile="${outputFile}"`,
|
`-sOutputFile="${outputFile}"`,
|
||||||
`"${inputFile}"`,
|
`"${inputFile}"`,
|
||||||
|
'-dDownsampleColorImages=true -dColorImageResolution=72',
|
||||||
].join(' ');
|
].join(' ');
|
||||||
|
|
||||||
console.log('Compressione del PDF in corso...');
|
console.log('Compressione del PDF in corso...');
|
||||||
@@ -186,15 +197,34 @@ async function compressPdf(inputFile, outputFile, compressione) {
|
|||||||
await execPromise(gsCommand);
|
await execPromise(gsCommand);
|
||||||
|
|
||||||
// Calcola la percentuale di compressione
|
// Calcola la percentuale di compressione
|
||||||
const origSize = (await fs.promises.stat(inputFile)).size;
|
const newSize = await tools.getSizeFile(outputFile);
|
||||||
const newSize = (await fs.promises.stat(outputFile)).size;
|
|
||||||
const percent = Math.round(((origSize - newSize) / origSize) * 100);
|
const percent = Math.round(((origSize - newSize) / origSize) * 100);
|
||||||
console.log(`Il file compresso è circa il ${percent}% più piccolo dell'originale`);
|
console.log(`Il file compresso è circa il ${percent}% più piccolo dell'originale`);
|
||||||
|
|
||||||
console.log(`PDF compresso e salvato come '${outputFile}'`);
|
console.log(`PDF compresso e salvato come '${outputFile}'`);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Errore durante la compressione:', error);
|
console.error('Errore durante la compressione:', error);
|
||||||
throw error; // Propaga l'errore
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function compressPdfWithPs2Pdf(inputFile, outputFile, compression = 'ebook') {
|
||||||
|
try {
|
||||||
|
// Verifica se il file di input esiste
|
||||||
|
if (!(await tools.isFileExistsAsync(inputFile))) {
|
||||||
|
console.error(`Il file di input '${inputFile}' non esiste`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Costruisci il comando ps2pdf
|
||||||
|
const ps2pdfCommand = `ps2pdf -dPDFSETTINGS=/${compression} "${inputFile}" "${outputFile}"`;
|
||||||
|
|
||||||
|
console.log(`Eseguendo: ${ps2pdfCommand}`);
|
||||||
|
|
||||||
|
// Esegui il comando ps2pdf
|
||||||
|
await execPromise(ps2pdfCommand);
|
||||||
|
|
||||||
|
console.log(`PDF compresso con successo: ${outputFile}`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Errore durante la compressione del PDF:', error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -331,10 +361,11 @@ async function ConvertPDF_Generatore(options, instampa) {
|
|||||||
let marginLeft = instampa ? parseFloat(options.print_left) : 0;
|
let marginLeft = instampa ? parseFloat(options.print_left) : 0;
|
||||||
|
|
||||||
if (instampa) {
|
if (instampa) {
|
||||||
suffisso_filename = '-stampabile';
|
suffisso_filename = '-stampabile_generato';
|
||||||
|
} else {
|
||||||
|
suffisso_filename = '_generato';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let outputFilename = suffisso_filename
|
let outputFilename = suffisso_filename
|
||||||
? tools.aggiungiSuffissoAlNomeFile(options.filenameOut, suffisso_filename)
|
? tools.aggiungiSuffissoAlNomeFile(options.filenameOut, suffisso_filename)
|
||||||
: options.filenameOut;
|
: options.filenameOut;
|
||||||
@@ -396,13 +427,18 @@ async function ConvertPDF_Generatore(options, instampa) {
|
|||||||
fileout = outputFullPathFileName;
|
fileout = outputFullPathFileName;
|
||||||
extractPdfInfo(fileout);
|
extractPdfInfo(fileout);
|
||||||
|
|
||||||
|
// Elimina il file compresso precedente...
|
||||||
|
fileOLD_compressed = tools.removeFileExtension(fileout) + `_compressed.pdf`;
|
||||||
|
if (await tools.isFileExistsAsync(fileOLD_compressed)) {
|
||||||
|
await tools.deleteFile(fileOLD_compressed);
|
||||||
|
}
|
||||||
|
|
||||||
if (options.compressione) {
|
if (options.compressione) {
|
||||||
const mostrainfo = true;
|
const mostrainfo = true;
|
||||||
// const todayDate = tools.getDateYYYYMMDD_Today();
|
|
||||||
//const compressed = tools.removeFileExtension(outputFile) + `_${todayDate}.pdf`;
|
|
||||||
fileout_compressed = tools.removeFileExtension(fileout) + `_compressed.pdf`;
|
fileout_compressed = tools.removeFileExtension(fileout) + `_compressed.pdf`;
|
||||||
|
|
||||||
await compressPdf(fileout, fileout_compressed, options.compressione);
|
// await compressPdf(fileout, fileout_compressed, options.compressione);
|
||||||
|
await compressPdfWithPs2Pdf(fileout, fileout_compressed, options.compressione);
|
||||||
|
|
||||||
// if (mostrainfo) extractPdfInfo(fileout_compressed);
|
// if (mostrainfo) extractPdfInfo(fileout_compressed);
|
||||||
}
|
}
|
||||||
@@ -419,6 +455,8 @@ async function ConvertPDF_Generatore(options, instampa) {
|
|||||||
return {
|
return {
|
||||||
fileout: tools.removePathDirByFileName(options.idapp, fileout),
|
fileout: tools.removePathDirByFileName(options.idapp, fileout),
|
||||||
fileout_compressed: tools.removePathDirByFileName(options.idapp, fileout_compressed),
|
fileout_compressed: tools.removePathDirByFileName(options.idapp, fileout_compressed),
|
||||||
|
filesize: await tools.getSizeFile(fileout),
|
||||||
|
filesize_compressed: fileout_compressed ? await tools.getSizeFile(fileout_compressed) : 0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -429,6 +467,7 @@ router.post('/online-pdf', authenticate, async (req, res) => {
|
|||||||
id_catalog = req.body.id_catalog;
|
id_catalog = req.body.id_catalog;
|
||||||
id_raccolta = req.body.id_raccolta;
|
id_raccolta = req.body.id_raccolta;
|
||||||
stampa = req.body.stampa;
|
stampa = req.body.stampa;
|
||||||
|
compresso = req.body.compresso;
|
||||||
let mydir = '';
|
let mydir = '';
|
||||||
let risout = {};
|
let risout = {};
|
||||||
|
|
||||||
@@ -442,7 +481,7 @@ router.post('/online-pdf', authenticate, async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (myrec) {
|
if (myrec) {
|
||||||
mydir = tools.getdirByIdApp(idapp);
|
mydir = tools.getdirByIdApp(idapp) + '/';
|
||||||
// const baseurl = this.getHostByIdApp(idapp);
|
// const baseurl = this.getHostByIdApp(idapp);
|
||||||
|
|
||||||
if (stampa) {
|
if (stampa) {
|
||||||
@@ -452,12 +491,27 @@ router.post('/online-pdf', authenticate, async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Aggiorna il PDF OnLine, copiando il file da Generato a OnLine
|
// Aggiorna il PDF OnLine, copiando il file da Generato a OnLine
|
||||||
const fileOrigin = mydir + (stampa ? myrec.pdf_generato_stampa : myrec.pdf_generato);
|
let fileOrigin = mydir + (stampa ? myrec.pdf_generato_stampa : myrec.pdf_generato);
|
||||||
|
|
||||||
|
if (compresso) {
|
||||||
|
let fileInCompressed = tools.removeFileExtension(fileOrigin) + `_compressed.pdf`;
|
||||||
|
// Se esiste allora prende questo COmpresso !
|
||||||
|
if (await tools.isFileExistsAsync(fileInCompressed)) {
|
||||||
|
fileOrigin = fileInCompressed;
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
}
|
||||||
const fileDest = mydir + (stampa ? myrec.pdf_online_stampa : myrec.pdf_online);
|
const fileDest = mydir + (stampa ? myrec.pdf_online_stampa : myrec.pdf_online);
|
||||||
const fileDestNoDir = stampa ? myrec.pdf_online_stampa : myrec.pdf_online;
|
const fileDestNoDir = stampa ? myrec.pdf_online_stampa : myrec.pdf_online;
|
||||||
// copia il file
|
// copia il file
|
||||||
await fs.promises.copyFile(fileOrigin, fileDest);
|
await fs.promises.copyFile(fileOrigin, fileDest);
|
||||||
|
|
||||||
|
if (stampa) {
|
||||||
|
myrec.pdf_online_stampa_size = await tools.getSizeFile(fileDest);
|
||||||
|
} else {
|
||||||
|
myrec.pdf_online_size = await tools.getSizeFile(fileDest);
|
||||||
|
}
|
||||||
|
|
||||||
if (stampa) {
|
if (stampa) {
|
||||||
myrec.data_online_stampa = myrec.data_generato_stampa;
|
myrec.data_online_stampa = myrec.data_generato_stampa;
|
||||||
} else {
|
} else {
|
||||||
@@ -561,6 +615,7 @@ router.post('/join-pdf', authenticate, async (req, res) => {
|
|||||||
const ris = await JoinPDFCatalogs(cataloghi, options, outputFile, false);
|
const ris = await JoinPDFCatalogs(cataloghi, options, outputFile, false);
|
||||||
if (ris) {
|
if (ris) {
|
||||||
raccolta.pdf_generato = ris.outputPath;
|
raccolta.pdf_generato = ris.outputPath;
|
||||||
|
raccolta.pdf_generato_size = await tools.getSizeFile(ris.outputPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -572,6 +627,14 @@ router.post('/join-pdf', authenticate, async (req, res) => {
|
|||||||
}
|
}
|
||||||
const fileDest = options.mydir + raccolta.pdf_online;
|
const fileDest = options.mydir + raccolta.pdf_online;
|
||||||
await fs.promises.writeFile(fileDest, pdfBytes);
|
await fs.promises.writeFile(fileDest, pdfBytes);
|
||||||
|
|
||||||
|
let size = await tools.getSizeFile(FileDest);
|
||||||
|
|
||||||
|
if (options.stampa) {
|
||||||
|
raccolta.pdf_online_stampa_size = size;
|
||||||
|
} else {
|
||||||
|
raccolta.pdf_online = size;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
raccolta.save();
|
raccolta.save();
|
||||||
|
|||||||
@@ -6124,7 +6124,7 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
return filename;
|
return filename;
|
||||||
},
|
},
|
||||||
|
|
||||||
isDateValid(mydate) {
|
isDateValid(mydate) {
|
||||||
try {
|
try {
|
||||||
return (
|
return (
|
||||||
@@ -6135,6 +6135,22 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
async getSizeFile(filename) {
|
||||||
|
try {
|
||||||
|
if (await this.isFileExistsAsync(filename)) {
|
||||||
|
const stats = fs.statSync ? fs.statSync(filename) : null;
|
||||||
|
const fileSizeInBytes = stats.size;
|
||||||
|
const fileSizeInMB = fileSizeInBytes / (1024 * 1024);
|
||||||
|
return fileSizeInMB.toFixed(2); // Returns size in MB with 2 decimal places
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error('Error getting file size:', e);
|
||||||
|
return '0.00';
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
aggiungiSuffissoAlNomeFile(filePath, suffisso) {
|
aggiungiSuffissoAlNomeFile(filePath, suffisso) {
|
||||||
const dir = path.dirname(filePath);
|
const dir = path.dirname(filePath);
|
||||||
const estensione = path.extname(filePath);
|
const estensione = path.extname(filePath);
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
1.2.49
|
1.2.50
|
||||||
Reference in New Issue
Block a user