NischchalVashisht / pdfMerger

1 stars 4 forks source link

How to add all pages from merger pdf #1

Open Chetan0602 opened 1 year ago

Chetan0602 commented 1 year ago

i m merging 4 pdf into one pdf. pdf merging successfully but only first page is merge other pages are skip. how to merge all pages?

teju2980 commented 10 months ago

Hi Chetan,

Replace createPDF() method's if condition with the below code to merge all the pages of multiple PDF files.

    if (this.docData.length > 1) {
        for (let i = 1; i < this.docData.length; i++) {
            tempBytes = Uint8Array.from(atob(this.docData[i]), (c) => c.charCodeAt(0));
            console.log('tempBtes>> ', tempBytes);

            const usConstitutionPdf = await PDFLib.PDFDocument.load(tempBytes);
            console.log('After123 ', usConstitutionPdf.getPages().length)
            for(let j =0; j<usConstitutionPdf.getPages().length; j++){
                page = pdfDoc.addPage();
                const preamble = await pdfDoc.embedPage(usConstitutionPdf.getPages()[j]);
                console.log(' Inside page is ', page)

                const preambleDims = preamble.scale(0.95);

                page.drawPage(preamble, {
                    ...preambleDims,
                    x: page.getWidth() - americanFlagDims.width,
                    y: page.getHeight() - americanFlagDims.height - 10,
                });
            }

        }

    }