Skip to content

Commit

Permalink
backport PR #2771
Browse files Browse the repository at this point in the history
  • Loading branch information
liborm85 committed Aug 28, 2024
1 parent dc73c5a commit 6eb85c3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/documentContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,16 @@ DocumentContext.prototype.moveToNextPage = function (pageOrientation) {
var prevPage = this.page;
var prevY = this.y;

// If we are in a column group
if (this.snapshots.length > 0) {
var lastSnapshot = this.snapshots[this.snapshots.length - 1];
// We have to update prevY accordingly by also taking into consideration
// the 'y' of cells that don't break page
if (lastSnapshot.bottomMost && lastSnapshot.bottomMost.y) {
prevY = Math.max(this.y, lastSnapshot.bottomMost.y);
}
}

var createNewPage = nextPageIndex >= this.pages.length;
if (createNewPage) {
var currentAvailableWidth = this.availableWidth;
Expand Down
19 changes: 19 additions & 0 deletions src/layoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,11 +591,30 @@ LayoutBuilder.prototype.processRow = function (columns, widths, gaps, tableBody,
}
}

// If there are page breaks in this row, update data with prevY of last cell
updatePageBreakData(self.writer.context().page, self.writer.context().y);

self.writer.context().completeColumnGroup(height, endingSpanCell);
});

return { pageBreaks: pageBreaks, positions: positions };

function updatePageBreakData(page, prevY) {
var pageDesc;
// Find page break data for this row and page
for (var i = 0, l = pageBreaks.length; i < l; i++) {
var desc = pageBreaks[i];
if (desc.prevPage === page) {
pageDesc = desc;
break;
}
}
// If row has page break in this page, update prevY
if (pageDesc) {
pageDesc.prevY = Math.max(pageDesc.prevY, prevY);
}
}

function storePageBreakData(data) {
var pageDesc;

Expand Down
26 changes: 24 additions & 2 deletions tests/layoutBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1691,13 +1691,23 @@ describe('LayoutBuilder', function () {
});

it('on page break should return an entry with ending/starting positions', function () {
var doc = createTable(0, 1, 10, 5, 5);
var result = builder2.processRow(doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);

assert(result.pageBreaks instanceof Array);
assert.equal(result.pageBreaks.length, 1);
assert.equal(result.pageBreaks[0].prevPage, 0);
assert.equal(result.pageBreaks[0].prevY, 40 + 12 * 6);
});

it('on page break should return an entry with ending/starting positions 2', function () {
var doc = createTable(0, 1, 10, 5);
var result = builder2.processRow(doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);

assert(result.pageBreaks instanceof Array);
assert.equal(result.pageBreaks.length, 1);
assert.equal(result.pageBreaks[0].prevPage, 0);
assert.equal(result.pageBreaks[0].prevY, 40 + 12 * 5);
assert.equal(result.pageBreaks[0].prevY, 40 + 12 * 11);
});

it('on multi-pass page break (columns or table columns) should treat bottom-most page-break as the ending position ', function () {
Expand All @@ -1708,7 +1718,7 @@ describe('LayoutBuilder', function () {
});

it('on multiple page breaks (more than 2 pages), should return all entries with ending/starting positions', function () {
var doc = createTable(0, 1, 100, 90);
var doc = createTable(0, 1, 100, 90, 90);
var result = builder2.processRow(doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);

assert(result.pageBreaks instanceof Array);
Expand All @@ -1719,6 +1729,18 @@ describe('LayoutBuilder', function () {
assert.equal(result.pageBreaks[1].prevY, 40 + (90 - 60) * 12);
});

it('on multiple page breaks (more than 2 pages), should return all entries with ending/starting positions 2', function () {
var doc = createTable(0, 1, 100, 90);
var result = builder2.processRow(doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);

assert(result.pageBreaks instanceof Array);
assert.equal(result.pageBreaks.length, 2);
assert.equal(result.pageBreaks[0].prevPage, 0);
assert.equal(result.pageBreaks[0].prevY, 40 + 60 * 12);
assert.equal(result.pageBreaks[1].prevPage, 1);
assert.equal(result.pageBreaks[1].prevY, 40 + 41 * 12);
});

it('on multiple and multi-pass page breaks should calculate bottom-most endings for every page', function () {
var doc = createTable(0, 1, 100, 90, 92);
var result = builder2.processRow(doc.table.body[0], doc.table.widths, doc._offsets.offsets, doc.table.body, 0);
Expand Down

0 comments on commit 6eb85c3

Please sign in to comment.