julianhille / MuhammaraJS

Muhammara a node module with c/cpp bindings to modify PDF with js for node or electron (based/replacement on/of galkhana/hummusjs)
Other
205 stars 43 forks source link

EditPage on loop #331

Open xLEpEX opened 9 months ago

xLEpEX commented 9 months ago

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