Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loosing Data between two pages #2826

Open
DontShootMe opened this issue Dec 26, 2024 · 1 comment
Open

Loosing Data between two pages #2826

DontShootMe opened this issue Dec 26, 2024 · 1 comment

Comments

@DontShootMe
Copy link

DontShootMe commented Dec 26, 2024

Hello,

First of all, I want to thank you for your work!
really and sincerely a big Thank you.

I encounter a strange phenomenon.
You will be able to see it thanks to this piece of code in the playground.
Between 2 pages we lose 1 line of data

In my example I lose the lines:
9,19,29,39,49

This is mainly due to the use of the "pageBreakBefore" function which returns this behavior to me

// playground requires you to assign document definition to a variable called dd
let __content = [];
let i = 0;
for( i=0; i<50; i++){
    __content[i] = [{
        id   : `lig_${i}`,
        text :  `line ${i} => Line Content N° : ${i}\n\t\t\t\tMultiline : true\n\t\t\t\tpreserveLeadingSpaces : true`,
        preserveLeadingSpaces : true,
    }];
}
var dd = {
	content: [
	    {
	        table : {
	            dontBreakRows : true ,
	            body :  __content 
	        }
	    }
	],
	pageBreakBefore : function( currentNode ){
	     let limit = 430;
	     let horsLimit = currentNode.startPosition.top >= limit;
	     return horsLimit;
	}
	
};

Setting dontBreakRows to false bring long empty box, setting dontBreakRows to true bring this behavior !
What is the good way to do it ?

@DontShootMe DontShootMe changed the title Loosing Data between two page Loosing Data between two pages Dec 26, 2024
@suchislife801
Copy link

suchislife801 commented Dec 28, 2024

The issue occurs because the pageBreakBefore function is forcing breaks at a specific position (430) without properly considering the table row content. Here's a solution that should handle this better:

// playground requires you to assign document definition to a variable called dd
let __content = [];
for(let i = 0; i < 50; i++) {
    __content[i] = [{
        id: `lig_${i}`,
        text: `line ${i} => Line Content N° : ${i}\n\t\t\t\tMultiline : true\n\t\t\t\tpreserveLeadingSpaces : true`,
        preserveLeadingSpaces: true,
    }];
}

var dd = {
    content: [
        {
            table: {
                dontBreakRows: true,
                keepWithHeaderRows: 1,
                headerRows: 1,
                // Define width to prevent overflow
                widths: ['*'],
                body: __content
            },
            layout: {
                hLineWidth: function(i, node) {
                    return 1;
                },
                vLineWidth: function(i, node) {
                    return 1;
                },
                paddingTop: function(i, node) {
                    return 4;
                },
                paddingBottom: function(i, node) {
                    return 4;
                }
            }
        }
    ],
    // Instead of using pageBreakBefore, use pageSize and margins
    pageSize: 'A4',
    pageMargins: [40, 40, 40, 40]
};

Key changes made to fix the issue:

  1. Removed the pageBreakBefore function and instead used proper page sizing and margins
  2. Added table layout configuration to ensure consistent spacing
  3. Added widths property to prevent overflow
  4. Added keepWithHeaderRows and headerRows to maintain table structure
  5. Included proper padding to ensure content doesn't get cut off

If you still need to maintain specific control over page breaks, you could use this alternative approach:

var dd = {
    content: [
        {
            table: {
                dontBreakRows: true,
                widths: ['*'],
                body: __content
            }
        }
    ],
    pageSize: 'A4',
    pageMargins: [40, 40, 40, 40],
    pageBreakBefore: function(currentNode, followingNodesOnPage) {
        if (currentNode.table) {
            // Only break if we're at least 50 units from the bottom
            return currentNode.startPosition.top > 700;
        }
        return false;
    }
};

Additionally, if you're dealing with large datasets, you might want to consider splitting your content into multiple tables:

function splitIntoChunks(array, chunkSize) {
    let chunks = [];
    for (let i = 0; i < array.length; i += chunkSize) {
        chunks.push(array.slice(i, i + chunkSize));
    }
    return chunks;
}

let tableChunks = splitIntoChunks(__content, 10);

var dd = {
    content: tableChunks.map(chunk => ({
        table: {
            dontBreakRows: true,
            widths: ['*'],
            body: chunk
        },
        pageBreak: 'after' // except for last chunk
    })),
    pageSize: 'A4',
    pageMargins: [40, 40, 40, 40]
};

This approach gives you more control over where the breaks occur while ensuring no data is lost.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants