Skip to content

Commit

Permalink
Merge pull request #2771 from fe-cj/fix/issue-371
Browse files Browse the repository at this point in the history
Fixes issue #371
  • Loading branch information
liborm85 authored Aug 28, 2024
2 parents 259b669 + 5a1c380 commit 9b89aff
Show file tree
Hide file tree
Showing 3 changed files with 57 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 @@ -186,6 +186,16 @@ class DocumentContext extends EventEmitter {
let prevPage = this.page;
let prevY = this.y;

// If we are in a column group
if (this.snapshots.length > 0) {
let 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);
}
}

let createNewPage = nextPageIndex >= this.pages.length;
if (createNewPage) {
let 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 @@ -511,6 +511,22 @@ class LayoutBuilder {
}

processRow(columns, widths, gaps, tableBody, tableRow, height) {
const updatePageBreakData = (page, prevY) => {
let pageDesc;
// Find page break data for this row and page
for (let i = 0, l = pageBreaks.length; i < l; i++) {
let 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);
}
};

const storePageBreakData = data => {
let pageDesc;

Expand Down Expand Up @@ -595,6 +611,9 @@ class LayoutBuilder {
}
}

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

this.writer.context().completeColumnGroup(height, endingSpanCell);

this.writer.removeListener('pageChanged', storePageBreakData);
Expand Down
30 changes: 28 additions & 2 deletions tests/unit/LayoutBuilder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1686,13 +1686,24 @@ 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 @@ -1703,7 +1714,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 @@ -1714,6 +1725,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 All @@ -1727,6 +1750,9 @@ describe('LayoutBuilder', function () {
});
});




describe('dynamic header/footer', function () {
var docStructure, pdfDocument, styleDictionary, defaultStyle, background, header, footer, watermark, pageBreakBeforeFunction;

Expand Down

0 comments on commit 9b89aff

Please sign in to comment.