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

Printing list item with line break fails with Unsupported Number: NaN #1486

Closed
filecage opened this issue Dec 20, 2023 · 0 comments · Fixed by #1487
Closed

Printing list item with line break fails with Unsupported Number: NaN #1486

filecage opened this issue Dec 20, 2023 · 0 comments · Fixed by #1487

Comments

@filecage
Copy link
Contributor

Bug Report

Description of the problem

When using doc.list() to print list items, the process fails if one list item contains a line break (newline or return carriage characters both affected).

This is due to the internal flatten() of the text mixin calculates the amount of levels based on the number of list items given here:

pdfkit/lib/mixins/text.js

Lines 124 to 142 in 6338314

var flatten = function(list) {
let n = 1;
for (let i = 0; i < list.length; i++) {
const item = list[i];
if (Array.isArray(item)) {
level++;
flatten(item);
level--;
} else {
items.push(item);
levels.push(level);
if (listType !== 'bullet') {
numbers.push(n++);
}
}
}
};
flatten(list);

But the firstLine event is emitted per actual line, causing the event handler to access an undefined level item here:
https://github.com/foliojs/pdfkit/blob/6338314daf44f7f188e4cc6cb4d70fc2f2698fca/lib/mixins/text.js#L179-182

Which then results in diff being NaN and the consecutive variables this.x and wrapper.lineWidth as well.

From my knowledge of the library, I can't tell whether flatten() should produce the levels based on the actual lines or the firstLine event should only be emitted once per list item.

Code sample

With any PDFKit document, call

doc.list(['Foo\nBar']);

Your environment

  • pdfkit version: 0.14.0
  • Node version: 20.10.0
  • Browser version (if applicable): -
  • Operating System: macOS 14.1.2
filecage added a commit to filecage/pdfkit that referenced this issue Dec 20, 2023
filecage added a commit to filecage/pdfkit that referenced this issue Dec 20, 2023
filecage added a commit to filecage/pdfkit that referenced this issue Dec 20, 2023
blikblum pushed a commit that referenced this issue Dec 22, 2023
* adds test case with line-breaks in list items

reproduces #1486

* use LineBreaker per item and only draw `firstLine` once

fixes #1486

* boyscout: consistent naming

* boyscout: test multiple line break versions

* update changelog for fix of #1486

---------

Co-authored-by: David <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant