Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Unreleased

- Add a flag to ignore the trailing line break in text height calculations (including tables)

### [v0.17.1] - 2025-05-02

- Fix null values in table cells rendering as `[object Object]`
Expand Down
12 changes: 10 additions & 2 deletions lib/line_wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,19 @@ class LineWrapper extends EventEmitter {
if (bk.required || !this.canFit(word, w)) {
// if the user specified a max height and an ellipsis, and is about to pass the
// max height and max columns after the next line, append the ellipsis
const lh = this.document.currentLineHeight(true);
let lh = this.document.currentLineHeight(true);

if (options.trailingLineBreak === false) {
if (bk.required) lh = this.document.currentLineHeight();
}

if (
this.height != null &&
this.ellipsis &&
PDFNumber(this.document.y + lh * 2) > this.maxY &&
// If the current line and its following one cant fit, then render the ellipsis
PDFNumber(
this.document.y + lh + this.document.currentLineHeight(true),
) > this.maxY &&
this.column >= this.columns
) {
if (this.ellipsis === true) {
Expand Down
11 changes: 10 additions & 1 deletion lib/mixins/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ export default {
}

let contentHeight = this.y - y;
if (options.trailingLineBreak === false) {
const fontGap = this.currentLineHeight(true) - this.currentLineHeight();
contentHeight -= fontGap;
}
// Clamp height to max height
if (options.height) contentHeight = Math.min(contentHeight, options.height);

Expand Down Expand Up @@ -245,7 +249,12 @@ export default {
this.y += this.currentLineHeight(true) + lineGap;
});

const height = this.y - y;
let height = this.y - y;
if (options.trailingLineBreak === false) {
const fontGap = this.currentLineHeight(true) - this.currentLineHeight();
height -= fontGap;
}

this.x = x;
this.y = y;

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions tests/visual/table.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,25 @@ describe('table', function () {
});
});
});

test('ignore trailing line break - issue #1620', function () {
return runDocTest(
{
systemFonts: true,
failureThreshold: 0.002,
failureThresholdType: 'percent',
},
function (doc) {
doc.table({
debug: true,
data: [['trailingLineBreak\ndefault (true)']],
});
doc.table({
debug: true,
defaultStyle: { textOptions: { trailingLineBreak: false } },
data: [['trailingLineBreak\nfalse']],
});
},
);
});
});
30 changes: 30 additions & 0 deletions tests/visual/text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,34 @@ describe('text', function () {
.fill('blue');
});
});

test('ignore trailing line break - issue #1620', function () {
return runDocTest(
{
systemFonts: true,
failureThreshold: 0.002,
failureThresholdType: 'percent',
},
function (doc) {
const text = 'test\ntest';
let heightWithout = doc.heightOfString(text, {
trailingLineBreak: false,
});
doc
.save()
.rect(doc.x, doc.y, doc.page.contentWidth, heightWithout)
.strokeColor('red', 0.3)
.stroke()
.restore();
let height = doc.heightOfString(text);
doc
.save()
.rect(doc.x, doc.y, doc.page.contentWidth, height)
.strokeColor('blue', 0.3)
.stroke()
.restore();
doc.text(text, { height });
},
);
});
});