@@ -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 ) ;