Creazione Nutriben-natoropatia.it
This commit is contained in:
@@ -40,7 +40,7 @@
|
||||
"lodash": "^4.17.21",
|
||||
"mongodb": "^6.14.2",
|
||||
"mongoose": "^8.12.1",
|
||||
"multer": "^1.4.5-lts.1",
|
||||
"multer": "^1.4.5-lts.2",
|
||||
"mysql": "^2.18.1",
|
||||
"node-cron": "^3.0.3",
|
||||
"node-emoji": "^2.2.0",
|
||||
|
||||
@@ -248,7 +248,7 @@ async function extractPdfInfo(inputFile) {
|
||||
console.log('Page Dimensions (in points):', dpiInfo.pageInfo);
|
||||
}
|
||||
|
||||
async function convertPDF_PdfLib(idapp, inputFile, outputFile, width, height, compressione, dir_out, file_out) {
|
||||
async function convertPDF_PdfLib(idapp, inputFile, outputFile, options) {
|
||||
if (!tools.isFileExists(inputFile)) {
|
||||
throw new Error(`Il file di input non esiste: ${inputFile}`);
|
||||
}
|
||||
@@ -267,8 +267,8 @@ async function convertPDF_PdfLib(idapp, inputFile, outputFile, width, height, co
|
||||
const { width: originalWidth, height: originalHeight } = page.getSize();
|
||||
|
||||
// Calcola la larghezza e l'altezza in punti
|
||||
const newWidth = width * 72; // Convertito in punti
|
||||
const newHeight = height * 72; // Convertito in punti
|
||||
const newWidth = options.width * 72; // Convertito in punti
|
||||
const newHeight = options.height * 72; // Convertito in punti
|
||||
|
||||
// Crea una nuova pagina con le dimensioni specificate
|
||||
const newPage = newPdfDoc.addPage([newWidth, newHeight]);
|
||||
@@ -300,31 +300,106 @@ async function convertPDF_PdfLib(idapp, inputFile, outputFile, width, height, co
|
||||
|
||||
let fileout = outputFile;
|
||||
|
||||
if (compressione) {
|
||||
if (options.compressione) {
|
||||
const compressed = tools.removeFileExtension(outputFile) + '-compressed.pdf';
|
||||
|
||||
await compressPdf(outputFile, compressed, compressione);
|
||||
await compressPdf(outputFile, compressed, options.compressione);
|
||||
|
||||
if (mostrainfo) extractPdfInfo(compressed);
|
||||
|
||||
fileout = compressed;
|
||||
}
|
||||
|
||||
if (dir_out && idapp) {
|
||||
if (options.dir_out && options.idapp) {
|
||||
// Salva il fileout anche su questa directory dir_out
|
||||
dir_out = tools.getdirByIdApp(idapp) + '/' + dir_out;
|
||||
const fileoutdir = path.join(dir_out, file_out);
|
||||
options.dir_out = tools.getdirByIdApp(options.idapp) + '/' + options.dir_out;
|
||||
const fileoutdir = path.join(options.dir_out, options.file_out);
|
||||
await fs.promises.copyFile(fileout, fileoutdir);
|
||||
console.log(`File ${fileout} anche salvato in ${dir_out}`);
|
||||
|
||||
console.log(`File ${fileout} anche salvato in ${options.dir_out}`);
|
||||
}
|
||||
|
||||
return fileout;
|
||||
let fileout_print = '';
|
||||
|
||||
// Crea anche il PDF per la stampa
|
||||
if (
|
||||
options.print_left !== 0 ||
|
||||
options.print_top !== 0 ||
|
||||
options.print_right !== 0 ||
|
||||
options.print_bottom !== 0
|
||||
) {
|
||||
options.filenameIn = fileout;
|
||||
ris = await ConvertPDF_WithMargins(options);
|
||||
}
|
||||
|
||||
return { fileout, fileout_print };
|
||||
} catch (e) {
|
||||
console.error('Errore: ', e.message);
|
||||
return '';
|
||||
}
|
||||
}
|
||||
async function ConvertPDF_WithMargins(options) {
|
||||
let fileout_print = '';
|
||||
|
||||
const marginTop = parseFloat(options.print_top) || 0;
|
||||
const marginRight = parseFloat(options.print_right) || 0;
|
||||
const marginBottom = parseFloat(options.print_bottom) || 0;
|
||||
const marginLeft = parseFloat(options.print_left) || 0;
|
||||
|
||||
if (!options.filenameIn) {
|
||||
return { err: 'Nessun file caricato.' };
|
||||
}
|
||||
|
||||
const outputFilename = path.basename(tools.removeFileExtension(options.filenameIn))+ '-stampabile.pdf';
|
||||
const outputPath = path.join(options.dir_out, outputFilename);
|
||||
|
||||
try {
|
||||
// Leggi il PDF originale
|
||||
const existingPdfBytes = await fs.promises.readFile(options.filenameIn);
|
||||
const srcPdfDoc = await PDFDocument.load(existingPdfBytes); // Documento sorgente
|
||||
const destPdfDoc = await PDFDocument.create(); // Nuovo documento vuoto
|
||||
|
||||
const pages = srcPdfDoc.getPages();
|
||||
|
||||
for (let i = 0; i < pages.length; i++) {
|
||||
const page = pages[i];
|
||||
const { width, height } = page.getSize();
|
||||
|
||||
const newWidth = width - marginLeft - marginRight;
|
||||
const newHeight = height - marginTop - marginBottom;
|
||||
|
||||
// Embed della pagina originale nel nuovo documento
|
||||
const embeddedPages = await destPdfDoc.embedPdf(srcPdfDoc, [i]); // Embeddiamo la pagina i-esima
|
||||
const embeddedPage = embeddedPages[0]; // Otteniamo l'oggetto embedded
|
||||
|
||||
// Aggiungi una nuova pagina alla stessa dimensione dell’originale
|
||||
const newPage = destPdfDoc.addPage([width, height]);
|
||||
|
||||
// Disegna la pagina embedded nella nuova pagina
|
||||
newPage.drawPage(embeddedPage, {
|
||||
x: marginLeft,
|
||||
y: marginBottom,
|
||||
width: newWidth,
|
||||
height: newHeight,
|
||||
});
|
||||
}
|
||||
|
||||
// Salva il nuovo PDF
|
||||
const pdfBytes = await destPdfDoc.save();
|
||||
|
||||
await fs.promises.mkdir(options.dir_out, { recursive: true });
|
||||
|
||||
await fs.promises.writeFile(outputPath, pdfBytes);
|
||||
|
||||
|
||||
fileout_print = outputPath;
|
||||
} catch (error) {
|
||||
const log = 'Errore durante la creazione del PDF per la Stampa:' + error.message;
|
||||
console.error(log);
|
||||
return { err: log, fileout_print: '' };
|
||||
}
|
||||
|
||||
return { fileout_print };
|
||||
}
|
||||
|
||||
// Endpoint POST per la conversione del PDF
|
||||
router.post('/convert-pdf', upload.single('pdf'), async (req, res) => {
|
||||
@@ -333,8 +408,30 @@ router.post('/convert-pdf', upload.single('pdf'), async (req, res) => {
|
||||
}
|
||||
|
||||
const inputFile = req.file.path;
|
||||
const { width, height, compressione, dir_out, file_out, idapp } = req.body;
|
||||
|
||||
const {
|
||||
width,
|
||||
height,
|
||||
compressione,
|
||||
dir_out,
|
||||
file_out,
|
||||
idapp,
|
||||
print_top = '0',
|
||||
print_right = '0',
|
||||
print_bottom = '0',
|
||||
print_left = '0',
|
||||
} = req.body;
|
||||
const options = {
|
||||
width,
|
||||
height,
|
||||
compressione,
|
||||
dir_out,
|
||||
file_out,
|
||||
idapp,
|
||||
print_top,
|
||||
print_right,
|
||||
print_bottom,
|
||||
print_left,
|
||||
};
|
||||
|
||||
if (!width) {
|
||||
fs.unlinkSync(inputFile);
|
||||
@@ -349,9 +446,9 @@ router.post('/convert-pdf', upload.single('pdf'), async (req, res) => {
|
||||
|
||||
// Converti il PDF
|
||||
// await convertPDF_GS(inputFile, outputFile, width, height);
|
||||
const fileout = await convertPDF_PdfLib(idapp, inputFile, outputFile, width, height, compressione, dir_out, file_out);
|
||||
const fileout = await convertPDF_PdfLib(idapp, inputFile, outputFile, options);
|
||||
|
||||
if (!dir_out) {
|
||||
if (!options.dir_out) {
|
||||
// Invia il file convertito
|
||||
res.download(fileout, 'output-converted.pdf', (err) => {
|
||||
if (err) {
|
||||
@@ -372,8 +469,7 @@ router.post('/convert-pdf', upload.single('pdf'), async (req, res) => {
|
||||
cleanupFiles(inputFile, outputFile);
|
||||
}
|
||||
|
||||
return res.status(200).send({fileout});
|
||||
|
||||
return res.status(200).send({ fileout });
|
||||
} catch (error) {
|
||||
console.error('Errore durante la conversione:', error);
|
||||
cleanupFiles(inputFile, outputFile);
|
||||
|
||||
@@ -2515,7 +2515,7 @@ function deleteFile(req, res, version) {
|
||||
const idapp = req.user.idapp;
|
||||
|
||||
if (!relativefile || relativefile.endsWith('/')) {
|
||||
res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -2530,12 +2530,12 @@ function deleteFile(req, res, version) {
|
||||
console.log('Delete file ' + relativefile);
|
||||
// ++ Move in the folder application !
|
||||
let fullpathfile = tools.getdirByIdApp(idapp) + dirmain + '/' +
|
||||
relativefile;
|
||||
relativefile.replace(/^\//, '');
|
||||
|
||||
tools.delete(fullpathfile, true, (err) => {
|
||||
if (err) console.log('err', err);
|
||||
if (err === undefined || err.errno === -2)
|
||||
res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
|
||||
return res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
|
||||
@@ -513,13 +513,14 @@ connectToDatabase(connectionUrl, options)
|
||||
let dirmain = '';
|
||||
let filefrom = '';
|
||||
let filefrom2 = '';
|
||||
let dir = tools.getdirByIdApp(idapp) + dirmain + '/upload/';
|
||||
|
||||
try {
|
||||
if (!tools.sulServer()) {
|
||||
dirmain = server_constants.DIR_PUBLIC_LOCALE;
|
||||
}
|
||||
|
||||
let dir = tools.getdirByIdApp(idapp) + dirmain + '/upload/';
|
||||
|
||||
for (const rec of arrlist) {
|
||||
const myuser = await User.findOne({ idapp, _id: rec.userId }).lean();
|
||||
if (myuser) {
|
||||
|
||||
@@ -1 +1 @@
|
||||
1.2.40
|
||||
1.2.42
|
||||
@@ -6081,10 +6081,10 @@ ms@2.1.3, ms@^2.0.0, ms@^2.1.1, ms@^2.1.3:
|
||||
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
|
||||
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==
|
||||
|
||||
multer@^1.4.5-lts.1:
|
||||
version "1.4.5-lts.1"
|
||||
resolved "https://registry.yarnpkg.com/multer/-/multer-1.4.5-lts.1.tgz#803e24ad1984f58edffbc79f56e305aec5cfd1ac"
|
||||
integrity sha512-ywPWvcDMeH+z9gQq5qYHCCy+ethsk4goepZ45GLD63fOu0YcNecQxi64nDs3qluZB+murG3/D4dJ7+dGctcCQQ==
|
||||
multer@^1.4.5-lts.2:
|
||||
version "1.4.5-lts.2"
|
||||
resolved "https://registry.yarnpkg.com/multer/-/multer-1.4.5-lts.2.tgz#340af065d8685dda846ec9e3d7655fcd50afba2d"
|
||||
integrity sha512-VzGiVigcG9zUAoCNU+xShztrlr1auZOlurXynNvO9GiWD1/mTBbUljOKY+qMeazBqXgRnjzeEgJI/wyjJUHg9A==
|
||||
dependencies:
|
||||
append-field "^1.0.0"
|
||||
busboy "^1.0.0"
|
||||
|
||||
Reference in New Issue
Block a user