Nodejs: Loop Through req.files and Parse PDFs to get Dimensions and write them to the req.files objects

30 Views Asked by At

I try to Loop through req.files to get the dimensions of each Page. The Problem is to write back the dimensions into the req.files objects. I need the dimensions at the next function on my route, but I dont know how to do this with this type of code: "pdfParser.on("pdfParser_dataReady", pdfData => {"

I think i have to Promisify this funtion, but i dont know how to do this. Please I need Help !

exports.getPdfDimensions = async (req, res, next) => {
    // console.log("pdfParser_dataReady...");
    try {
        const pdfParser = new PDFParser(this, 1);

        for (let index = 0; index < req.files.length; index++) {
            const ulelement =  req.files[index];
                console.log(ulelement);
                pdfParser.on("pdfParser_dataReady", pdfData => {
                    var pages;
                    pages = pdfData.formImage.Pages.length;
                    for (let index = 0; index < pages; index++) {
                        var width = pdfData.formImage.Width / 4.5; // pdf width
                        var height = pdfData.formImage.Pages[index].Height / 4.5; // page height
                        let page = index + 1;
                        let heightinCM = Math.round(height * 2.54 * 10);
                        let widthinCM = Math.round(width * 2.54 * 10);
                        console.log(`Page : ${page}`);
                        // console.log(`Height : ${height} in inch`);
                        console.log(`Height : ${heightinCM} in mm`);
                        // console.log(`Width : ${width} in inch`);
                        console.log(`Width : ${widthinCM} in mm`);

                        (ulelement.filepages = ulelement.filepages || []).push({"file": index, "pagenr": page , "width": widthinCM , "height": heightinCM });
                    }
                });
                pdfParser.loadPDF(ulelement.path);
        }
        next();
    } catch (error) {
        // next(error);
        console.log(error);
    }

};
0

There are 0 best solutions below