- Generazione PDF e per Stampa
This commit is contained in:
@@ -69,9 +69,13 @@ const CatalogSchema = new Schema({
|
|||||||
},
|
},
|
||||||
|
|
||||||
pdf_generato: String,
|
pdf_generato: String,
|
||||||
|
pdf_generato_stampa: String,
|
||||||
data_generato: {
|
data_generato: {
|
||||||
type: Date,
|
type: Date,
|
||||||
},
|
},
|
||||||
|
data_generato_stampa: {
|
||||||
|
type: Date,
|
||||||
|
},
|
||||||
username_lista_generata: {
|
username_lista_generata: {
|
||||||
type: String,
|
type: String,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ var { authenticate } = require('../middleware/authenticate');
|
|||||||
const multer = require('multer');
|
const multer = require('multer');
|
||||||
|
|
||||||
const DIR_PDF_IN = __dirname + '/upload/files_input/';
|
const DIR_PDF_IN = __dirname + '/upload/files_input/';
|
||||||
const DIR_PDF_OUT = __dirname + '/upload/files_output/';
|
// const DIR_PDF_OUT = __dirname + '/upload/files_output/';
|
||||||
|
|
||||||
const upload = multer({ dest: DIR_PDF_IN });
|
const upload = multer({ dest: DIR_PDF_IN });
|
||||||
|
|
||||||
@@ -301,7 +301,8 @@ async function convertPDF_PdfLib(idapp, inputFile, outputFile, options) {
|
|||||||
let fileout = outputFile;
|
let fileout = outputFile;
|
||||||
|
|
||||||
if (options.compressione) {
|
if (options.compressione) {
|
||||||
const compressed = tools.removeFileExtension(outputFile) + '-compressed.pdf';
|
const todayDate = tools.getDateYYYYMMDD_Today();
|
||||||
|
const compressed = tools.removeFileExtension(outputFile) + `_${todayDate}.pdf`;
|
||||||
|
|
||||||
await compressPdf(outputFile, compressed, options.compressione);
|
await compressPdf(outputFile, compressed, options.compressione);
|
||||||
|
|
||||||
@@ -311,15 +312,17 @@ async function convertPDF_PdfLib(idapp, inputFile, outputFile, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (options.dir_out && options.idapp) {
|
if (options.dir_out && options.idapp) {
|
||||||
|
// Crea directory se non esiste !
|
||||||
// Salva il fileout anche su questa directory dir_out
|
// Salva il fileout anche su questa directory dir_out
|
||||||
options.dir_out = tools.getdirByIdApp(options.idapp) + '/' + options.dir_out;
|
|
||||||
const fileoutdir = path.join(options.dir_out, options.file_out);
|
//const fileoutdir = path.join(options.dir_out, options.file_out);
|
||||||
await fs.promises.copyFile(fileout, fileoutdir);
|
//await fs.promises.copyFile(fileout, fileoutdir);
|
||||||
console.log(`File ${fileout} anche salvato in ${options.dir_out}`);
|
//console.log(`File ${fileout} anche salvato in ${options.dir_out}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
let fileout_print = '';
|
let fileout_print = '';
|
||||||
|
|
||||||
|
|
||||||
// Crea anche il PDF per la stampa
|
// Crea anche il PDF per la stampa
|
||||||
if (
|
if (
|
||||||
options.print_left !== 0 ||
|
options.print_left !== 0 ||
|
||||||
@@ -331,7 +334,12 @@ async function convertPDF_PdfLib(idapp, inputFile, outputFile, options) {
|
|||||||
ris = await ConvertPDF_WithMargins(options);
|
ris = await ConvertPDF_WithMargins(options);
|
||||||
}
|
}
|
||||||
|
|
||||||
return { fileout, fileout_print };
|
const uscita = {
|
||||||
|
pdf_generato: tools.removePathDirByFileName(options.idapp, fileout),
|
||||||
|
pdf_generato_stampa: tools.removePathDirByFileName(options.idapp, ris.fileout_print),
|
||||||
|
}
|
||||||
|
|
||||||
|
return uscita;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Errore: ', e.message);
|
console.error('Errore: ', e.message);
|
||||||
return '';
|
return '';
|
||||||
@@ -438,19 +446,21 @@ router.post('/convert-pdf', upload.single('pdf'), async (req, res) => {
|
|||||||
return res.status(400).send('Width parameter is required');
|
return res.status(400).send('Width parameter is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
let outputFile = path.join(DIR_PDF_OUT, `${tools.removeFileExtension(req.file.originalname)}-converted.pdf`);
|
options.dir_out = tools.getdirByIdApp(options.idapp) + '/' + options.dir_out;
|
||||||
|
// let outputFile = path.join(options.dir_out, `${tools.removeFileExtension(req.file.originalname)}-converted.pdf`);
|
||||||
|
let outputFile = path.join(options.dir_out, `${options.file_out}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fs.promises.mkdir(DIR_PDF_IN, { recursive: true });
|
await fs.promises.mkdir(DIR_PDF_IN, { recursive: true });
|
||||||
await fs.promises.mkdir(DIR_PDF_OUT, { recursive: true });
|
await fs.promises.mkdir(options.dir_out, { recursive: true });
|
||||||
|
|
||||||
// Converti il PDF
|
// Converti il PDF
|
||||||
// await convertPDF_GS(inputFile, outputFile, width, height);
|
// await convertPDF_GS(inputFile, outputFile, width, height);
|
||||||
const fileout = await convertPDF_PdfLib(idapp, inputFile, outputFile, options);
|
const risout = await convertPDF_PdfLib(idapp, inputFile, outputFile, options);
|
||||||
|
|
||||||
if (!options.dir_out) {
|
if (!options.dir_out) {
|
||||||
// Invia il file convertito
|
// Invia il file convertito
|
||||||
res.download(fileout, 'output-converted.pdf', (err) => {
|
res.download(risout.pdf_generato, 'output-converted.pdf', (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err.code === 'ECONNABORTED' || err.code === 'ECONNRESET') {
|
if (err.code === 'ECONNABORTED' || err.code === 'ECONNRESET') {
|
||||||
console.warn('Richiesta annullata dal client:', err.message);
|
console.warn('Richiesta annullata dal client:', err.message);
|
||||||
@@ -462,17 +472,17 @@ router.post('/convert-pdf', upload.single('pdf'), async (req, res) => {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Rimuovi i file temporanei
|
// Rimuovi i file temporanei
|
||||||
cleanupFiles(inputFile, outputFile);
|
cleanupFiles(inputFile, '');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
cleanupFiles(inputFile, outputFile);
|
cleanupFiles(inputFile, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
return res.status(200).send({ fileout });
|
return res.status(200).send(risout);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Errore durante la conversione:', error);
|
console.error('Errore durante la conversione:', error);
|
||||||
cleanupFiles(inputFile, outputFile);
|
cleanupFiles(inputFile, '');
|
||||||
if (!res.headersSent) {
|
if (!res.headersSent) {
|
||||||
res.status(500).send(`Errore durante la conversione del PDF: ${error.message}`);
|
res.status(500).send(`Errore durante la conversione del PDF: ${error.message}`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6268,7 +6268,32 @@ module.exports = {
|
|||||||
return Array.from(mystr).map(char => accentsMap.get(char) || char).join('');
|
return Array.from(mystr).map(char => accentsMap.get(char) || char).join('');
|
||||||
},
|
},
|
||||||
|
|
||||||
|
getDateYYYYMMDD_Today() {
|
||||||
|
// Ottieni la data attuale nel formato YYYY-MM-DD
|
||||||
|
const today = new Date();
|
||||||
|
const formattedDate = today.toISOString().split('T')[0]; // Format YYYY-MM-DD
|
||||||
|
return formattedDate
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
convertFullFileNameToURL(idapp, fullfilename) {
|
||||||
|
const mydir = this.getdirByIdApp(idapp);
|
||||||
|
const myhost = this.getHostByIdApp(idapp);
|
||||||
|
|
||||||
|
// ++ remove mydir from fullfilename and add myhost to generate a URL
|
||||||
|
const url = fullfilename.replace(mydir, '').replace(/\\/g, '/'); // Replace backslashes with slashes
|
||||||
|
const myurl = myhost + '/' + url;
|
||||||
|
|
||||||
|
console.log('myurl', myurl);
|
||||||
|
return myurl;
|
||||||
|
},
|
||||||
|
|
||||||
|
removePathDirByFileName(idapp, fullfilename) {
|
||||||
|
const mydir = this.getdirByIdApp(idapp);
|
||||||
|
|
||||||
|
// ++ remove mydir from fullfilename and add myhost to generate a URL
|
||||||
|
const filename = fullfilename.replace(mydir, '').replace(/\\/g, '/'); // Replace backslashes with slashes
|
||||||
|
return filename;
|
||||||
|
},
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user