Vishal Chauhan
Vishal Chauhan

Reputation: 35

PDFKIT over-flow data goes to newpage for each row

I have a function that generates a row of data depending on an array. Works perfectly fine for the first page but as soon as the data is overflowed somewhere around doc.text("exmaple",70,560) When the Y coordinate is set to 560 it goes to next page. But further to that the next iteration should result into doc.text("example2,70,600) goes into 3rd page and 4th page for the next item and so on. This behaviour is not expected as it should create a new row in the second page. Full code is listed below. I am not expert at JS and node so maybe making some silly mistake. The issue starts at around the 10th item of array or y coordinate being 560 as mentioned above

const doc = new PDFDocument({
    layout: 'landscape'
});
doc.pipe(fs.createWriteStream('output.pdf'));

const productCodes = ['JD100979758','JD4010069','JD5879085','JD7515824','JD7517614','JD100979758','JD4010069','JD5879085','JD7515824','JD7517614','JD100979758','JD4010069','JD5879085','JD7515824','JD7517614','JD100979758','JD4010069','JD5879085','JD7515824','JD7517614','JD100979758','JD4010069','JD5879085','JD7515824','JD7517614','JD100979758','JD4010069','JD5879085','JD7515824','JD7517614','JD100979758','JD4010069','JD5879085','JD7515824','JD7517614']
function generateDexTable(doc,product){
    let i,
    invoiceTableTop= 160;
        for(let i = 0;i<product.length;i++){
            const item = product[i]
            const position = invoiceTableTop+(i*40);
            doc.text(item,70,position)
        }
}
generateDexTable(doc,productCodes)

I have genuinely created a redundant array to simulate this error. This creates a pdf output of 26 pages where expected is much less. I want essentially to create a kind of invoice via this kit.

Thanks

Upvotes: 2

Views: 1885

Answers (1)

eol
eol

Reputation: 24555

I think you can simplify this by using the default height for the text-blocks and moveDown():

function generateDexTable(doc, product) {

    for (let i = 0; i < product.length; i++) {
        const item = product[i];
        doc.text(item);
        doc.moveDown(3); // you can adjust this to get the desired margins between text blocks
    }
    doc.end();
}

Upvotes: 1

Related Questions