MuhammaraJS icon indicating copy to clipboard operation
MuhammaraJS copied to clipboard

EditPage on loop

Open xLEpEX opened this issue 2 years ago • 1 comments

Hello, i Would like to know if i'm using the Recip object correctly. I want to loop over every page of my pdf document to add a text, for exmaple the page number to the document, but when im using the loop at the following example. The saved out put buffer is still the same. Im also using the createReader since i'm not able to get the page count over the recip.



const writeTest = (pdfBuffer: Buffer) => {
        this.logger.log("write file")
        fs.writeFileSync('./pdfs/BaseWithTestText.pdf', pdfBuffer);
    }

const addPageNumber = (pdfBuffer: Buffer, skipFirstPage?: boolean) => {
        //create readStream from Buffer
        const readBuffer = new PDFRStreamForBuffer(pdfBuffer);

        //create readStream to get Page Count
        const pdfReader = createReader(readBuffer)

        //create recipe
        const recipe = new Recipe(pdfBuffer);

        //loop over pages at index 1
        for(let i = 1; i == pdfReader.getPagesCount(); i++) {
            recipe.editPage(i).text("Test funktioniert das", 42, 42).endPage();
        }
        //save updated pdf to file system
        recipe.endPDF((cb) => this.writeTest(cb));
    }

const main = () => {
        const buffer = fs.readFileSync('./pdfs/Base.pdf');
        addPageNumber(buffer);
}

main();

xLEpEX avatar Sep 29 '23 11:09 xLEpEX

Your for statement is malformed. You need i <= pdfReader.getPagesCount().

ericbf avatar Jun 07 '25 01:06 ericbf